Skip to content

Instantly share code, notes, and snippets.

@lkleuver
Created October 25, 2018 16:05
Show Gist options
  • Save lkleuver/b7f995cff0c0e153544d5203ad4be665 to your computer and use it in GitHub Desktop.
Save lkleuver/b7f995cff0c0e153544d5203ad4be665 to your computer and use it in GitHub Desktop.
import { GraphQLScalarType } from "graphql";
import { Kind } from "graphql/language";
const typeDefs = /* GraphQL */ `
scalar Coordinates
scalar ObjectType
interface Geometry {
type: String!
coordinates: Coordinates
}
interface GeoJSON {
type: String!
}
type Point implements GeoJSON & Geometry {
type: String!
coordinates: Coordinates
}
type MultiPoint implements GeoJSON & Geometry {
type: String!
coordinates: Coordinates
}
type LineString implements GeoJSON & Geometry {
type: String!
coordinates: Coordinates
}
type MultiLineString implements GeoJSON & Geometry {
type: String!
coordinates: Coordinates
}
type Polygon implements GeoJSON & Geometry {
type: String!
coordinates: Coordinates
}
type MultiPolygon implements GeoJSON & Geometry {
type: String!
coordinates: Coordinates
}
type GeometryCollection implements GeoJSON {
type: String!
geometries: [Geometry]!
}
type Feature implements GeoJSON {
id: String
type: String!
geometry: Geometry
properties: ObjectType
}
type FeatureCollection implements GeoJSON {
type: String!
features: [Feature]
}
input GeometryInput {
type: String!
coordinates: Coordinates!
}
input FeatureInput {
id: String
type: String!
geometry: GeometryInput
properties: ObjectType
}
input FeatureCollectionInput {
type: String!
features: [FeatureInput]
}
`;
const resolvers = {
Coordinates: new GraphQLScalarType({
name: "Coordinates",
description: "A set of coordinates. x, y",
parseValue(value) {
return value;
},
serialize(value) {
return value;
},
parseLiteral(ast) {
return ast.value;
}
}),
ObjectType: new GraphQLScalarType({
name: "ObjectType",
description: "Arbitrary JSON value",
serialize: value => value,
parseValue: value => value,
parseLiteral: ast => {
if (ast.kind !== Kind.OBJECT) {
throw new GraphQLError(
`Query error: Can only parse object but got a: ${ast.kind}`,
[ast]
);
}
return ast.value;
}
}),
GeoJSON: {
__resolveType: (obj, context, info) => obj.type
},
Geometry: {
__resolveType: (obj, context, info) => obj.type
}
};
export default {
typeDefs,
resolvers
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment