Skip to content

Instantly share code, notes, and snippets.

@brambow
Created December 17, 2017 02:31
Show Gist options
  • Save brambow/60a4eb916cda18b80e297408eb88030f to your computer and use it in GitHub Desktop.
Save brambow/60a4eb916cda18b80e297408eb88030f to your computer and use it in GitHub Desktop.
module.exports = function PostGisPlugin(builder) {
builder.hook(
'GraphQLObjectType:fields',
(
fields, // Input object
{ extend,
pgGetGqlTypeByTypeId,
pgIntrospectionResultsByKind: introspectionResultsByKind,
pgSql: sql,
pg2gql,
graphql: { GraphQLString },
getAliasFromResolveInfo,
}, // Build
{ scope: { isRootQuery } }
) => {
if (!isRootQuery) {
return fields;
}
addDataGenerator(parsedResolveInfoFragment => {
const { alias } = parsedResolveInfoFragment;
return {
pgQuery: queryBuilder => {
queryBuilder.select(
sql.fragment`ST_AsGeoJSON(${sql.identifier(queryBuilder.getTableAlias(), attr.name)})`,
alias
);
},
};
});
return extend(fields, {
geojson: {
type: GraphQLString,
resolve(data, _args, _context, resolveInfo) {
const alias = getAliasFromResolveInfo(resolveInfo);
return data[alias];
}
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment