-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New plugin:
useExtendedValidation
and implementation of @oneOf
di…
…rective (#149) * wip * Fix implementation of oneOf directive test fixes * fixes * added docs
- Loading branch information
1 parent
028129a
commit ced704e
Showing
15 changed files
with
576 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@envelop/extended-validation': patch | ||
--- | ||
|
||
NEW PLUGIN! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@envelop/core': patch | ||
'@envelop/types': patch | ||
--- | ||
|
||
Allow plugins to stop execution and return errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
## `@envelop/extended-validation` | ||
|
||
Extended validation plugin adds support for writing GraphQL validation rules, that has access to all `execute` parameters, including variables. | ||
|
||
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). | ||
|
||
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). | ||
|
||
## Getting Started | ||
|
||
Start by installing the plugin: | ||
|
||
``` | ||
yarn add @envelop/extended-validation | ||
``` | ||
|
||
Then, use the plugin with your validation rules: | ||
|
||
```ts | ||
import { useExtendedValidation } from '@envelop/extended-validation'; | ||
|
||
const getEnveloped = evelop({ | ||
plugins: [ | ||
useExtendedValidation({ | ||
rules: [ ... ] // your rules here | ||
}) | ||
] | ||
}) | ||
``` | ||
|
||
To create your custom rules, implement the `ExtendedValidationRule` interface and return your GraphQL AST visitor. | ||
|
||
For example: | ||
|
||
```ts | ||
import { ExtendedValidationRule } from '@envelop/extended-validation'; | ||
|
||
export const MyRule: ExtendedValidationRule = (validationContext, executionArgs) => { | ||
return { | ||
OperationDefinition: node => { | ||
// This will run for every executed Query/Mutation/Subscription | ||
// And now you also have access to the execution params like variables, context and so on. | ||
// If you wish to report an error, use validationContext.reportError or throw an exception. | ||
}, | ||
}; | ||
}; | ||
``` | ||
|
||
## Built-in Rules | ||
|
||
### Union Inputs: `@oneOf` | ||
|
||
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). | ||
|
||
To use that validation rule, make sure to include the following directive in your schema: | ||
|
||
```graphql | ||
directive @oneOf on INPUT_OBJECT | FIELD_DEFINITION | ||
``` | ||
|
||
Then, apply it to field definitions, or to a complete `input` type: | ||
|
||
```graphql | ||
## Apply to entire input type | ||
input FindUserInput @oneOf { | ||
id: ID | ||
organizationAndRegistrationNumber: OrganizationAndRegistrationNumberInput | ||
} | ||
|
||
## Or, apply to a set of input arguments | ||
|
||
type Query { | ||
foo(id: ID, str1: String, str2: String): String @oneOf | ||
} | ||
``` | ||
|
||
Then, make sure to add that rule to your plugin usage: | ||
|
||
```ts | ||
import { useExtendedValidation, OneOfInputObjectsRule } from '@envelop/extended-validation'; | ||
|
||
const getEnveloped = evelop({ | ||
plugins: [ | ||
useExtendedValidation({ | ||
rules: [OneOfInputObjectsRule], | ||
}), | ||
], | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@envelop/extended-validation", | ||
"version": "0.0.0", | ||
"author": "Dotan Simha <dotansimha@gmail.com>", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dotansimha/envelop.git", | ||
"directory": "packages/plugins/extended-validation" | ||
}, | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"typescript": { | ||
"definition": "dist/index.d.ts" | ||
}, | ||
"scripts": { | ||
"test": "jest", | ||
"prepack": "bob prepack" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"bob-the-bundler": "1.2.0", | ||
"graphql": "15.5.0", | ||
"typescript": "4.2.4" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^14.0.0 || ^15.0.0" | ||
}, | ||
"buildOptions": { | ||
"input": "./src/index.ts" | ||
}, | ||
"publishConfig": { | ||
"directory": "dist", | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
ASTVisitor, | ||
DirectiveNode, | ||
ExecutionArgs, | ||
GraphQLNamedType, | ||
GraphQLType, | ||
isListType, | ||
isNonNullType, | ||
ValidationContext, | ||
} from 'graphql'; | ||
|
||
export type ExtendedValidationRule = (context: ValidationContext, executionArgs: ExecutionArgs) => ASTVisitor; | ||
|
||
export function getDirectiveFromAstNode( | ||
astNode: { directives?: ReadonlyArray<DirectiveNode> }, | ||
names: string | string[] | ||
): null | DirectiveNode { | ||
if (!astNode) { | ||
return null; | ||
} | ||
|
||
const directives = astNode.directives || []; | ||
const namesArr = Array.isArray(names) ? names : [names]; | ||
const authDirective = directives.find(d => namesArr.includes(d.name.value)); | ||
|
||
return authDirective || null; | ||
} | ||
|
||
export function unwrapType(type: GraphQLType): GraphQLNamedType { | ||
if (isNonNullType(type) || isListType(type)) { | ||
return unwrapType(type.ofType); | ||
} | ||
|
||
return type; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './plugin'; | ||
export * from './common'; | ||
|
||
export * from './rules/one-of'; |
Oops, something went wrong.