Skip to content

Commit 24bf313

Browse files
committed
update docs on how to specify null defaults
1 parent 6238025 commit 24bf313

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

docs/functions.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ Functions accepting or returning arrays of non-composite types are also supporte
371371

372372
## Default Arguments
373373

374-
Functions with default arguments can have their default arguments omitted.
374+
Arguments without a default value are required in the GraphQL schema, to make them optional they should have a default value.
375375

376376
=== "Function"
377377

@@ -410,6 +410,66 @@ Functions with default arguments can have their default arguments omitted.
410410
}
411411
```
412412

413+
If there is no sensible default, and you still want to make the argument optional, consider using the default value null.
414+
415+
416+
=== "Function"
417+
418+
```sql
419+
create function "addNums"(a int default null, b int default null)
420+
returns int
421+
immutable
422+
language plpgsql
423+
as $$
424+
begin
425+
426+
if a is null and b is null then
427+
raise exception 'a and b both can''t be null';
428+
end if;
429+
430+
if a is null then
431+
return b;
432+
end if;
433+
434+
if b is null then
435+
return a;
436+
end if;
437+
438+
return a + b;
439+
end;
440+
$$;
441+
```
442+
443+
=== "QueryType"
444+
445+
```graphql
446+
type Query {
447+
addNums(a: Int, b: Int): Int
448+
}
449+
```
450+
451+
452+
=== "Query"
453+
454+
```graphql
455+
query {
456+
addNums(a: 42)
457+
}
458+
```
459+
460+
=== "Response"
461+
462+
```json
463+
{
464+
"data": {
465+
"addNums": 42
466+
}
467+
}
468+
```
469+
470+
Currently, null defaults are only supported as simple expressions, as shown in the previous example.
471+
472+
413473
## Limitations
414474

415475
The following features are not yet supported. Any function using these features is not exposed in the API:

0 commit comments

Comments
 (0)