Search code examples
iosswiftgraphqlapolloapollo-client

JSON Scalar throws JSONDecondingError.couldNotConvert in iOS apollo client


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

Apollo.GraphQLResultError(path: productRequests.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?

This may be somewhat similar to #23. I tried the proposed solution there (just aliasing JSON to [String : Any?]) but it gave a bunch of compile errors.

Below is my query and response.

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

 Response :
 {
"data": {
    "productRequests": {
        "collection ": [{
            "id": "0",
            "item": {
                "businessKey": {
                    "group": "POL",
                    "warehouse": "RAD",
                    "prefix": "MB",
                    "item": "33 - 44 - 1939 TT40"
                }
            }
        }]
    }
  }
}

How to resolve the decoding issue, can any one help on this?

Thanks in advance.


Solution

  • I found a similar issue in GitHub that includes a workaround solution here: https://github.com/apollographql/apollo-ios/issues/36#issuecomment-452515484

    This is a fairly dated thread but thought it could give you some direction on how to proceed.