Skip to content

Commit 847a1e4

Browse files
committed
feat(extended-validation): merge useExtendedValidation usages (#245)
1 parent e57cb32 commit 847a1e4

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

.changeset/curly-needles-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@envelop/extended-validation': patch
3+
---
4+
5+
suuport multiple usages of useExtendedValidation

packages/plugins/extended-validation/src/plugin.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@ import { Plugin } from '@envelop/types';
22
import { GraphQLError, TypeInfo, ValidationContext, visit, visitInParallel, visitWithTypeInfo } from 'graphql';
33
import { ExtendedValidationRule } from './common';
44

5+
const SYMBOL_EXTENDED_VALIDATION_RULES = Symbol('SYMBOL_EXTENDED_VALIDATION_RULES');
6+
57
export const useExtendedValidation = (options: { rules: ExtendedValidationRule[] }): Plugin => {
68
let schemaTypeInfo: TypeInfo;
79

810
return {
911
onSchemaChange({ schema }) {
1012
schemaTypeInfo = new TypeInfo(schema);
1113
},
14+
onParse({ context, extendContext }) {
15+
const rules: ExtendedValidationRule[] = (context as any)?.[SYMBOL_EXTENDED_VALIDATION_RULES] ?? [];
16+
rules.push(...options.rules);
17+
extendContext({
18+
[SYMBOL_EXTENDED_VALIDATION_RULES]: rules,
19+
});
20+
},
1221
onExecute({ args, setResultAndStopExecution }) {
22+
const rules: ExtendedValidationRule[] = args.contextValue[SYMBOL_EXTENDED_VALIDATION_RULES];
1323
const errors: GraphQLError[] = [];
1424
const typeInfo = schemaTypeInfo || new TypeInfo(args.schema);
1525
const validationContext = new ValidationContext(args.schema, args.document, typeInfo, e => {
1626
errors.push(e);
1727
});
1828

19-
const visitor = visitInParallel(options.rules.map(rule => rule(validationContext, args)));
29+
const visitor = visitInParallel(rules.map(rule => rule(validationContext, args)));
2030
visit(args.document, visitWithTypeInfo(typeInfo, visitor));
2131

22-
for (const rule of options.rules) {
32+
for (const rule of rules) {
2333
rule(validationContext, args);
2434
}
2535

2636
if (errors.length > 0) {
27-
setResultAndStopExecution({
37+
return setResultAndStopExecution({
2838
data: null,
2939
errors,
3040
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { createTestkit } from '@envelop/testing';
2+
import { buildSchema, GraphQLError } from 'graphql';
3+
import { useExtendedValidation } from '../src';
4+
5+
describe('useExtendedValidation', () => {
6+
it('supports usage of multiple useExtendedValidation in different plugins', async () => {
7+
const schema = buildSchema(/* GraphQL */ `
8+
type Query {
9+
foo: String!
10+
}
11+
`);
12+
const operation = /* GraphQL */ `
13+
{
14+
foo
15+
}
16+
`;
17+
const testInstance = createTestkit(
18+
[
19+
useExtendedValidation({
20+
rules: [
21+
ctx => {
22+
return {
23+
OperationDefinition() {
24+
ctx.reportError(new GraphQLError('No 1'));
25+
},
26+
};
27+
},
28+
],
29+
}),
30+
useExtendedValidation({
31+
rules: [
32+
ctx => {
33+
return {
34+
OperationDefinition() {
35+
ctx.reportError(new GraphQLError('No 2'));
36+
},
37+
};
38+
},
39+
],
40+
}),
41+
],
42+
schema
43+
);
44+
45+
const result = await testInstance.execute(operation);
46+
expect(result).toMatchInlineSnapshot(`
47+
Object {
48+
"data": null,
49+
"errors": Array [
50+
[GraphQLError: No 1],
51+
[GraphQLError: No 2],
52+
],
53+
}
54+
`);
55+
});
56+
});

packages/testing/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function createTestkit(
7878
return {
7979
wait: ms => new Promise(resolve => setTimeout(resolve, ms)),
8080
replaceSchema,
81-
execute: async (operation, rawVariables = {}, initialContext = null) => {
81+
execute: async (operation, rawVariables = {}, initialContext = {}) => {
8282
const request = {
8383
headers: {},
8484
method: 'POST',

0 commit comments

Comments
 (0)