Home > Software design >  How to pass enum value inside GraphQL query
How to pass enum value inside GraphQL query

Time:12-17

GraphsQL mutation gives the following error. I have attached my Query and code.

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)

Query:

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

And Inside API.swift this Enum gets generated automatically from the schema.json file.

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,
    ]
  }
}

And in my code, I am calling this method as follows

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

CodePudding user response:

I don't believe your immediate problem is with enums, but rather how you are calling the mutation.

The mutation's argument signature needs to match the schema when you call it in the client:

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

addTicket's top-level arg is input in your schema.

  • Related