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#
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for "pg_graphql" and enable the extension.
Usage#
Given a table
_10create table "Blog"(_10 id serial primary key,_10 name text not null,_10 description text,_10);_10_10insert into "Blog"(name)_10values ('My Blog');
The reflected GraphQL schema can be queries immediately as
_13select_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#
graphql.resolve
: A SQL function for executing GraphQL queries.
Resources#
- Official
pg_graphql
documentation