Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用apollo server epxress从graphql解析器中更新会话_Node.js_Express_Graphql_Apollo Server - Fatal编程技术网

Node.js 如何使用apollo server epxress从graphql解析器中更新会话

Node.js 如何使用apollo server epxress从graphql解析器中更新会话,node.js,express,graphql,apollo-server,Node.js,Express,Graphql,Apollo Server,我有一个基于apollo server express的graphql服务器 我的解析器通常在遗留API上执行REST请求。 其中一个解析器通过将用户名和密码发送到后端服务器来执行用户身份验证。响应包括一个令牌,用于对后续请求进行身份验证 此时,我将令牌传递给客户端应用程序,并将其包含在后续请求中 我现在想将这个令牌保存在epxress会话中,这样就可以在后续解析器的上下文中隐式地传递它 但我不知道如何在冲突解决程序中收到响应后更新request.session。首先,通过将会话对象包含在上下文

我有一个基于apollo server express的graphql服务器

我的解析器通常在遗留API上执行REST请求。 其中一个解析器通过将用户名和密码发送到后端服务器来执行用户身份验证。响应包括一个令牌,用于对后续请求进行身份验证

此时,我将令牌传递给客户端应用程序,并将其包含在后续请求中

我现在想将这个令牌保存在epxress会话中,这样就可以在后续解析器的上下文中隐式地传递它


但我不知道如何在冲突解决程序中收到响应后更新request.session。

首先,通过将会话对象包含在上下文中,将其公开给冲突解决程序<默认情况下,code>expressgraphql包含请求对象作为上下文,但我认为Apollo server的中间件不具有这种行为——相反,我们需要显式定义上下文

app.use('/graphql', bodyParser.json(), (req, res, next) => {
  const context = { session:req.session }
  graphqlExpress({ schema })(req, res, next)
})
然后,在解析器中,只需将返回的令牌添加到会话对象:

const loginResolver = (obj, {username, password}, context) => {
  return apiAuthCall(username, password)
    .then(token => {
      context.session.token = token
      return // whatever other data, could just be a boolean indicated whether the login was successful
    })
}

谢谢与此同时,我自己也明白了,但我就是这么做的