I'm generating error messages for the front end like this:
create or replace function app_public.register_person(
handle text,
display_name text,
email text,
password text
) returns app_public.person as $$
declare
person app_public.person;
constraint_name text;
begin
insert into app_public.person (handle, display_name) values
(handle, display_name)
returning * into person;
insert into app_private.person_account (person_id, email, password_hash) values
(person.id, email, crypt(password, gen_salt('bf')));
return person;
exception
when check_violation then
get stacked diagnostics constraint_name = CONSTRAINT_NAME;
if constraint_name = 'handle_length' then
raise exception 'Your handle is too long';
end if;
when unique_violation then
get stacked diagnostics constraint_name = CONSTRAINT_NAME;
if constraint_name = 'person_account_email_key' then
raise exception 'That email is already in use';
elsif constraint_name = 'person_handle_key' then
raise exception 'That handle is already in use';
else
raise exception '%', constraint_name;
end if;
end;
$$ language plpgsql strict security definer;
It works, but I'm curious as to whether this is the "right" approach or not.
graphile-utils
to make plugin factories to make doing things a lot easier for people. For example, there really should be an inflector
plugin factory that enables you to override the inflector. Anyway I'm getting ahead of myself.
nodes: [Move]!
node item not marked as not null? [Move!]!
mutation {
updateUserByUserId(input: {userId: "de5f6a1e-58ad-4c54-ab51-55fbca17ba55", userPatch: {address: ""}}) {
user {
userId
name
address
}
}
}
Return {
"errors": [
{
"message": "cannot extract elements from a scalar",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"updateUserByUserId"
]
}
],
"data": {
"updateUserByUserId": null
}
}
totalCount
; the others can be added via plugins but we don't have any official plugins for that yet. I'm planning on adding them at some point in a similar way to computed columns, but using aggregate functions instead. You can also write custom queries or computed columns that do this; for example I just wrote this one:create function XXX_public."items_checklistItemCompletedCount"(i XXX_public.items) returns int as $$
select coalesce(
(select count(*)::int from XXX_public.items where "rootPostId" = i.id AND type = 'CHECKLIST_ITEM' and completed is true),
0
) where i.type = 'POST';
$$ language sql stable;
checklistItemCompletedCount
to the Item
type which does a count. You could replace the count with any aggregate function you like.