如何在 GraphQL 查询中传递枚举值

How to pass enum value inside GraphQL query

GraphsQL 突变出现以下错误。我附上了我的查询和代码。

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 中,此枚举是从 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 ....
}

我不认为你的直接问题是枚举,而是你如何调用突变。

当您在客户端中调用它时,突变的参数签名需要匹配模式:

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

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