如何从apollo graphql解析器中获取cookie?

问题描述 投票:0回答:1

我可以在客户端设置cookie,但不知道如何从服务器reslover(apollo graphql.)获取cookie。

/ resolver.js / apolloClient.js

export const resolvers = {
  Query: {
    async getSinglePost(_, args, context) {

        // "context.req.headers.cookies" are empty. d
        // Even if setting "credentials: 'include' " in creating Apollo client chche.   
    },
  },

/ apolloClient.js

new ApolloClient({
    ssrMode: Boolean(ctx),
    link: authLink.concat(new HttpLink({
      uri: 'http://localhost:4000/graphql', // Server URL (must be absolute)
      credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
      fetch,
    })),
    cache: new InMemoryCache({ fragmentMatcher }).restore(initialState),
    credentials: 'include',
  })

我是不是错过了什么?

graphql apollo apollo-client
1个回答
0
投票

我不认为这与Apollo服务器本身有什么关系,因为假设你使用express,则 req 对象是直接传递到上下文中的,根本不会被Apollo修改。

相反,我会检查,如果你甚至解析cookie。为此,建议使用 Cookie-parser lib 并将其作为中间件运行。

另外,cookie对象应该在 req.cookies(不在 req.headers.cookies)

© www.soinside.com 2019 - 2024. All rights reserved.