Express 从GraphQL解析器设置Cookie

Express 从GraphQL解析器设置Cookie,express,graphql,Express,Graphql,我正在通过学习GraphQL 我想知道如何从解析器设置cookie,因为我已经习惯使用Express来设置cookie signIn: async ( parent, { login, password }, { models, secret }, ) => { const user = await models.User.findByLogin(login); if (!user) { throw new UserInputError( 'No

我正在通过学习GraphQL

我想知道如何从解析器设置cookie,因为我已经习惯使用Express来设置cookie

signIn: async (
  parent,
  { login, password },
  { models, secret },
) => {
  const user = await models.User.findByLogin(login);

  if (!user) {
    throw new UserInputError(
      'No user found with this login credentials.',
    );
  }

  const isValid = await user.validatePassword(password);

  if (!isValid) {
    throw new AuthenticationError('Invalid password.');
  }

  return { token: createToken(user, secret, '5m') };
},

如何访问响应对象并添加cookie,而不是返回token obj?

您可以使用上下文对象来实现这一点,查看您发送的示例。您需要从此函数返回
res
变量

上下文对象位于解析器的第三个参数处。上下文是在每个请求上创建的&可供所有解析程序使用

例如:

const server=新服务器({
上下文:({res})=>({res})
});
函数解析程序(根、参数、上下文){
context.res//express
}

like context:async({req,res,connection})=>{?@woopsie yes there,
返回{req,res,connection}