Skip to main content

Custom queries

Below, you'll find the result of running various GraphQL queries against the examples repo schema. This is intended to be an introduction and quick reference, for full information please use the documentation links.

Please be aware that these examples use the @graphile-contrib/pg-simplify-inflector plugin to simplify the field names over the defaults.

Single scalar

{
randomNumber
}

Generated by SQL like:

create function app_public.random_number() returns int
language sql stable
as $$
select 4; -- Chosen by fair dice roll. Guaranteed to be random. XKCD#221
$$;

Result:

{ "randomNumber": 4 }

Single row

{
currentUser {
nodeId
id
username
}
}

Added to the GraphQL schema via this SQL:

create function current_user()
returns app_public.users
language sql stable
as $$
select users.*
from app_public.users
where id = current_user_id();
$$;

Result:

{
"currentUser": {
"nodeId": "WyJ1c2VycyIsMV0=",
"id": 1,
"username": "user"
}
}

Rows connection

{
forumsAboutCats {
nodes {
nodeId
id
name
slug
}
}
}

Created from SQL like:

create function app_public.forums_about_cats()
returns setof app_public.forums
language sql stable
as $$
select *
from app_public.forums
where slug like 'cat-%';
$$;

Result:

{
"forumsAboutCats": {
"nodes": [
{
"nodeId": "WyJmb3J1bXMiLDNd",
"id": 3,
"name": "Cat Life",
"slug": "cat-life"
},
{
"nodeId": "WyJmb3J1bXMiLDRd",
"id": 4,
"name": "Cat Help",
"slug": "cat-help"
}
]
}
}