【问题标题】:apollo-client reactjs 404 (Not Found) on client side when use HttpLink使用HttpLink时客户端的apollo-client reactjs 404(未找到)
【发布时间】:2021-01-04 07:32:05
【问题描述】:

y react 应用程序总是向 http://localhost:3000 发送 post 请求,它会抛出这样的错误:

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!)
})

【问题讨论】:

    标签: react-apollo apollo-client apollo-server


    【解决方案1】:

    我通过改变解决了它

    来自

    从“@apollo/client”导入 { ApolloClient };

    从“apollo-client”导入 { ApolloClient };

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 2020-03-01
      • 2018-12-01
      • 2020-07-17
      • 2023-03-09
      • 2020-01-04
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多