PostgreSQL

image

This handler allows you to use the GraphQL schema created by PostGraphile, based on a PostgreSQL database schema.

How to use?

To get started, install the handler library:

npm i @omnigraph/postgraphile

Now, you can use it directly in your Mesh config file:

mesh.config.ts
import { defineConfig } from '@graphql-mesh/compose-cli'
import { loadPostgraphileSubgraph } from '@omnigraph/postgraphile'
 
export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadPostgraphileSubgraph('MyPostgreSQL', {
        connectionString: 'postgres://postgres:password@localhost/postgres'
        // You can also use environment variables like below
        // connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@localhost/postgres`
      })
    }
  ]
})

Configuration

ParameterTypeDescription
connectionStringstringA connection string to your Postgres database
schemasstring[]PostgreSQL schemas PostGraphile will use (default: ['public'])
appendPluginsGraphileConfig.Plugin[]Extra PostGraphile v5 plugins to append
skipPluginsstring[]PostGraphile v5 plugin names to disable (e.g. "NodePlugin")
optionsGraphileConfig.PresetA PostGraphile v5 preset object merged on top of the default PostGraphileAmberPreset

Gateway

When using the composed schema with @graphql-hive/gateway, install the transport package:

npm i @graphql-mesh/transport-postgraphile

The transport is automatically detected from the composed schema and used by the gateway.

Many-to-Many support

Suppose you want to have automatic many-to-many mapping across your entities. You can install @graphile-contrib/pg-many-to-many and add it under appendPlugins.

npm i @graphile-contrib/pg-many-to-many
mesh.config.ts
// @ts-ignore - not typed
import PgManyToManyPlugin from '@graphile-contrib/pg-many-to-many'
import { defineConfig } from '@graphql-mesh/compose-cli'
import { loadPostgraphileSubgraph } from '@omnigraph/postgraphile'
 
export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadPostgraphileSubgraph('MyPostgreSQL', {
        connectionString: 'postgres://postgres:password@localhost/postgres',
        appendPlugins: [PgManyToManyPlugin]
      })
    }
  ]
})

PostGIS Support

If you use PostGIS in your PostgreSQL database, you need to install @graphile/postgis package and add it under appendPlugins.

npm i @graphile/postgis