我正在通过https://github.com/the-road-to-graphql/fullstack-apollo-express-postgresql-boilerplate学习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') };
},
我如何访问response对象并添加cookie,而不是返回令牌obj?
发布于 2020-04-14 04:40:17
您可以使用context对象来实现这一点,查看您发送的示例。您需要从此函数https://github.com/the-road-to-graphql/fullstack-apollo-express-postgresql-boilerplate/blob/master/src/index.js#L55返回res
变量
上下文对象位于解析器的第三个参数处。上下文是在每个请求上创建的&可供所有解析器使用。
示例:
const server = new ApolloServer({
context: ({res}) => ({res})
});
function resolver(root, args, context){
context.res// express
}
https://stackoverflow.com/questions/61200569
复制相似问题