Skip to content

Commit

Permalink
Add useValidationRule() plugin (#2215)
Browse files Browse the repository at this point in the history
* Add useValidationRule

* Add changeset

* Add a test

* Fix syntax in docs
  • Loading branch information
benjie committed May 7, 2024
1 parent 7ac1d3c commit dc1222f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-turkeys-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@envelop/core': patch
---

feat: add `useValidationRule()` plugin
1 change: 1 addition & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ https://github.com/n1ru4l/envelop
- [`useMaskedErrors`](./docs/use-masked-errors.md)
- [`usePayloadFormatter`](./docs/use-payload-formatter.md)
- [`useEngine`](./docs/use-engine.md)
- [`useValidationRule`](./docs/use-validation-rule.md)
25 changes: 25 additions & 0 deletions packages/core/docs/use-validation-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#### `useValidationRule`

This plugin is the simplest plugin for adding a validation rule to your GraphQL schema. You can
specify any function that conforms to
[the `ValidationRule` type](https://github.com/graphql/graphql-js/blob/8a95335f545024c09abfa0f07cc326f73a0e466f/src/validation/ValidationContext.ts#L269).

```ts
import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
import { envelop, useEngine, useValidationRule } from '@envelop/core'
import { depthLimit } from '@graphile/depth-limit'

const getEnveloped = envelop({
plugins: [
useEngine({ parse, validate, specifiedRules, execute, subscribe }),
useValidationRule(
depthLimit({
maxDepth: 12,
maxListDepth: 4,
maxSelfReferentialDepth: 2
})
)
// ... other plugins ...
]
})
```
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './plugins/use-extend-context.js';
export * from './plugins/use-payload-formatter.js';
export * from './plugins/use-masked-errors.js';
export * from './plugins/use-engine.js';
export * from './plugins/use-validation-rule.js';
export { getDocumentString } from './document-string-map.js';
9 changes: 9 additions & 0 deletions packages/core/src/plugins/use-validation-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Plugin } from '@envelop/types';

export const useValidationRule = (rule: any): Plugin => {
return {
onValidate({ addValidationRule }) {
addValidationRule(rule);
},
};
};
20 changes: 20 additions & 0 deletions packages/core/test/validate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GraphQLError, GraphQLSchema, validate, ValidationContext } from 'graphql';
import { assertSingleExecutionValue, createSpiedPlugin, createTestkit } from '@envelop/testing';
import { useValidationRule } from '../src/plugins/use-validation-rule.js';
import { query, schema } from './common.js';

describe('validate', () => {
Expand Down Expand Up @@ -126,6 +127,25 @@ describe('validate', () => {
expect(r.errors![0].message).toBe('Invalid!');
});

it('Should allow to add validation rules (reportError, `useValidationRule`)', async () => {
const teskit = createTestkit(
[
useValidationRule((context: any) => {
context.reportError(new GraphQLError('Invalid!'));
return {};
}),
],
schema,
);

const r = await teskit.execute(query);
assertSingleExecutionValue(r);

expect(r.errors).toBeDefined();
expect(r.errors!.length).toBe(1);
expect(r.errors![0].message).toBe('Invalid!');
});

it('Should allow to add validation rules (throw)', async () => {
const teskit = createTestkit(
[
Expand Down
11 changes: 11 additions & 0 deletions website/src/lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ export const PLUGINS: {
icon: envelopIcon,
tags: ['core', 'schema'],
},
{
identifier: 'use-validation-rule',
title: 'useValidationRule',
githubReadme: {
repo: 'n1ru4l/envelop',
path: 'packages/core/docs/use-validation-rule.md',
},
npmPackage: '@envelop/core',
icon: envelopIcon,
tags: ['core', 'utilities'],
},
{
identifier: 'use-error-handler',
title: 'useErrorHandler',
Expand Down

0 comments on commit dc1222f

Please sign in to comment.