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.

Hi, @calvincestari

Much appreciate for your response, sorry for delay response from our side.
we were trying to read the data as you suggested during these days but didn’t got any success till now.
I am sharing the zip file of the project that @Priya_Soni created in Apollo GraphQL Swift
Can you please have a look on it.

Below is the one drive link where we have share the project.

https://beyondkey-my.sharepoint.com/personal/priya_soni1_beyondkey_com/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fpriya_soni1_beyondkey_com%2FDocuments%2FDemoGraphQlCustomscalar%203%202.zip&parent=%2Fpersonal%2Fpriya_soni1_beyondkey_com%2FDocuments&ga=1

Here is the GraphQL API URL
Banana Cake Pop >> untitled 2 (dqt-geometry.azurewebsites.net)

GraphQL Query
query a {
location {
nodes {
id
spatialData {
position {
coordinates
}
}
}
}
}

Hi @Pramit_jain, we’re in the final rush towards GraphQL Summit and the GA release of v1.0. With all that going on I’ll only be able to look at this again towards the end of next week. Chat soon.

Hi @calvincestari
Ok thanks for your reply.

Hi, @calvincestari
if u get some time can you please check the code we shared.

Hi, @calvincestari
if u get some time can you please check the code we shared.

Hi @calvincestari

Can u please check the code below is the link to download the code
for spatial data we are unable to retrieve the spatial data

https://beyondkey-my.sharepoint.com/personal/priya_soni1_beyondkey_com/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fpriya_soni1_beyondkey_com%2FDocuments%2FDemoGraphQlCustomscalar%203%202.zip&parent=%2Fpersonal%2Fpriya_soni1_beyondkey_com%2FDocuments&ga=1

Hello @calvincestari,
I am still facing this issue
can you please look on this

Hi @calvincestari

Can you please help us in solving this issue.
we are still facing the same issue.

Regards,
Pramit Jain

@Pramit_jain, @Priya_Soni - I will try take a look at the issue this week.

@Pramit_jain, are there any special headers or variables needed in the request?

I’ve tried to make a query fetching the locations but it always times out and returns an error about it taking too long (30s). I then tried to fetch just the first 2 locations but it still timed out.

Query:

query SampleQuery($first: Int) {
  location(first: $first) {
    nodes {
      id
      spatialData {
        position {
          coordinates
        }
      }
    }
  }
}

Response:

{
  "errors": [
    {
      "message": "The request exceeded the configured timeout of `00:00:30`.",
      "extensions": {
        "code": "HC0045"
      }
    }
  ]
}

@Pramit_jain, @Priya_Soni - the requests are still timing out. I won’t be able to look into the returned data structure until I can get a response from that server.