@envelop/extended-validation 1.3.5-alpha-1c290fd.0 → 1.4.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/index.js CHANGED
@@ -8,6 +8,9 @@ const utils = require('@graphql-tools/utils');
8
8
  const symbolExtendedValidationRules = Symbol('extendedValidationContext');
9
9
  const useExtendedValidation = (options) => {
10
10
  let schemaTypeInfo;
11
+ function getTypeInfo() {
12
+ return schemaTypeInfo;
13
+ }
11
14
  return {
12
15
  onSchemaChange({ schema }) {
13
16
  schemaTypeInfo = new graphql.TypeInfo(schema);
@@ -26,43 +29,48 @@ const useExtendedValidation = (options) => {
26
29
  }
27
30
  validationRulesContext.rules.push(...options.rules);
28
31
  },
29
- onExecute({ args, setResultAndStopExecution }) {
30
- // We hook into onExecute even though this is a validation pattern. The reasoning behind
31
- // it is that hooking right after validation and before execution has started is the
32
- // same as hooking into the validation step. The benefit of this approach is that
33
- // we may use execution context in the validation rules.
34
- const validationRulesContext = args.contextValue[symbolExtendedValidationRules];
35
- if (validationRulesContext === undefined) {
36
- throw new Error('Plugin has not been properly set up. ' +
37
- "The 'contextFactory' function is not invoked and the result has not been passed to 'execute'.");
38
- }
39
- // we only want to run the extended execution once.
40
- if (validationRulesContext.didRun === false) {
41
- validationRulesContext.didRun = true;
42
- if (validationRulesContext.rules.length !== 0) {
43
- const errors = [];
44
- // We replicate the default validation step manually before execution starts.
45
- const typeInfo = schemaTypeInfo || new graphql.TypeInfo(args.schema);
46
- const validationContext = new graphql.ValidationContext(args.schema, args.document, typeInfo, e => {
47
- errors.push(e);
48
- });
49
- const visitor = graphql.visitInParallel(validationRulesContext.rules.map(rule => rule(validationContext, args)));
50
- graphql.visit(args.document, graphql.visitWithTypeInfo(typeInfo, visitor));
51
- if (errors.length > 0) {
52
- let result = {
53
- data: null,
54
- errors,
55
- };
56
- if (options.onValidationFailed) {
57
- options.onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
58
- }
59
- setResultAndStopExecution(result);
32
+ onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed),
33
+ onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed),
34
+ };
35
+ };
36
+ function buildHandler(name, getTypeInfo, onValidationFailed) {
37
+ return function handler({ args, setResultAndStopExecution, }) {
38
+ var _a;
39
+ // We hook into onExecute/onSubscribe even though this is a validation pattern. The reasoning behind
40
+ // it is that hooking right after validation and before execution has started is the
41
+ // same as hooking into the validation step. The benefit of this approach is that
42
+ // we may use execution context in the validation rules.
43
+ const validationRulesContext = args.contextValue[symbolExtendedValidationRules];
44
+ if (validationRulesContext === undefined) {
45
+ throw new Error('Plugin has not been properly set up. ' +
46
+ `The 'contextFactory' function is not invoked and the result has not been passed to '${name}'.`);
47
+ }
48
+ // we only want to run the extended execution once.
49
+ if (validationRulesContext.didRun === false) {
50
+ validationRulesContext.didRun = true;
51
+ if (validationRulesContext.rules.length !== 0) {
52
+ const errors = [];
53
+ // We replicate the default validation step manually before execution starts.
54
+ const typeInfo = (_a = getTypeInfo()) !== null && _a !== void 0 ? _a : new graphql.TypeInfo(args.schema);
55
+ const validationContext = new graphql.ValidationContext(args.schema, args.document, typeInfo, e => {
56
+ errors.push(e);
57
+ });
58
+ const visitor = graphql.visitInParallel(validationRulesContext.rules.map(rule => rule(validationContext, args)));
59
+ graphql.visit(args.document, graphql.visitWithTypeInfo(typeInfo, visitor));
60
+ if (errors.length > 0) {
61
+ let result = {
62
+ data: null,
63
+ errors,
64
+ };
65
+ if (onValidationFailed) {
66
+ onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
60
67
  }
68
+ setResultAndStopExecution(result);
61
69
  }
62
70
  }
63
- },
71
+ }
64
72
  };
65
- };
73
+ }
66
74
 
