LoginSignup
2
0

More than 1 year has passed since last update.

AWS Amplify + Ionic(Angular) で、AWSにデータを簡単に保存・取得する(2)

Posted at

はじめに

前回に引き続き、AWS上にWebアプリのデータを簡単に保存・取得する方法を考えていきます。
今回は、GraphQLスキーマーの編集をします。

関連ページ

https://qiita.com/too/items/fae2879ea36f00c3ae10
https://docs.amplify.aws/cli/graphql/authorization-rules/

@authの設定

AWS Amplifyで作成した GraphQLのスキーマーは、初期状態では、ほぼ下記の様になっています。

schema.graphql
# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql/authorization-rules
input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!

type Storage @model {
  id: ID!
  data: String!
}

この中に、気になる文言があります。

# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql/authorization-rules
input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!

FOR TESTING ONLY! とは、気になる文言ですね。。。
上記の指示通り、https://docs.amplify.aws/cli/graphql/authorization-rules/で確認してみました。

なになに、、、。input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!が記述してあると、誰でもCreate/Read/Update/Deleteができる状態になります。

ということで、スキーマ定義を直していきます。

まず、上記の一文をコメントアウトします。

代わりに データモデルに対して、@authでアクセス権を設定します。
今回の場合、誰でもアクセス可能としたいので、allow:publicを設定します。

schema.graphql
# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql/authorization-rules
# input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!

type Storage @model @auth(rules: [{ allow: public }]){
  id: ID!
  data: String!
}

上記修正後、amplify pushを実施して、スキーマー設定を反映させます。

今回の場合、@authを allow:pubicにしましたが、
Cognitoを使ったユーザー認証を行なって、
自分のデータ以外アクセスできない様にしたい場合は、allow:ownerを設定します。

以上です。

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0