执行经过身份验证的 keystone.js / GraphQL API 查询

Perform an authenticated keystone.js / GraphQL API query

我是 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)

如果我为列表 Post 设置 access controls,我会按预期从第二个查询中收到访问错误,但我不知道如何为 [=15 执行经过身份验证的查询=],例如我要:

我做错了什么?

答案是首先提交下面的查询,以验证您的用户并生成访问令牌:

query: `mutation ($identity: String, $secret: String) {
  authenticate: authenticateUserWithPassword(email: $identity, password: $secret) {
    token
  }
}`

随后的查询通过将提供的令牌添加到您的请求的 headers 中进行身份验证:

'Authorization: Bearer <token>'.

更多信息here

更一般地说,the GraphQL playground 非常值得学习 keystonejs 和 GraphQL 的新手。