67
75
  function getDirectiveFromAstNode(astNode, names) {
68
76
  const directives = astNode.directives || [];
package/index.mjs CHANGED
@@ -4,6 +4,9 @@ import { getArgumentValues } from '@graphql-tools/utils';
4
4
  const symbolExtendedValidationRules = Symbol('extendedValidationContext');
5
5
  const useExtendedValidation = (options) => {
6
6
  let schemaTypeInfo;
7
+ function getTypeInfo() {
8
+ return schemaTypeInfo;
9
+ }
7
10
  return {
8
11
  onSchemaChange({ schema }) {
9
12
  schemaTypeInfo = new TypeInfo(schema);
@@ -22,43 +25,48 @@ const useExtendedValidation = (options) => {
22
25
  }
23
26
  validationRulesContext.rules.push(...options.rules);
24
27
  },
25
- onExecute({ args, setResultAndStopExecution }) {
26
- // We hook into onExecute even though this is a validation pattern. The reasoning behind
27
- // it is that hooking right after validation and before execution has started is the
28
- // same as hooking into the validation step. The benefit of this approach is that
29
- // we may use execution context in the validation rules.
30
- const validationRulesContext = args.contextValue[symbolExtendedValidationRules];
31
- if (validationRulesContext === undefined) {
32
- throw new Error('Plugin has not been properly set up. ' +
33
- "The 'contextFactory' function is not invoked and the result has not been passed to 'execute'.");
34
- }
35
- // we only want to run the extended execution once.
36
- if (validationRulesContext.didRun === false) {
37
- validationRulesContext.didRun = true;
38
- if (validationRulesContext.rules.length !== 0) {
39
- const errors = [];
40
- // We replicate the default validation step manually before execution starts.
41
- const typeInfo = schemaTypeInfo || new TypeInfo(args.schema);
42
- const validationContext = new ValidationContext(args.schema, args.document, typeInfo, e => {
43
- errors.push(e);
44
- });
45
- const visitor = visitInParallel(validationRulesContext.rules.map(rule => rule(validationContext, args)));
46
- visit(args.document, visitWithTypeInfo(typeInfo, visitor));
47
- if (errors.length > 0) {
48
- let result = {
49
- data: null,
50
- errors,
51
- };
52
- if (options.onValidationFailed) {
53
- options.onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
54
- }
55
- setResultAndStopExecution(result);
28
+ onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed),
29
+ onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed),
30
+ };
31
+ };
32
+ function buildHandler(name, getTypeInfo, onValidationFailed) {
33
+ return function handler({ args, setResultAndStopExecution, }) {
34
+ var _a;
35
+ // We hook into onExecute/onSubscribe even though this is a validation pattern. The reasoning behind
36
+ // it is that hooking right after validation and before execution has started is the
37
+ // same as hooking into the validation step. The benefit of this approach is that
38
+ // we may use execution context in the validation rules.
39
+ const validationRulesContext = args.contextValue[symbolExtendedValidationRules];
40
+ if (validationRulesContext === undefined) {
41
+ throw new Error('Plugin has not been properly set up. ' +
42
+ `The 'contextFactory' function is not invoked and the result has not been passed to '${name}'.`);
43
+ }
44
+ // we only want to run the extended execution once.
45
+ if (validationRulesContext.didRun === false) {
46
+ validationRulesContext.didRun = true;
47
+ if (validationRulesContext.rules.length !== 0) {
48
+ const errors = [];
49
+ // We replicate the default validation step manually before execution starts.
50
+ const typeInfo = (_a = getTypeInfo()) !== null && _a !== void 0 ? _a : new TypeInfo(args.schema);
51
+ const validationContext = new ValidationContext(args.schema, args.document, typeInfo, e => {
52
+ errors.push(e);
53
+ });
54
+ const visitor = visitInParallel(validationRulesContext.rules.map(rule => rule(validationContext, args)));
55
+ visit(args.document, visitWithTypeInfo(typeInfo, visitor));
56
+ if (errors.length > 0) {
57
+ let result = {
58
+ data: null,
59
+ errors,
60
+ };
61
+ if (onValidationFailed) {
62
+ onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
56
63
  }
64
+ setResultAndStopExecution(result);
57
65
  }
58
66
  }
59
- },
67
+ }
60
68
  };
61
- };
69
+ }
62
70
 
63
71
  function getDirectiveFromAstNode(astNode, names) {
64
72
  const directives = astNode.directives || [];
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@envelop/extended-validation",
3
- "version": "1.3.5-alpha-1c290fd.0",
3
+ "version": "1.4.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "^2.0.0",
6
+ "@envelop/core": "^2.1.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
package/plugin.d.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  import { Plugin } from '@envelop/core';
2
2
  import { ExecutionArgs, ExecutionResult } from 'graphql';
3
3
  import { ExtendedValidationRule } from './common';
4
+ declare type OnValidationFailedCallback = (params: {
5
+ args: ExecutionArgs;
6
+ result: ExecutionResult;
7
+ setResult: (result: ExecutionResult) => void;
8
+ }) => void;
4
9
  export declare const useExtendedValidation: (options: {
5
10
  rules: Array<ExtendedValidationRule>;
6
11
  /**
7
12
  * Callback that is invoked if the extended validation yields any errors.
8
13
  */
9
- onValidationFailed?: ((params: {
10
- args: ExecutionArgs;
11
- result: ExecutionResult;
12
- setResult: (result: ExecutionResult) => void;
13
- }) => void) | undefined;
14
+ onValidationFailed?: OnValidationFailedCallback;
14
15
  }) => Plugin;
16
+ export {};