Graphql - Not Authorized to access error message from AWS AppSync Amplify Console

Viewed 3135

I'm working with React + NextJs and have Amplify Graphql implemented. I'm logged in and set my current user as an Admin group, however, I'm not able to mutate a data based on authorId.

The logged in userId is 1234 (for example)

type Xp
  @model
  @key(name: "xpsByUserId", fields: ["authorId"])
  @auth(
    rules: [
      { allow: owner, ownerField: "authorId" }
      { allow: public, operations: [read] }
      { allow: private, operations: [read] }
    ]
  ) {
  id: ID!
  authorId: ID!
  author: User @connection(fields: ["authorId"])
  name: String
  visibility: Visibility
  post: [Post] @connection(name: "XpPosts")
  createdAt: String
}

Trying to mutate:

mutation MyMutation {
  createXp(input: {authorId: "1234", name: "fdsfa"}) {
    id
    name
    author {
      username
    }
  }
}

I get a message: Not Authorized to access createXp on type Xp

This is my user type:

type User
  @model(subscriptions: null)
  @key(fields: ["userId"])
  @auth(
    rules: [
      { allow: groups, groups: ["Admin"] }
      { allow: owner, ownerField: "userId" }
      { allow: private, operations: [read] }
    ]
  ) {
  userId: ID!
  username: String!
  email: String!
  posts: [Post] @connection(keyName: "postsByUserId", fields: ["userId"])
  xps: [Xp] @connection(keyName: "xpsByUserId", fields: ["userId"])
  createdAt: String
  updatedAt: String
  following: [Following] @connection(keyName: "followingByUserId", fields: ["userId"])
}

What am I doing wrong here?

1 Answers

Try mentioning auth rules in Xp model like below as per link,

type Xp
  @model
  @key(name: "xpsByUserId", fields: ["authorId"])
  @auth(
    rules: [
      { allow: owner, ownerField: "authorId", operations: [create, delete, read, update] },
      { allow: groups, groups: ["Admin"], operations: [create, delete, read, update]  },
      { allow: public, operations: [read] },
      { allow: private, operations: [read] }
    ]
  ) {
  id: ID!
  authorId: ID!
  author: User @connection(fields: ["authorId"])
  name: String
  visibility: Visibility
  post: [Post] @connection(name: "XpPosts")
  createdAt: String
}
Related