npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@xuhaojun/graphile-postgis

v0.1.0

Published

PostGIS support for PostGraphile v5 - Query and mutate spatial data using GeoJSON

Downloads

117

Readme

@xuhaojun/graphile-postgis

PostGIS support for PostGraphile v5.

Overview

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.

Status

This project is currently under active development.

Installation

npm install @xuhaojun/graphile-postgis
# or
yarn add @xuhaojun/graphile-postgis
# or
pnpm add @xuhaojun/graphile-postgis

Prerequisites

  • PostgreSQL with PostGIS extension (2.5+)
  • PostGraphile v5.0.0-rc or later

Usage

Basic Setup with graphile.config.mjs

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"],
    }),
  ],
};

Using with makeSchema

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);

Using with V4 Compatibility (Optional)

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"],
    }),
  ],
};

Example GraphQL Query

query {
  allTestGeometries {
    nodes {
      id
      geomPoint {
        # GeoJSON format
        geojson {
          type
          coordinates
        }
        srid

        # Direct coordinate access for Point types
        x
        y
      }
    }
  }
}

Example GraphQL Query with LineString

query {
  allTestGeometries {
    nodes {
      id
      geomLinestring {
        geojson {
          type
          coordinates
        }
        srid
        # Type-specific fields for LineString
        points {
          x
          y
        }
      }
    }
  }
}

Example GraphQL Mutation (Create)

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
      }
    }
  }
}

Example GraphQL Mutation (Update)

mutation {
  updateTestMutationById(
    input: {
      id: 1
      testMutationPatch: {
        location: { type: "Point", coordinates: [-122.4194, 37.7749] }
      }
    }
  ) {
    testMutation {
      id
      location {
        geojson {
          type
          coordinates
        }
        x
        y
      }
    }
  }
}

Features

  • ✅ 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:
    • points for LineString and MultiPoint
    • exterior and interiors for Polygon
    • lineStrings for MultiLineString
    • polygons for MultiPolygon
    • geometries for 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

Troubleshooting

PostGIS Extension Not Detected

If you see warnings about PostGIS not being detected:

  1. Verify PostGIS is installed:

    SELECT PostGIS_version();
  2. Check extension is enabled:

    SELECT * FROM pg_extension WHERE extname = 'postgis';
  3. Enable PostGIS if needed:

    CREATE EXTENSION IF NOT EXISTS postgis;

Invalid GeoJSON Errors

If mutations fail with GeoJSON validation errors:

  • Ensure coordinates match the expected format (array of numbers)
  • Check that the type field matches the column's geometry type constraint
  • Verify coordinates are valid numbers (not strings)

Type Not Found Errors

If GraphQL types are not found:

  1. Ensure the plugin is included in your PostGraphile configuration
  2. Check that PostGIS extension is detected
  3. Verify geometry columns use geometry or geography types (not geometry(Point, 4326) syntax issues)

Development

See CONTRIBUTING.md for development setup and guidelines.

License

MIT