【问题标题】:Amplify GraphQL Error not authorized to access create放大 GraphQL 错误未授权访问创建
【发布时间】:2020-11-26 05:36:49
【问题描述】:

在我的应用中,用户可以与其他用户开始对话,并且在创建对话后,他们可以互相发送消息。但是,在尝试创建两个用户之间的对话后,我收到一条错误消息

 Failed to create graphql GraphQLResponseError<Conversation>: GraphQL service returned a successful response containing errors: [Amplify.GraphQLError(message: "Not Authorized to access createConversation on type Conversation", locations: Optional([Amplify.GraphQLError.Location(line: 2, column: 3)]), path: Optional([Amplify.JSONValue.string("createConversation")]), extensions: Optional(["data": Amplify.JSONValue.null, "errorType": Amplify.JSONValue.string("Unauthorized"), "errorInfo": Amplify.JSONValue.null]))]
Recovery suggestion: The list of `GraphQLError` contains service-specific messages 

这是我第一次使用 GraphQL,它可能会显示出来。我想给模型Conversationmembers 创建他们的convo 的能力。任何人都可以引导我朝着正确的方向前进吗? 以下是我的 GraphQL Schema

type User 
  @model 
  @auth(rules: [{ allow: owner, operations: [create, delete, update]}]) {
  id: ID!
  userSub: String!
  fullName: String!
  profileImageFileName: String!
  conversations: [ConvoLink] @connection(name: "UserLinks")
  messages: [ChatMessage] @connection(name: "UserMessages", keyField: "authorId")
  createdAt: String
  updatedAt: String
}

type Conversation
  @model
  @auth(rules: [{ allow: owner, ownerField: "members", operations: [create, delete, update] }]) {
  id: ID!
  messages: [ChatMessage] @connection(name: "ConvoMsgs", sortField: "createdAt")
  associated: [ConvoLink] @connection(name: "AssociatedLinks")
  name: String!
  members: [String!]!
  createdAt: String
  updatedAt: String
}

type ChatMessage 
  @model
  @auth(rules: [{ allow: owner, ownerField: "authorId" }]) {
  id: ID!
  author: User @connection(name: "UserMessages", keyField: "authorId")
  authorId: String
  content: String!
  conversation: Conversation! @connection(name: "ConvoMsgs")
  messageConversationId: ID!
  createdAt: String
  updatedAt: String
}

type ConvoLink 
  @model(
    mutations: { create: "createConvoLink", update: "updateConvoLink" }
    queries: null
    subscriptions: null
  ) {
  id: ID!
  user: User! @connection(name: "UserLinks")
  convoLinkUserId: ID
  conversation: Conversation! @connection(name: "AssociatedLinks")
  convoLinkConversationId: ID!
  createdAt: String
  updatedAt: String
}

Swift 代码

func createConvo(){
      let conversation = Conversation( messages: List<ChatMessage>.init(), associated: List<ConvoLink>.init(),name: "convo", members: [currentUserSub, recieverUserSub])
        _ = Amplify.API.mutate(request: .create(conversation)) { event in
            switch event {
            case .success(let result):
                switch result {
                case .success(let convo):
                  //  DispatchQueue.main.async {
                        
                        print("Successfully created the convo: \(convo)")
                       // self.messageButton.isHidden = true
                   // }
                case .failure(let graphQLError):
                    print("Failed to create graphql \(graphQLError)")
                    //  self.checkIfOffline()
                }
            case .failure(let apiError):
                print("Failed to create a todo", apiError)
                // self.checkIfOffline()
            }
        }
}

【问题讨论】:

    标签: swift graphql aws-amplify


    【解决方案1】:

    当您在模型类型上使用 auth 指令时,即。 @auth(rules: [{allow: owner, operations: [create, delete, update]}) 需要经过身份验证的用户才能调用 API。

    用户是否在Amplify.API.mutate/query/subscribe 之前使用Amplify.Auth.signIn 登录?

    调试

    由于您使用的是 auth 指令,因此配置 API 也会配置 auth 类别。您可以通过amplify console auth 打开配置的身份验证资源并选择 Cognito 用户池。您可以通过控制台创建一个用户,它将在需要新密码的状态下创建。

    然后,您可以运行 amplify console api,选择 GraphQL,打开 AppSync 控制台,然后导航到查询选项卡以测试您的 API。在测试您的 API 时,查询控制台应指示您对该 API 的主要授权模式,并要求您登录。从 Cognito 用户池控制台获取 Web 应用程序客户端 ID,其中包含您创建的前一个用户的用户名和密码。一旦您尝试登录,它会提示您输入新密码。通过查询控制台对用户进行身份验证后,您可以测试您的 API 调用

    应用内

    如果您能够通过将 Amplify.API.signUp/confirmSignUp/signIn 流程合并到您的应用程序来通过 NotAuthorized 错误,但仍然看到 API 调用的其他问题,请随时在 Github 存储库中的问题中提供更多详细信息:@ 987654321@,因为 repo 受到主动监控

    【讨论】:

    • 在修改架构一段时间后,我删除了 Conversations 类型的身份验证规则 ownerField 并解决了错误。
    • @GBMR 我遇到了一些类似的问题,我在这里提出了这个问题:stackoverflow.com/questions/68465885/…,但为什么要删除 ownerField?
    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2021-12-30
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多