我是 Keystone.js 和 GraphQL 的新手。到目前为止,我已经能够通过 POST 请求成功执行以下 API 查询(取自 page ):
const SIGNIN = `mutation signin($identity: String, $secret: String) {
authenticate: authenticateUserWithPassword(email: $identity, password: $secret) {
item {
id
name
}
}
}`;
// Returns id and name of authenticated user
和
const GET_ALL_POSTS = `query GetPosts {
allPosts {
name
id
}
}`;
// Returns id and name of all posts (if no access controls)
如果我设置access controls对于列表 Post
我按预期从第二个查询中收到访问错误,但我无法了解如何对 allPosts
执行经过身份验证的查询,例如我想要:
- 通过电子邮件和密码(以编程方式)对用户进行身份验证
- 如果成功,则运行
allPosts
查询并返回结果
我做错了什么?
最佳答案
答案是首先提交下面的查询,以验证您的用户并生成访问 token :
query: `mutation ($identity: String, $secret: String) {
authenticate: authenticateUserWithPassword(email: $identity, password: $secret) {
token
}
}`
然后通过将提供的 token 添加到请求的 header 中来对后续查询进行身份验证:
'Authorization: Bearer <token>'.
更多信息here .
更一般地说,the GraphQL playground对于任何进入 keystonejs 和 GraphQL 新手的人来说都非常值得一看。
关于node.js - 执行经过身份验证的 keystone.js/GraphQL API 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63077526/