da45
da45

Reputation: 381

AWS Amplify Graphql Schema, how to filter own data?

consider for example a Comment type as discribed below:

type Comment @model  @auth(rules: [
  { allow: public, operations: [read]}
  { allow: owner }
]) {
id: ID!
text: String!
}

my question is how can an owner filter his own comments, is there any implicit relation that we can use between Apmlify Auth & Amplify api , otherwise I will add a new field in the schema

cognitoID @index(name:'byCognitoID', queryField: "commentByCognitoID")

thanks

Upvotes: 2

Views: 837

Answers (2)

Robbie Dutton
Robbie Dutton

Reputation: 1

The new way V2 is to use the "ownerField" attribute as shown below.

type Recipe 
@model 
@auth(rules: [{ allow: owner, ownerField:"author" }]) 
{
  id: ID!
  title: String!
  ingredients: [String]!
  instructions: [String]!
  author: String
}

An example is shown here https://docs.amplify.aws/javascript/build-a-backend/graphqlapi/customize-authorization-rules/#per-user--owner-based-data-access

Upvotes: 0

huzz
huzz

Reputation: 76

You have to create the field to have the relationship:

type Comment @model  @auth(rules: [
   { allow: owner }
   { allow: public, operations: [read]}
]) {
id: ID!
text: String!
owner: String @index(name: "commentsByOwner", queryField: "commentsByOwner")
}

Then use:

amplify push

Now it will create your queries and you should see one named commentsByOwner.

I realize this is still creating a new property in your schema, but I believe this is the only way to do this for now without removing the public auth rule.

https://docs.amplify.aws/cli-legacy/graphql-transformer/auth/#field-level-authorization

Upvotes: 4

Related Questions