Apollo GraphQL swift Spatial Data

Hi, all we are facing issue while reading the spatial data in Apollo GraphQL swift
swift version 5
Apollo version 0.49.0
Is Net Topology Suite Spatial data supported in Apollo swift.
how can we read the position and area from spatial data
below is the screenshot for reference.

I don’t understand what you mean by this? Apollo iOS will generate code for any type in your operation. Are you correctly using Position as a scalar type?

We have documentation and a Swift Playground that demonstrates how custom scalars work.

I have used below query for getting the coordinates of position -

query CustomerInfo($foreignId : Int) {
cusCustomer(where: { id: { eq: $foreignId } }) {
nodes {
id
mainCustomerName
mainCustomerCode
mainCustomerAddress {
address1
address2
state
city
zipCode
phone
}
code
name
address {
address1
address2
state
city
zipCode
phone
spatialData {
position {
coordinates
}
}
}
}
}
}

The automatic generated API file is not understanding the Spatial data of “position”. I have used this extension for resolving , but it is not solving my purpose.
Following is the extension:-

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

extension Position: 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: Position.self)
}
}
}

Below Apollo Graphql Script I have used :-

Go to the build root and search up the chain to find the Derived Data Path where the source packages are checked out.

DERIVED_DATA_CANDIDATE=“${BUILD_ROOT}”

while ! [ -d “${DERIVED_DATA_CANDIDATE}/SourcePackages” ]; do
if [ “${DERIVED_DATA_CANDIDATE}” = / ]; then
echo >&2 “error: Unable to locate SourcePackages directory from BUILD_ROOT: ‘${BUILD_ROOT}’”
exit 1
fi

DERIVED_DATA_CANDIDATE=“$(dirname “${DERIVED_DATA_CANDIDATE}”)”
done

Grab a reference to the directory where scripts are checked out

SCRIPT_PATH=“${DERIVED_DATA_CANDIDATE}/SourcePackages/checkouts/apollo-ios/scripts”

if [ -z “${SCRIPT_PATH}” ]; then
echo >&2 “error: Couldn’t find the CLI script in your checked out SPM packages; make sure to add the framework to your project.”
exit 1
fi

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

Still facing this issue.

Hi @calvincestari ,

we are facing the above issue as mentioned.
can u please help.

Do you possibly have conflicting types named Position? If I change your code to the following the error is resolved for me:

GraphQLField("coordinates", type: .scalar(Position2.self)),
enum Position2 {
  case dictionary([String: Any])
  case array([Any])
}

extension Position2: 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: Position.self)
    }
  }
}

If it’s the actual GraphQLSelectionSet that you want to be JSONDecodable then your extension must be on the fully qualified type name:

extension CustomerInfoQuery.Data.CusCustomer.Node.Address.SpatialDatum.Position: JSONDecodable {
  public init(jsonValue value: JSONValue) throws {
  }
}

Hi, @calvincestari Thanks for the help.
I have resolved my issue, as you have suggested but when i run the query in response it gives me failure condition

Failure! Error: GraphQLResultError(path: cusCustomer.nodes.0.address.spatialData.position.coordinates, underlying: Apollo.JSONDecodingError.couldNotConvert(value: <__NSArrayI 0x600003d49e20>(

-98.49941699999999,

29.768573

)

, to: ODT_Mobile.CustomerInfoQuery.Data.CusCustomer.Node.Address.SpatialDatum.Position))

I have written the following extension as you have suggested:-

extension CustomerInfoQuery.Data.CusCustomer.Node.Address.SpatialDatum.Position: JSONDecodable {
public init(jsonValue value: JSONValue) throws {
guard let dictionary = value as? CustomerInfoQuery.Data.CusCustomer.Node.Address.SpatialDatum.Position else {
throw JSONDecodingError.couldNotConvert(value: value, to: CustomerInfoQuery.Data.CusCustomer.Node.Address.SpatialDatum.Position.self)
}
self = dictionary
}
}

@Priya_Soni my suggestion was for you to decide what structure you need and resolve the naming conflict. That error message looks like the failure is because the data is an Array but CustomerInfoQuery.Data.CusCustomer.Node.Address.SpatialDatum.Position is a different struct that won’t initialize from just an array. Try the Position2 suggestion above.