cognito認証ありのAppSyncを試してみた
AppSync定義ファイル(schema.graphql)を、cognito認証ありに変更
amplify\backend\api\amplifyappsyncmock\schema.graphql
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | type Todo   @model   @auth(     rules: [       { allow: owner } # 作成者のみ変更可能       { allow: public, operations: [read] } # 閲覧は誰でも可能     ]   ) {   id: ID!   name: String!   description: String } type User   @model   @auth(     rules: [       { allow: owner } # 作成者のみ変更可能       { allow: private, operations: [read] } # ログインユーザなら読み取り可能     ]   ) {   id: ID!   name: String!   email: String! @index(name: "byEmail", queryField: "userByEmail")   createdAt: AWSDateTime! } | 
1, email認証のログインユーザを生成(仮登録)
2, メアドに届いた認証コードを使って本登録
3, emailとパスワードでログインして、JWT(JSON Web Token)を取得
4, JWTを使って、graphQL実行
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | # amplifyプロジェクトに、cognito認証を追加 amplify add auth # aws反映 amplify push # nameにプロジェクト名が入っているUserPool IDを取得 aws cognito-idp list-user-pools --max-results 10 --region ap-northeast-1 ユーザープールID(ログインユーザをグループ化しているもの)からクライアントID(APIキー)を取得f aws cognito-idp list-user-pool-clients \   --user-pool-id YOUR_USER_POOL_ID \   --region ap-northeast-1 # (sign-up) まずユーザ登録する。emailに認証コードが自動送信されるので受信できるメアドで! aws cognito-idp sign-up \   --region ap-northeast-1 \   --client-id YOUR_CLIENT_ID \   --username taro@example.com \   --password YourSecurePassword1! \   --user-attributes Name="email",Value="taro@example.com" #  (confirm-sign-up)メール認証(Eメールに届いたコードで本登録) aws cognito-idp confirm-sign-up \   --region ap-northeast-1 \   --client-id YOUR_CLIENT_ID \   --username taro@example.com \   --confirmation-code 275727 # emailとパスワードでログインして、JWT(JSON Web Token)を取得し、$TOKENに格納 TOKEN=$(aws cognito-idp initiate-auth \   --auth-flow USER_PASSWORD_AUTH \   --client-id YOUR_CLIENT_ID \   --auth-parameters USERNAME="taro@example.com",PASSWORD="YourSecurePassword1!" \   --query "AuthenticationResult.IdToken" --output text) echo $TOKEN # amplify pushした時のGraphQL endpointを使って、GraphQLを実行! curl -X POST "https://xxxxxxxxxxxxxxxxxxxxxx.appsync-api.ap-northeast-1.amazonaws.com/graphql" \   -H "Content-Type: application/json" \   -H "Authorization: Bearer $TOKEN" \   --data '{"query": "query ListUsers { listUsers { items { id name email } } }"}' |