-
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.
feat: add @envelop/filter-operation-type package (#131)
* feat: add @envelop/filter-operation-type package * update readme Co-authored-by: Dotan Simha <dotansimha@gmail.com>
- Loading branch information
1 parent
d1c0ce0
commit 219c41e
Showing
6 changed files
with
112 additions
and
22 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/filter-operation-type': patch | ||
--- | ||
|
||
Initial implementation of the 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
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,20 @@ | ||
## `@envelop/filter-operation-type` | ||
|
||
This plugins injects a validation rule into the validation phase that only allows the specified operation types (e.g. `subscription`, `query` or `mutation`). | ||
|
||
## Getting Started | ||
|
||
``` | ||
yarn add @envelop/filter-operation-type | ||
``` | ||
|
||
## Usage Example | ||
|
||
```ts | ||
import { envelop } from '@envelop/core'; | ||
import { useFilterAllowedOperations } from '@envelop/filter-operation-type'; | ||
const getEnveloped = envelop({ | ||
// only allow execution of subscription operations | ||
plugins: [useFilterAllowedOperations(['subscription'])], | ||
}); | ||
``` |
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/filter-operation-type", | ||
"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/filter-operation-type" | ||
}, | ||
"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" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/plugins/filter-operation-type/src/filter-operation-type-rule.ts
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,14 @@ | ||
import { ValidationRule, OperationTypeNode, GraphQLError } from 'graphql'; | ||
|
||
export type AllowedOperations = Iterable<OperationTypeNode>; | ||
|
||
export const createFilterOperationTypeRule = (allowedOperations: AllowedOperations): ValidationRule => context => { | ||
const ops = new Set(allowedOperations); | ||
return { | ||
OperationDefinition(node) { | ||
if (!ops.has(node.operation)) { | ||
context.reportError(new GraphQLError(`GraphQL operation type "${node.operation}" is not allowed.`, [node])); | ||
} | ||
}, | ||
}; | ||
}; |
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,12 @@ | ||
import { Plugin } from '@envelop/types'; | ||
import { createFilterOperationTypeRule, AllowedOperations } from './filter-operation-type-rule'; | ||
|
||
export const useFilterAllowedOperations = (allowedOperations: AllowedOperations): Plugin => { | ||
return { | ||
onValidate: ({ addValidationRule }) => { | ||
addValidationRule(createFilterOperationTypeRule(allowedOperations)); | ||
}, | ||
}; | ||
}; | ||
|
||
export { createFilterOperationTypeRule }; |