Database

pg_graphql: GraphQL for PostgreSQL


pg_graphql is PostgreSQL extension for interacting with the database using GraphQL instead of SQL.

The extension reflects a GraphQL schema from the existing SQL schema and exposes it through a SQL function, graphql.resolve(...). This enables any programming language that can connect to PostgreSQL to query the database via GraphQL with no additional servers, processes, or libraries.

The pg_graphql resolve method is designed to interop with PostgREST, the tool that underpins the Supabase API, such that the graphql.resolve function can be called via RPC to safely and performantly expose the GraphQL API over HTTP/S.

For more information about how the SQL schema is reflected into a GraphQL schema, see the pg_graphql API docs.

Enable the extension

  1. Go to the Database page in the Dashboard.
  2. Click on Extensions in the sidebar.
  3. Search for "pg_graphql" and enable the extension.

Usage

Given a table


_10
create table "Blog"(
_10
id serial primary key,
_10
name text not null,
_10
description text
_10
);
_10
_10
insert into "Blog"(name)
_10
values ('My Blog');

The reflected GraphQL schema can be queried immediately as


_13
select
_13
graphql.resolve($$
_13
{
_13
blogCollection(first: 1) {
_13
edges {
_13
node {
_13
id,
_13
name
_13
}
_13
}
_13
}
_13
}
_13
$$);

returning the JSON


_14
{
_14
"data": {
_14
"blogCollection": {
_14
"edges": [
_14
{
_14
"node": {
_14
"id": 1
_14
"name": "My Blog"
_14
}
_14
}
_14
]
_14
}
_14
}
_14
}

Note that pg_graphql fully supports schema introspection so you can connect any GraphQL IDE or schema inspection tool to see the full set of fields and arguments available in the API.

API

Resources