Functions
Using Postgres Functions with GraphQL.
Functions can be exposed by pg_graphql to allow running custom queries or mutations.
Query vs Mutation
For example, a function to add two numbers will be available on the query type as a field:
1create function "addNums"(a int, b int)2 returns int3 immutable4 language sql5as $$ select a + b; $$;Functions marked immutable or stable are available on the query type. Functions marked with the default volatile category are available on the mutation type:
1create table account(2 id serial primary key,3 email varchar(255) not null4);56create function "addAccount"(email text)7 returns int8 volatile9 language sql10as $$ insert into account (email) values (email) returning id; $$;Supported Return Types
Built-in GraphQL scalar types Int, Float, String, Boolean and custom scalar types are supported as function arguments and return types. Function types returning a table or view are supported as well. Such functions implement the Node interface:
1create table account(2 id serial primary key,3 email varchar(255) not null4);56insert into account(email)7values8 ('a@example.com'),9 ('b@example.com');1011create function "accountById"("accountId" int)12 returns account13 stable14 language sql15as $$ select id, email from account where id = "accountId"; $$;Since Postgres considers a row/composite type containing only null values to be null, the result can be a little surprising in this case. Instead of an object with all columns null, the top-level field is null:
1create table account(2 id int,3 email varchar(255),4 name text null5);67insert into account(id, email, name)8values9 (1, 'aardvark@x.com', 'aardvark'),10 (2, 'bat@x.com', null),11 (null, null, null);1213create function "returnsAccountWithAllNullColumns"()14 returns account language sql stable15as $$ select id, email, name from account where id is null; $$;Functions returning multiple rows of a table or view are exposed as collections.
1create table "Account"(2 id serial primary key,3 email varchar(255) not null4);56insert into "Account"(email)7values8 ('a@example.com'),9 ('a@example.com'),10 ('b@example.com');1112create function "accountsByEmail"("emailToSearch" text)13 returns setof "Account"14 stable15 language sql16as $$ select id, email from "Account" where email = "emailToSearch"; $$;A set returning function with any of its argument names clashing with argument names of a collection (first, last, before, after, filter, or orderBy) will not be exposed.
Functions accepting or returning arrays of non-composite types are also supported. In the following example, the ids array is used to filter rows from the Account table:
1create table "Account"(2 id serial primary key,3 email varchar(255) not null4);56insert into "Account"(email)7values8 ('a@example.com'),9 ('b@example.com'),10 ('c@example.com');1112create function "accountsByIds"("ids" int[])13 returns setof "Account"14 stable15 language sql16as $$ select id, email from "Account" where id = any(ids); $$;Default Arguments
Arguments without a default value are required in the GraphQL schema, to make them optional they should have a default value.
1create function "addNums"(a int default 1, b int default 2)2 returns int3 immutable4 language sql5as $$ select a + b; $$;If there is no sensible default, and you still want to make the argument optional, consider using the default value null.
1create function "addNums"(a int default null, b int default null)2 returns int3 immutable4 language plpgsql5as $$6begin78 if a is null and b is null then9 raise exception 'a and b both can''t be null';10 end if;1112 if a is null then13 return b;14 end if;1516 if b is null then17 return a;18 end if;1920 return a + b;21end;22$$;Currently, null defaults are only supported as simple expressions, as shown in the previous example.
Limitations
The following features are not yet supported. Any function using these features is not exposed in the API:
- Functions that accept a table's tuple type
- Overloaded functions
- Functions with a nameless argument
- Functions returning void
- Variadic functions
- Functions that accept or return an array of composite type
- Functions that accept or return an enum type or an array of enum type