Package Exports
- @graphile-contrib/constraint-error-tags
- @graphile-contrib/constraint-error-tags/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@graphile-contrib/constraint-error-tags) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@graphile-contrib/constraint-error-tags
A PostGraphile plugin for writing nicer error messages on constraints. Adding the @error <message> smart tag to constraints will replace generic Postgres constraint violation message.
Installation:
npm install --save @graphile-contrib/constraint-error-tagsor
yarn add @graphile-contrib/constraint-error-tagsUsage:
Library:
const {
ConstraintErrorTagsPlugin,
handleErrors,
} = require("@graphile-contrib/constraint-error-tags");
app.use(
postgraphile(process.env.POSTGRES_ENDPOINT, process.env.POSTGRES_SCHEMA, {
appendPlugins: [ConstraintErrorTagsPlugin],
handleErrors: (errors) => handleErrors(errors),
})
);ConstraintErrorTagsPluginintrospects all your constraints and extracts the ones with the@error <message>smart tag.handleErrorsviolating a constraint with the above mentioned signature will replace the generic error with the<message>from the@errorsmart tag.
Example table constraint:
SQL schema:
create table public.user (
id serial primary key,
first_name text not null,
last_name text not null,
age int not null,
constraint age_check check(age >= 18)
);
comment on constraint "age_check" on "public.user" is '@error The user has to be at least 18 years of age.';GraphQL mutation:
mutation {
createUser(input: { firstName: "John", lastName: "Doe", age: 16 }) {
clientMutationId
}
}Response:
GraphQL error with the message: The user has to be at least 18 years of age.
Example type constraint:
SQL schema:
create domain strong_password as text
constraint "symbol" check (value ~ '[!@#$%^&*()]');
comment on constraint "symbol" on domain "strong_password" is '@error password must contain at least one symbol of !@#$%^&*()';GraphQL mutation:
mutation {
setPassword(input: {userId: "100", newPass: "weakpassword"}) {
clientMutationId
}
}Response:
GraphQL error with the message: password must contain at least one symbol of !@#$%^&*()