简体   繁体   English

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

[英]How do I get cookie from apollo graphql resolver?

I can set cookie in client, but I don't know how to get cookies from server reslover (apollo graphql.)我可以在客户端设置 cookie,但我不知道如何从服务器 reslover 获取 cookies(apollo graphql。)

// resolver.js // 解析器.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 // 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?我错过了什么吗?

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.我认为这与 Apollo Server 本身无关,因为假设您使用 express, req object 会直接传递到上下文中,并且根本不会被 Apollo 修改。

Instead i would check, if you even parse the cookies.相反,我会检查,如果你甚至解析 cookies。 For that, it is recommended to use cookie-parser lib and run it as a middleware.为此,建议使用cookie-parser lib并将其作为中间件运行。

Also, the cookie object should be then in req.cookies (not in req.headers.cookies )此外,cookie object 应该在req.cookies中(不在req.headers.cookies中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM