use*_*715 0 node.js sequelize.js apollo graphql serverless
因此,我创建了一堆突变和查询,并将它们拼接在一起,这些工作并希望将身份验证引入到组合中。我添加了一个 HTTP 标头“x-token”来保存我的登录令牌,以便能够删除他们的工作或用户本身等内容。
const getMe = async req => {
const token = req.headers['x-token'];
if (token) {
try {
return await jwt.verify(token, "notSoSecret");
} catch (e) {
throw new AuthenticationError(
'Your session expired. Sign in again.',
);
}
}
};
const server = new ApolloServer({
typeDefs: schema,
resolvers,
formatError: error => {
// remove the internal sequelize error message
// leave only the important validation error
const message = error.message
.replace('SequelizeValidationError: ', '')
.replace('Validation error: ', '');
return {
...error,
message,
};
},
context: async ({ req }) => {
const me = await getMe(req);
return {
models,
me,
secret: "notSoSecret",
}
},
path: "/graphql"
});
server.applyMiddleware({ app });
sequelize.sync().then(async () => {
createUsersWithJob();
});
app.get("/playground", graphiql({ endpoint: "/graphql" }));
const handler = serverless(app);
export { handler };
const createUsersWithJob = ... //creates seed data
Run Code Online (Sandbox Code Playgroud)
所以当我添加令牌并查看我的命令行控制台时,我实际上看到我正在设置我想要的标题,但它一遍又一遍地循环并且不会停止。操场也收到错误“无法访问服务器”
{
"error": "Response not successful: Received status code 400"
}
Run Code Online (Sandbox Code Playgroud)
并且在我删除我在操场上设置的 HTTP 标头之前,运行 deleteUser 突变或任何其他突变和查询都不起作用。
还有一个次要问题,这个根文件中的所有内容都运行了两次,但目前对我来说还没有概述的标题问题那么大。
如果有人对此有任何见解,我很想知道更多。提前致谢。
编辑:只是一个快速编辑,当我硬编码一个预先存在的用户时,它可以正常工作。
我很难让 GraphQL Playground 的 React 版本在一个非常简单的 html 设置中工作,但我想出了一些可能对你也有帮助的东西(手指交叉)。
我headers在GraphQLPlayground.init调用中的配置中添加了一个部分,如下所示:
const root = document.getElementById('root');
GraphQLPlayground.init(root, {
endpoint: "/graphql",
headers: {
"Authorization": "Bearer " + token
}
})
Run Code Online (Sandbox Code Playgroud)
我有一个带有 id 的元素,root因为它嵌入在 HTML 中。
不确定这会帮助你,因为我刚刚从你的代码示例中注意到你正在调用graphiql它是一个与 GraphQL Playground 不同的 GraphQL 客户端。
GraphIQL:https : //github.com/skevy/graphiql-app
GraphQL 游乐场:https : //github.com/prisma/graphql-playground
| 归档时间: |
|
| 查看次数: |
13099 次 |
| 最近记录: |