我是 Keystone.js 和 GraphQL 的新手。到目前为止,我已经能够成功执行以下 API 查询(取自此page) 通过 POST 请求:
const SIGNIN = `mutation signin($identity: String, $secret: String) {
authenticate: authenticateUserWithPassword(email: $identity, password: $secret) {
item {
id
name
}
}
}`;
// Returns id and name of authenticated user
and
const GET_ALL_POSTS = `query GetPosts {
allPosts {
name
id
}
}`;
// Returns id and name of all posts (if no access controls)
如果我设置访问控制对于列表Post
我按预期从第二个查询中收到访问错误,但我无法了解如何执行经过身份验证的查询allPosts
,例如我想:
- 通过电子邮件和密码(以编程方式)对用户进行身份验证
- 如果成功,则运行查询
allPosts
并返回结果
我究竟做错了什么?
答案是首先提交下面的查询,以验证您的用户并生成访问令牌:
query: `mutation ($identity: String, $secret: String) {
authenticate: authenticateUserWithPassword(email: $identity, password: $secret) {
token
}
}`
然后,通过将提供的令牌添加到请求的标头中来对后续查询进行身份验证:
'Authorization: Bearer <token>'.
更多信息here.
更普遍,GraphQL 游乐场对于任何刚接触 keystonejs 和 GraphQL 的新手来说,都非常值得一看。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)