Passing Dictionary or Array to Custom Scalars - Swift iOS

Hi, I am currently using the enum approach in parsing Custom Scalar from the server.
Its working well. But now I need to submit/pass the same custom scalar to the server, the custom scalar can be a Dictionary or an Array.

Do I have to edit the API.swift to process the enum before sending it to the server?

enum CustomJSON {
case dictionary([String: Any])
case array([Any])

extension CustomJSON: JSONDecodable {
init(jsonValue value: JSONValue) throws {
if let dict = value as? [String: Any] {
self = .dictionary(dict)
} else if let array = value as? [Any] {
self = .array(array)
} else {
throw JSONDecodingError.couldNotConvert(value: value, to: CustomJSON.self)

It looks like you’ve already found our docs on working with custom scalars, which I think is where this code comes from.

We definitely do not recommend adding it to API.swift since that file is constantly overwritten by the code generation process. We recommend adding it in a separate file that is manually created and maintained.

We also recommend making sure the CustomJSON typename lines up with the typename of your custom scalar (meaning, if the custom scalar is named JSON, you should make sure your enum and extension are named JSON rather than CustomJSON).

Hope that helps!

1 Like