Created
December 17, 2017 02:31
-
-
Save brambow/60a4eb916cda18b80e297408eb88030f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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