@envelop/extended-validation 1.7.0-alpha-26c4ae2.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
## `@envelop/extended-validation`
|
|
2
|
+
|
|
3
|
+
Extended validation plugin adds support for writing GraphQL validation rules, that has access to all `execute` parameters, including variables.
|
|
4
|
+
|
|
5
|
+
While GraphQL supports fair amount of built-in validations, and validations could be extended, it's doesn't expose `variables` to the validation rules, since operation variables are not available during `validate` flow (it's only available through execution of the operation, after input/variables coercion is done).
|
|
6
|
+
|
|
7
|
+
This plugin runs before `validate` but allow developers to write their validation rules in the same way GraphQL `ValidationRule` is defined (based on a GraphQL visitor).
|
|
8
|
+
|
|
9
|
+
## Getting Started
|
|
10
|
+
|
|
11
|
+
Start by installing the plugin:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
yarn add @envelop/extended-validation
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Then, use the plugin with your validation rules:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { useExtendedValidation } from '@envelop/extended-validation';
|
|
21
|
+
|
|
22
|
+
const getEnveloped = evelop({
|
|
23
|
+
plugins: [
|
|
24
|
+
useExtendedValidation({
|
|
25
|
+
rules: [ ... ] // your rules here
|
|
26
|
+
})
|
|
27
|
+
]
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
To create your custom rules, implement the `ExtendedValidationRule` interface and return your GraphQL AST visitor.
|
|
32
|
+
|
|
33
|
+
For example:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { ExtendedValidationRule } from '@envelop/extended-validation';
|
|
37
|
+
|
|
38
|
+
export const MyRule: ExtendedValidationRule = (validationContext, executionArgs) => {
|
|
39
|
+
return {
|
|
40
|
+
OperationDefinition: node => {
|
|
41
|
+
// This will run for every executed Query/Mutation/Subscription
|
|
42
|
+
// And now you also have access to the execution params like variables, context and so on.
|
|
43
|
+
// If you wish to report an error, use validationContext.reportError or throw an exception.
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Built-in Rules
|
|
50
|
+
|
|
51
|
+
### Union Inputs: `@oneOf`
|
|
52
|
+
|
|
53
|
+
This directive provides validation for input types and implements the concept of union inputs. You can find the [complete spec RFC here](https://github.com/graphql/graphql-spec/pull/825).
|
|
54
|
+
|
|
55
|
+
You can use union inputs either via a the SDL flow, by annotating types and fields with `@oneOf` or via the `extensions` field.
|
|
56
|
+
|
|
57
|
+
First, make sure to add that rule to your plugin usage:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { useExtendedValidation, OneOfInputObjectsRule } from '@envelop/extended-validation';
|
|
61
|
+
|
|
62
|
+
const getEnveloped = evelop({
|
|
63
|
+
plugins: [
|
|
64
|
+
useExtendedValidation({
|
|
65
|
+
rules: [OneOfInputObjectsRule],
|
|
66
|
+
}),
|
|
67
|
+
],
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### Schema Directive Flow
|
|
72
|
+
|
|
73
|
+
Make sure to include the following directive in your schema:
|
|
74
|
+
|
|
75
|
+
```graphql
|
|
76
|
+
directive @oneOf on INPUT_OBJECT | FIELD_DEFINITION
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Then, apply it to field definitions, or to a complete `input` type:
|
|
80
|
+
|
|
81
|
+
```graphql
|
|
82
|
+
## Apply to entire input type
|
|
83
|
+
input FindUserInput @oneOf {
|
|
84
|
+
id: ID
|
|
85
|
+
organizationAndRegistrationNumber: GraphQLInt
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
## Or, apply to a set of input arguments
|
|
89
|
+
|
|
90
|
+
type Query {
|
|
91
|
+
foo(id: ID, str1: String, str2: String): String @oneOf
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Programmatic extensions flow
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
const GraphQLFindUserInput = new GraphQLInputObjectType({
|
|
99
|
+
name: 'FindUserInput',
|
|
100
|
+
fields: {
|
|
101
|
+
id: {
|
|
102
|
+
type: GraphQLID,
|
|
103
|
+
},
|
|
104
|
+
organizationAndRegistrationNumber: {
|
|
105
|
+
type: GraphQLInt,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
extensions: {
|
|
109
|
+
oneOf: true,
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const Query = new GraphQLObjectType({
|
|
114
|
+
name: 'Query',
|
|
115
|
+
fields: {
|
|
116
|
+
foo: {
|
|
117
|
+
type: GraphQLString,
|
|
118
|
+
args: {
|
|
119
|
+
id: {
|
|
120
|
+
type: GraphQLID,
|
|
121
|
+
},
|
|
122
|
+
str1: {
|
|
123
|
+
type: GraphQLString,
|
|
124
|
+
},
|
|
125
|
+
str2: {
|
|
126
|
+
type: GraphQLString,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
extensions: {
|
|
130
|
+
oneOf: true,
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
```
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/extended-validation",
|
|
3
|
-
"version": "1.7.0
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.4.0
|
|
6
|
+
"@envelop/core": "^2.4.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@graphql-tools/utils": "^8.
|
|
10
|
+
"@graphql-tools/utils": "^8.8.0"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|