How to use scalar type in iOS?

query AppTable(
$sortBy: SortInfo
)
{
ProductRequests( sortBy: $sortBy) {
collection {
id
createdAt
item {
id
description
productKey
}
}
}
}

In the above query ‘productKey’ as scalar type, when we execute above query getting below conversion error.

Apollo.GraphQLResultError(path: ProductRequests.collection.0.item.productKey, underlying: Apollo.JSONDecodingError.couldNotConvert(value: { group = POL; item = “97664-70C”; prefix = LR; }, to: Swift.String)))

if we removed the ‘productKey’ getting success response.

Can you please help on this?

Thanks

Hi @Ravikanth :wave: - are you able to share relevant parts of the schema related to this query so we can better help you?

Hi @calvincestari , Can we get any sample project/link on using scalar types in iOS?

The full tutorial is available here and if you’re needing to use a custom scalar type you should read this.

@calvincestari We have a custom scalar type called JSON, which is used when we want to send unstructured data.
When trying to access that field using Apollo (even just trying to print it), we get the following error:

Apollo.GraphQLResultError(path: dealerRequests.collection.0.item.businessKey, underlying: Apollo.JSONDecodingError.couldNotConvert(value: {
group = POL;
item = “32-43-1839TT40”;
prefix = MB;
warehouse = RAD;
}, to: Swift.String))
)

I’m not sure why it would have a problem converting to a String. Also, how can we get the Dictionary value rather than a String?

Below are the query and responses.

Query

query ProductTable(
$pageInfo: PageInfo!)
{
productRequests(pageInfo: $pageInfo) {
collection {
id
item {
id
businessKey
}
}
}
}

ApiResponse :

{
“data”: {
“productRequests": {
“collection”: [
{
“id”: “0”,
“item”: {
“businessKey”: {
“group”: “POL”,
“warehouse”: “RAD”,
“prefix”: “MB”,
“item”: “32-43-1839TT40”
}
}
}
]
}
}
}

Pease find the attached schema file for the reference.

Thanks in advance

@calvincestari We found the similar issue ticket(JSON Scalar throws JSONDecondingError.couldNotConvert · Issue #36 · apollographql/apollo-ios · GitHub) in GitHub.

I have followed steps in the attached link but facing below issues.

  1. I have updated my run script (build phase) as below but getting
    Cannot find ‘ItemBusinessKey’ in scope compilation error from API.Swift file.

cd “${SRCROOT}/${TARGET_NAME}”
“${SCRIPT_PATH}”/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile=“schema.json” --passthroughCustomScalars API.swift

2.Please let me know if below code snippet is the answer for question? if yes, where do we need to add this snippet and how to access it
.
public typealias businessKey = [String : Any?]

extension Dictionary: JSONDecodable {
/// Custom init extension so Apollo can decode custom scalar type CurrentMissionChallenge
public init(jsonValue value: JSONValue) throws {
guard let dictionary = value as? Dictionary else {
throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
}
self = dictionary
}
}

Please let me know if anything missed.

Thanks

A String is the underlying type of a custom scalar, the problem here is not that it can’t convert it to a String but that the custom scalar is not correctly configured. In order to get to the Dictionary/Object it has to be deserialized first and that is where things are failing.

There’s nothing in the data for businessKey that appears to require a custom scalar, I suspect that’s throwing us off here. I’m also confused about the type of businessKey being defined as String when it’s really another dictionary object in the JSON - signified by the {. Which IMO should be another type in your GraphQL schema.

Hi @calvincestari, when it comes to the businessKey, it is of type ItemBusinessKey, that is a complex scalar, it consists of fields:
“group”,
“wh”,
“prefix”,
“item”,
that are used to properly map the data. We’re using it successfully in Java code (by extending GraphQLScalarDefinition and implementing necessary methods), so the question is - what should we do in Swift code to ItemBusinessKey be properly recognized as a dictionary? Maybe we’re missing something from the documentation?

OK, that makes more sense now why you get the compile error “Cannot find ‘ItemBusinessKey’ in scope”. When you use the -passthroughCustomScalar option you still need to define the type in your own code. So the compile error is telling you that the type hasn’t been defined yet.

Have you looked at the playground page for custom scalars, it demonstrates a working custom scalar.

thanks @calvincestari