skip to Main Content

I want to create a User model and I want the User ID to always be equal to the Owner ID.
How to do this?

This is my schema.graphql file content:

type User @model {
  id: ID! @default(value: @owner) # == owner
  name: String!
  description: String
  birthday: AWSDateTime
}

Thank you

2

Answers


  1. I had the same problem and found the solution highlighted below:

    type User
      @model
      @auth(rules: [
        { allow: owner, ownerField: "id" }
        { allow: private, operations: [read] }
      ])
    {
      id: String @auth(rules: [
        { allow: owner, operations: [create, read, delete] } 
        { allow: private, operations: [read] }
      ])
      name: String!
      description: String
      birthday: AWSDateTime
    }
    

    I’ve specified on the model level @auth rules to use the id field as the "ownerField" and give other authenticated users a read access.

    Then on the field level, I’ve removed the update access to the owner (again using @auth rules), so it is impossible to re-assign the record later on.

    Login or Signup to reply.
  2. for my case, I just added sub from Congnito User:

    type User
      @model
      @auth(rules: [{ allow: private, operations: [read] }, { allow: owner }]) 
      {
      id: ID!
      sub: String! @index(name: "bySub", queryField: "userBySub")
    }
    

    sub is retrieved from cognito thanks to Auth.currentAuthenticatedUser(), when you login for the first time a new user row is added to the dynamo table User.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search