【问题标题】:amplify subscription using auth in swift在 swift 中使用 auth 放大订阅
【发布时间】:2021-03-14 16:51:44
【问题描述】:

我想设置“移动”的实时列表,所以我使用了放大文档中的这个 sn-p。

 func createSubscription() {
        subscription = Amplify.API.subscribe(request: .subscription(of: Move.self, type: .onCreate))
        dataSink = subscription?.subscriptionDataPublisher.sink {
            if case let .failure(apiError) = $0 {
                print("Subscription has terminated with \(apiError)")
            } else {
                print("Subscription has been closed successfully")
            }
        }
        receiveValue: { result in
            switch result {
            case .success(let createdTodo):
                print("Successfully got todo from subscription: \(createdTodo)")
            case .failure(let error):
                print("Got failed result with \(error.errorDescription)")
            }
        }
    }

架构身份验证规则

type Move 
  @model 
  @auth( rules: [
    { allow: owner, ownerField: "owner", operations: [create, update, delete, read] },
  ]) 
{

但由于我将身份验证添加到“移动”类型,因此出现此错误。 GraphQLResponseError<Move>: GraphQL service returned a successful response containing errors: [Amplify.GraphQLError(message: "Validation error of type MissingFieldArgument: Missing field argument owner @ \'onCreateMove\'", locations: nil, path: nil, extensions: nil)]

Recovery suggestion: The list of GraphQLError contains service-specific messages

所以一切都在本地工作,但我认为我需要将授权传递给请求,但我找不到任何方法。有什么想法可以让我正确处理这个请求吗?

【问题讨论】:

    标签: swift authentication graphql amplify


    【解决方案1】:

    通过编写我自己的请求并直接传递所有者字段来使其工作

    extension GraphQLRequest {
        static func newMoves() -> GraphQLRequest<Move> {
            let operationName = "getMove"
            let document = """
            subscription MySubscription {
              onCreateMove(owner: "MyUser") {
                accerationMagnitude
                id
              }
            }
    
            """
            return GraphQLRequest<Move>(document: document,
    //                                    variables: [],
                                        responseType: Move.self,
                                        decodePath: operationName)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 2022-12-22
      • 2021-03-03
      • 2018-01-20
      相关资源
      最近更新 更多