简体   繁体   中英

Authorization error when updating a GraphQL object using aws-amplify

I'm experiencing issues when using aws amplify to generate a graphql API

My model has mainly two objects (User and messages):

User object:

type User
  @model(subscriptions: null)
  @auth(rules: [{ allow: owner, ownerField: "id" }]) {
  id: ID!
  email: String!
  username: String!
  avatar: S3Object
  name: String
  conversations: [Conversation] @manyToMany(relationName: "UserConversations")
  messages: [Message!]! @hasMany(indexName: "byAuthor", fields: ["id"])
  createdAt: String
  updatedAt: String
}

Message object:

type Message
  @model(subscriptions: null)
  @auth(
    rules: [
      {
        allow: owner
        ownerField: "authorId"
        operations: [create, update, delete]
      }
    ]
  ) {
  id: ID!
  author: User @belongsTo(fields: ["authorId"])
  authorId: ID! @index(name: "byAuthor", sortKeyFields: ["content"])
  content: String!
  conversation: Conversation! @belongsTo(fields: ["messageConversationId"])
  messageConversationId: ID!
    @index(name: "byConversation", sortKeyFields: ["createdAt"])
  createdAt: String
  updatedAt: String
}

There's a hasMany / belongsTo relationship between the two and auth rules on both.

After I signin to the API and try to create a user object through the JS API, i'm getting the following error

'Not Authorized to access messages on type ModelMessageConnection'

        await AuthAPI.graphql(
          graphqlOperation(createUser, {
            input: {
              id,
              username,
              email,
              name,
            },
          })
        );

This is actually due to the message rule that was missing the read action. Changing the object model to the code below fixed it

type Message
  @model(subscriptions: null)
  @auth(
    rules: [
      {
        allow: owner
        ownerField: "authorId"
        operations: [create, update, delete, read]
      }
    ]
  ) {
  id: ID!
  author: User @belongsTo(fields: ["authorId"])
  authorId: ID! @index(name: "byAuthor", sortKeyFields: ["content"])
  content: String!
  conversation: Conversation! @belongsTo(fields: ["messageConversationId"])
  messageConversationId: ID!
    @index(name: "byConversation", sortKeyFields: ["createdAt"])
  createdAt: String
  updatedAt: String
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM