简体   繁体   English

如何通过GraphQL将响应标头转发到客户端

[英]How to forward response headers through GraphQL to client

I'm having an issue using GraphQL where the response headers are being dropped when returning to client. 我在使用GraphQL时遇到问题,返回到客户端时响应头被删除。 My application is using NodeJS, Passport.js and Express Cookie-Session to manage authentication and sessions. 我的应用程序使用NodeJS,Passport.js和Express Cookie-Session来管理身份验证和会话。

I have an existing /login route which authenticates with Passport and returns a user{} object and set-cookie headers to the client. 我有一个现有的/login路由,该路由通过Passport进行身份验证,并向客户端返回一个user{}对象和set-cookie标头。 In result, a cookie is created on the client's browser and their session is established. 结果,在客户端的浏览器上创建了一个cookie并建立了他们的会话。

Now, we have recently added GraphQL and I created a login query, which calls the existing /login route and responds with the user{} object. 现在,我们最近添加了GraphQL,我创建了一个login查询,该查询调用现有的/login路由并使用user{}对象进行响应。 However, when the GraphQL returns the response to the client, the response headers are no longer included so the cookie is never created. 但是,当GraphQL将响应返回给客户端时,不再包含响应​​标头,因此永远不会创建cookie。

How can I forward the original response headers through GraphQL to reach the client? 如何通过GraphQL转发原始响应标头以到达客户端?

Here is an example of the GraphQL login: 这是GraphQL登录的示例:

login { 
  type: typeLogin,
  args : { 'username/password here' },
  async resolve(_, args, context) {
    const response = await fetch('/login', {
      credentials: 'include',
      method: 'POST',
      body: JSON.stringify('username/password here')
   })

   const data = await response.json();

   // console.log(response.headers) will display correct headers

   return data; // User is returned but response headers are not
   }
 }

This is how I already do that, 这就是我已经做到的

Link: https://github.com/masmerino13/graphql-backend/blob/master/src/resolvers/Mutations/UserMutation.js#L31 链接: https//github.com/masmerino13/graphql-backend/blob/master/src/resolvers/Mutations/UserMutation.js#L31

Let me know if this works for you 让我知道这是否适合您

Regards! 问候!

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

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