apollo-ios: Incorrect code generation: String instead of Int

Steps to reproduce:

The resulting API.swift that was generated using apollo-ios version 0.4 and apollo-codegen version 0.9 contains: public let centAmount: String (MoneyDetails struct property). The actual type from the schema:

{
"name": "centAmount",
"type": {
  "kind": "NON_NULL",
  "name": null,
  "ofType": {
    "kind": "SCALAR",
    "name": "Long",
    "ofType": null
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 27 (14 by maintainers)

Commits related to this issue

Most upvoted comments

@NiltiakSivad I dug a little deeper, and I found that Apollo CLI now expects the parameter this way: --passthroughCustomScalars.

You can check out Apollo CLI’s other parameters here: https://github.com/apollographql/apollo-cli#apollo-codegengenerate-output

Hope this helps you 😃

thanks @martijnwalraven! judging by the nature of centAmount field, I think the value can be pretty high in some cases the current workaround could be manually altering the local schema file; is there anything better you’d suggest?

I hope to add better support for custom scalars soon, but in the meantime there is a temporary apollo-codegen option --passthrough-custom-scalars that doesn’t attempt to map custom scalars to string but keeps them as is. That means you’re responsible for defining the type and making sure it is JSONDecodable and JSONEncodable.

I haven’t tested it, but something like this should work:

typealias Long = Int64

extension Int64: JSONDecodable, JSONEncodable {
  public init(jsonValue value: JSONValue) throws {
    guard let string = value as? String else {
      throw JSONDecodingError.couldNotConvert(value: value, to: String.self)
    }
    guard let number = Int64(string) else {
      throw JSONDecodingError.couldNotConvert(value: value, to: Int64.self)
    }

    self = number
  }

  public var jsonValue: JSONValue {
    return String(self)
  }
}