0
votes

I'm trying to use the graphql-geojson library in an Apollo/Express app to serve GeoJSON responses.

The library provides the resolvers but I haven't found a way to add the types to my schema definition without writing them out by hand. And even then that hasn't proven that simple.

Another custom type, Date from graphql-date, I can just declare in the schema as a custom scalar:

const typeDefs: DocumentNode = gql`
  scalar Date
  scalar PointObject

  type Station {
    id: ID!
    name: String!
    geom: PointObject
    created: Date!
  }
  ...
}

However, declaring PointObject as scalar allows you to get the full GeoJSON (which is OK) but results in errors:

"GraphQLError: Field \"geom\" must not have a selection since type \"GeoJSONPoint\" has no subfields."

when you try to return a sub-selection of geom, e.g.:

{
  stations {
    id
    name
    geom {
      coordinates
    }
  }
}

Is there a way merge the types provided by the library with my schema?

1

1 Answers

1
votes

Apollo's graphql-tools library includes a mergeSchemas function which can combine multiple schemas and bits of schema together. You should be able to use this to build one big schema out of your type definitions (untested):

import * as GeoJSON from 'graphql-geojson';
import { mergeSchemas } from 'graphql-tools';

const typeDefs = `
  type Station {
    id: ID!
    name: String!
    geom: PointObject
    created: Date!
  }
`;

const schema = mergeSchemas({
  schemas: [Object.values(GeoJSON), typeDefs]
});

(Note that the types graphql-geojson defines are kind of involved; they are definitely not a single scalar type, and I wouldn't want to try to retype them in my schema definition if it could be avoided.)