首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >放大未授权访问创建的GraphQL错误

放大未授权访问创建的GraphQL错误
EN

Stack Overflow用户
提问于 2020-08-06 00:57:17
回答 1查看 10.8K关注 0票数 5

在我的应用程序中,用户可以与其他用户开始对话,并且在创建会话之后,用户可以相互发送消息。然而,在试图在两个用户之间创建一个对话之后,我发现一个错误:

代码语言:javascript
复制
 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,它可能会显示。我想让members of model Conversation能够创建他们的会议。有人能把我引向正确的方向吗?下面是我的GraphQL模式

代码语言:javascript
复制
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码

代码语言:javascript
复制
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()
            }
        }
}
EN

回答 1

Stack Overflow用户

发布于 2020-08-06 06:03:24

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

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

调试

由于您使用的是auth指令,所以提供API也会提供auth类别。您可以通过amplify console auth打开所提供的auth资源,并选择科尼图用户池。您可以通过控制台创建用户,它将在需要新密码的状态下创建。

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

应用程序中的

如果您能够通过将NotAuthorized流集成到应用程序中来传递Amplify.API.signUp/confirmSignUp/signIn错误,但仍然可以看到API调用的其他问题,那么可以在此提供更多关于Github回购中的问题的更多细节:https://github.com/aws-amplify/amplify-ios/issues作为回购程序被积极监控。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63275412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档