【问题标题】:How to pass enum value inside GraphQL query如何在 GraphQL 查询中传递枚举值
【发布时间】:2022-01-20 06:27:38
【问题描述】:

GraphQL 突变给出以下错误。我已附上我的查询和代码。

GraphQLResult<Data>(data: nil, errors: Optional([Validation error of type UnknownType: Unknown type TicketType, Validation error of type FieldUndefined: Field 'addTicket' in type 'Mutation' is undefined @ 'addTicket']), extensions: nil, source: Apollo.GraphQLResult<MyProject.MyMutation.Data>.Source.server, dependentKeys: nil)

查询:

mutation MyMutation($id: String!, $ticketType: TicketType) {
  addTicket(input: { id: $id, ticketType: $ticketType}) {
        accountId
        storyId
    }
  }

在 API.swift 内部,这个 Enum 是从 schema.json 文件中自动生成的。

public enum TicketType: RawRepresentable, Equatable, Hashable, CaseIterable, Apollo.JSONDecodable, Apollo.JSONEncodable {
  public typealias RawValue = String
  case normal
  case firstClass
  case secondClass
  /// Auto generated constant for unknown enum values
  case __unknown(RawValue)

  public init?(rawValue: RawValue) {
    switch rawValue {
      case "NORMAL": self = .normal
      case "FIRST_CLASS": self = .firstClass
      case "SECOND_CLASS": self = .secondClass
      default: self = .__unknown(rawValue)
    }
  }

  public var rawValue: RawValue {
    switch self {
      case .normal: return "NORMAL"
      case .firstClass: return "FIRST_CLASS"
      case .secondClass: return "SECOND_CLASS"
      case .__unknown(let value): return value
    }
  }

  public static func == (lhs: TicketType, rhs: TicketType) -> Bool {
    switch (lhs, rhs) {
      case (.normal, .normal): return true
      case (.firstClass, .firstClass): return true
      case (.secondClass, .secondClass): return true
      case (.__unknown(let lhsValue), .__unknown(let rhsValue)): return lhsValue == rhsValue
      default: return false
    }
  }

  public static var allCases: [TicketType] {
    return [
      .normal,
      .firstClass,
      .secondClass,
    ]
  }
}

在我的代码中,我调用这个方法如下

myNetworkObj.apollo.perform(mutation: addTicket(id: "1234", ticketType: .normal) {
result in ....
}

【问题讨论】:

    标签: ios swift objective-c graphql mutation


    【解决方案1】:

    我认为您的直接问题不在于枚举,而在于您如何称呼突变。

    mutation 的参数签名需要在客户端调用时与 schema 匹配:

    mutation: addTicket(input{id: "1234", ticketType: .normal})
    

    addTicket 的顶级 arg 在您的架构中是 input

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 2022-10-23
      • 2023-03-28
      • 2016-04-23
      • 2020-09-21
      • 1970-01-01
      • 2010-11-05
      • 2018-08-17
      • 2021-06-07
      相关资源
      最近更新 更多