How do I get cookie from apollo graphql resolver?

Viewed 5319

I can set cookie in client, but I don't know how to get cookies from server reslover (apollo graphql.)

// resolver.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',
  })

Am I miss something?

1 Answers

I don't think that this has to do something with Apollo Server itself, because assuming you use express, the req object is passed directly into context and isn't modified by Apollo at all.

Instead i would check, if you even parse the cookies. For that, it is recommended to use cookie-parser lib and run it as a middleware.

Also, the cookie object should be then in req.cookies(not in req.headers.cookies)

Related