React Apollo客户端不向Apollo Server 2.0发送cookie或标头

时间:2020-05-07 11:55:55

标签: reactjs react-apollo apollo-client graphql-js

我在使用React Apollo客户端和Apollo Server 2.0的cookie时遇到一些问题。 我的客户端不发送服务器API的cookie。我已经尝试了上面的答案。

运行在http://localhost:3000上的客户端(react-app)(React-Apollo-Client) 运行在http://localhost:4000/graphql上的API(Apollo Server 2.0,带有中间件的Express)

我的客户代码:

import ApolloClient from 'apollo-boost';
import { createHttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { gql } from "apollo-boost";

const link = createHttpLink({
  uri: 'http://localhost:4000/graphql',
  credentials: 'same-origin',
});

const client = new ApolloClient({
  link,
  cache: new InMemoryCache(),
  onError: ({ networkError, graphQLErrors }) => {
    console.log('graphQLErrors', graphQLErrors)
    console.log('networkError', networkError)
  }
});

client.query({ query: gql`
      {
        users{id,name} //My query in GraphQL
      }
    `
  })
  .then(result => console.log(result));

我的服务器:

const corsOptions = {
  origin: 'http://localhost:3000',
  credentials: true, // <-- REQUIRED backend setting
};
const app = express();

app.use(cors(corsOptions));
server.applyMiddleware({ app });

请问有人可以帮我吗?

0 个答案:

没有答案
相关问题