在 Graphql 游乐场上使用 AWS Cognito 用户池验证 AppSync 突变

ait*_*han 3 graphql aws-appsync

一个非常基本的场景,我想在 Graphql Playground 上测试 AppSync 突变,该突变与 API 密钥身份验证运行良好。

除了 API 密钥身份验证之外,我还附加了一个额外的授权提供程序。

额外授权截图

突变:

type Mutation {
  createPitch(gameID: ID!, pitchID: Int!, pitchEvent: PitchInput!): Pitch
    @aws_api_key
  predictPitch(userID: String!, gamePitchID: String!, prediction: PredictionInput): Prediction
    @aws_cognito_user_pools
}
Run Code Online (Sandbox Code Playgroud)

predictPitch在 graphql 游乐场上调用突变:

mutation PredictPitch($prediction:PredictionInput) {
  predictPitch(userID: "12345", gamePitchID: "29fb2xx-xxxxx-xxxxx-1", 
  prediction: $prediction ) {
    gameID
    gamePitchID
  }
}
Run Code Online (Sandbox Code Playgroud)

查询变量:

{
  "prediction": {
    "gameID": "29",
    "hitterGuess": "Miss",
    "pitcherGuess": "Fastball"
  }
}
Run Code Online (Sandbox Code Playgroud)

标题:


{
  "X-API-KEY": "da2-o6fs2lq47vbehexxxxxxxx",
  "Authorization": "Bearer xxxx-the-pretty-long-jwt-token-from-cognito login"
}
Run Code Online (Sandbox Code Playgroud)

我单独尝试过Authorizationheader 以及与x-api-key. 到目前为止没有任何效果。我很确定我错过了一点点。

{
  "error": {
    "errors": [
      {
        "errorType": "UnauthorizedException",
        "message": "Valid authorization header not provided."
      }
    ]
  }
}

Run Code Online (Sandbox Code Playgroud)

注意: JWT 令牌AccessToken是通过 aws-cli 生成的 aws cognito-idp admin-initiate-auth

ait*_*han 11

我必须在突变的同时添加@aws_cognito_user_pools类型。Prediction

type Prediction @aws_cognito_user_pools {
   gameID
   gamePitchID
}
Run Code Online (Sandbox Code Playgroud)

另外,从 Cognito 中我必须像这样使用 idToken:

{
   "Authorization": "xxxxxxxxxx"
}
Run Code Online (Sandbox Code Playgroud)

请注意Bearer缺少。

  • 看来我的评论对你来说很方便。我很高兴它有帮助! (2认同)