PostGIS support for PostGraphile v5.
The original @graphile/postgis plugin was built for PostGraphile v4 and is not compatible with PostGraphile v5. This project is a complete rewrite to leverage the new architecture and features of PostGraphile v5.
Note: This plugin is not compatible with PostGraphile v4. If you are using PostGraphile v4, please refer to the original @graphile/postgis plugin.
This project is currently under active development.
npm install @xuhaojun/graphile-postgis
# or
yarn add @xuhaojun/graphile-postgis
# or
pnpm add @xuhaojun/graphile-postgis- PostgreSQL with PostGIS extension (2.5+)
- PostGraphile v5.0.0-rc or later
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { makePgService } from "postgraphile/adaptors/pg";
import { postgisPlugin } from "@xuhaojun/graphile-postgis";
export default {
extends: [PostGraphileAmberPreset, postgisPlugin],
pgServices: [
makePgService({
connectionString: process.env.DATABASE_URL,
schemas: ["public"],
}),
],
};import * as adaptor from "postgraphile/@dataplan/pg/adaptors/pg";
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { makeSchema } from "postgraphile";
import { postgisPlugin } from "@xuhaojun/graphile-postgis";
import * as pg from "pg";
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
});
const preset = {
extends: [PostGraphileAmberPreset, postgisPlugin],
pgServices: [
adaptor.makePgService({
name: "main",
withPgClientKey: "withPgClient",
pgSettingsKey: "pgSettings",
schemas: ["public"],
pool: pool,
}),
],
};
const { schema } = await makeSchema(preset);If you're migrating from PostGraphile v4 or need v4-compatible behavior, you can add makeV4Preset:
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { makeV4Preset } from "postgraphile/presets/v4";
import { makePgService } from "postgraphile/adaptors/pg";
import { postgisPlugin } from "@xuhaojun/graphile-postgis";
export default {
extends: [
PostGraphileAmberPreset,
postgisPlugin,
makeV4Preset({
simpleCollections: "both",
// ... other v4 options
}),
],
pgServices: [
makePgService({
connectionString: process.env.DATABASE_URL,
schemas: ["public"],
}),
],
};query {
allTestGeometries {
nodes {
id
geomPoint {
# GeoJSON format
geojson {
type
coordinates
}
srid
# Direct coordinate access for Point types
x
y
}
}
}
}query {
allTestGeometries {
nodes {
id
geomLinestring {
geojson {
type
coordinates
}
srid
# Type-specific fields for LineString
points {
x
y
}
}
}
}
}mutation {
createTestMutation(
input: {
testMutation: {
name: "San Francisco"
location: { type: "Point", coordinates: [-122.4194, 37.7749] }
}
}
) {
testMutation {
id
name
location {
geojson {
type
coordinates
}
srid
x
y
}
}
}
}mutation {
updateTestMutationById(
input: {
id: 1
testMutationPatch: {
location: { type: "Point", coordinates: [-122.4194, 37.7749] }
}
}
) {
testMutation {
id
location {
geojson {
type
coordinates
}
x
y
}
}
}
}- ✅ Query PostGIS geometry/geography columns via GraphQL (returns GeoJSON)
- ✅ Mutate PostGIS data using GeoJSON input format (RFC 7946)
- ✅ Support for all PostGIS geometry types:
- Point, LineString, Polygon
- MultiPoint, MultiLineString, MultiPolygon
- GeometryCollection
- ✅ Direct coordinate access (x, y, z, srid fields for Point types)
- ✅ Type-specific fields:
pointsfor LineString and MultiPointexteriorandinteriorsfor PolygonlineStringsfor MultiLineStringpolygonsfor MultiPolygongeometriesfor GeometryCollection
- ✅ Automatic SRID handling and transformation
- ✅ Support for XY, XYZ, XYM, and XYZM coordinate dimensions
- ✅ Comprehensive GeoJSON validation with detailed error messages
- ✅ Large geometry warnings for performance monitoring
- ✅ Null geometry handling
If you see warnings about PostGIS not being detected:
-
Verify PostGIS is installed:
SELECT PostGIS_version(); -
Check extension is enabled:
SELECT * FROM pg_extension WHERE extname = 'postgis';
-
Enable PostGIS if needed:
CREATE EXTENSION IF NOT EXISTS postgis;
If mutations fail with GeoJSON validation errors:
- Ensure coordinates match the expected format (array of numbers)
- Check that the
typefield matches the column's geometry type constraint - Verify coordinates are valid numbers (not strings)
If GraphQL types are not found:
- Ensure the plugin is included in your PostGraphile configuration
- Check that PostGIS extension is detected
- Verify geometry columns use
geometryorgeographytypes (notgeometry(Point, 4326)syntax issues)
See CONTRIBUTING.md for development setup and guidelines.
MIT