Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/graphile-contrib/constraint-error-tags
A PostGraphile plugin for writing nicer error messages on constraints.
https://github.com/graphile-contrib/constraint-error-tags
constraints error graphql plugin postgraphile smart-tags
Last synced: 9 days ago
JSON representation
A PostGraphile plugin for writing nicer error messages on constraints.
- Host: GitHub
- URL: https://github.com/graphile-contrib/constraint-error-tags
- Owner: graphile-contrib
- License: mit
- Created: 2020-02-14T12:30:58.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-17T10:59:01.000Z (about 2 months ago)
- Last Synced: 2024-09-17T13:41:56.602Z (about 2 months ago)
- Topics: constraints, error, graphql, plugin, postgraphile, smart-tags
- Language: JavaScript
- Homepage:
- Size: 142 KB
- Stars: 12
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @graphile-contrib/constraint-error-tags
A PostGraphile plugin for writing nicer error messages on constraints. Adding the `@error ` smart tag to constraints will replace generic Postgres constraint violation message.
## Installation:
```bash
npm install --save @graphile-contrib/constraint-error-tags
```or
```bash
yarn add @graphile-contrib/constraint-error-tags
```## Usage:
**Library:**
```js
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),
})
);
```- `ConstraintErrorTagsPlugin` introspects all your constraints and extracts the ones with the `@error ` smart tag.
- `handleErrors` violating a constraint with the above mentioned signature will replace the generic error with the `` from the `@error` smart tag.## Example table constraint:
SQL schema:
```sql
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:
```graphql
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:
```sql
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:
```graphql
mutation {
setPassword(input: {userId: "100", newPass: "weakpassword"}) {
clientMutationId
}
}
```Response:
GraphQL error with the message: `password must contain at least one symbol of !@#$%^&*()`