如何解决使用HttpLink时,客户端上的apollo-client reactjs 404未找到
y react应用程序总是将发布请求发送到http:// localhost:3000,并且会引发如下错误:
createHttpLink.ts:121 POST http://localhost:3000/[object%20Object] 404 (Not Found)
当我尝试将uri传递给HttpLink时会发生这种情况,但是当我直接将uri传递给ApolloClient时,它运行良好,我已经尝试了多种方法的解决方案,但仍然对我不起作用,请帮助 我该如何解决这个问题
这是我的代码。
客户端
const httpLink = new HttpLink({
uri: "http://localhost:4000/graphql",credentials: "include"
});
const wsLink = new WebSocketLink({
uri: ws://localhost:4000/graphql,options: {
reconnect: true
}
});
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},wsLink,httpLink
)
const client = new ApolloClient({
uri: splitLink,cache: new InMemoryCache(),export default function ApolloProvider(props){
return <Provider client={client }{...props}/>
}
服务器端
const SERVER_PORT = 4000;
const DB_URI = "xxxxxxxxxxxxxxx";
const app = express();
const pubsub = new PubSub();
app.use(cors())
const apolloServer = new ApolloServer({
typeDefs: gql${typeDefs},resolvers,context: ({ req }) => ({ req,pubsub })
});
const db = async () => {
try {
const success = await mongoose.connect(DB_URI,{
useNewUrlParser: true,useUnifiedTopology: true,useCreateIndex: true,});
console.log('DB Connected');
} catch (error) {
console.log('DB Connection Error',error);
}
};
db();
apolloServer.applyMiddleware({ app });
const httpserver = http.createServer(app);
apolloServer.installSubscriptionHandlers(httpserver);
app.get('/',function (req,res) {
res.json({
data: 'BLANK PAGE !'
});
});
httpserver.listen({ port: SERVER_PORT },() => {
console.log(? Server ready at http://localhost:${SERVER_PORT} let's start!)
})
解决方法
我通过改变解决了它
来自
从'@ apollo / client'导入{ApolloClient};
到
从“ apollo-client”导入{ApolloClient};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。