使用 aws-amplify 更新 GraphQL 对象时出现授权错误

发布于 2025-01-18 13:30:18 字数 1502 浏览 2 评论 0原文

我在使用AWS Amplify生成GraphQl API时会遇到问题,

我的模型主要具有两个对象(用户和消息):

用户对象:

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
}

消息对象:

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
}

Hasmany/>两者之间两者之间的关系。

在我签名到API并尝试通过JS API创建用户对象后,我会收到以下错误

'未被授权在类型ModelMessageConnection上访问消息'

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

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,
            },
          })
        );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2025-01-25 13:30:18

这实际上是由于缺少read操作的消息规则所致。将对象模型更改为下面的代码修复

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文