Amplify GraphQL 错误无权访问创建

GBM*_*BMR 7 swift graphql aws-amplify

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

 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 
Run Code Online (Sandbox Code Playgroud)

这是我第一次使用 GraphQL,它可能表明了这一点。我想赋予members模型Conversation创建他们的 convo 的能力。有人能引导我走向正确的方向吗?以下是我的 GraphQL 架构

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
}
Run Code Online (Sandbox Code Playgroud)

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()
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

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

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

调试

由于您使用的是 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 存储库中的问题中提供更多详细信息:https ://github.com /aws-amplify/amplify-ios/issues因为存储库受到主动监控