"Cannot convert value of type <Custom Scalar> to expected dictionary value type 'JSONEncodable?'" for Apollo iOS

I’m struggling with using “custom scalar” for my GraphQL Apollo iOS App.

I want to get “Timestamp” as “UInt64” so I followed solution written on Apollo Tutorial. Codeine to API.swift seems using Timestamp, which is custom scalar. But there’s error in API.swift:

enter image description here

Cannot convert value of type 'Timestamp' (aka 'UInt64') to expected dictionary value type 'JSONEncodable?'

This is part of my API.swift including query:

public final class ScheduleListQuery: GraphQLQuery {
  /// The raw GraphQL definition of this operation.
  public let operationDefinition: String =
    """
    query ScheduleList($date: Timestamp!) {
      schedules(date: $date) {
        __typename
        schedules {
          __typename
          id
          name
          status
          category {
            __typename
            color
          }
          dateTimeStart
          dateTimeEnd
          stickerCount
          stickerNames
        }
      }
    }
    """

  public let operationName: String = "ScheduleList"

  public var date: Timestamp

  public init(date: Timestamp) {
    self.date = date
  }

  public var variables: GraphQLMap? {
    return ["date": date]
  }

...

This is my typealias Timestamp:

import Foundation
import Apollo

public typealias Timestamp = UInt64

extension Timestamp: JSONDecodable {

    public init(jsonValue value: JSONValue) throws {
        
        guard let string = value as? String else {
            throw JSONDecodingError.couldNotConvert(value: value, to: String.self)
        }
        
        guard let timeInterval = UInt64(string) else {
            throw JSONDecodingError.couldNotConvert(value: value, to: Double.self)
        }

        self = timeInterval
    }
}

Hi @ChoiysApple - you’ve got the JSONDecodable portion coded up correctly which is OK if you only want to receive your custom scalar. Since you’re wanting to send the custom scalar value as an argument you need to code Timestamp to conform to the JSONEncodable protocol too.

Something like this:

extension Timestamp: JSONDecodable, JSONEncodable {
    public var jsonValue: JSONValue {

    }

    public init(jsonValue value: JSONValue) throws {

    }
}
1 Like

It works thanks! :star_struck:
I was confused because I couldn’t find comments about JsonEncodable.

1 Like