-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvalidateWithCustomRules.ts
62 lines (58 loc) · 1.7 KB
/
validateWithCustomRules.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import {
ValidationRule,
DocumentNode,
Kind,
specifiedRules,
validate,
GraphQLError,
GraphQLSchema,
NoUnusedFragmentsRule,
KnownFragmentNamesRule,
} from 'graphql';
import { ExecutableDefinitions } from 'graphql/validation/rules/ExecutableDefinitions';
/**
* Validate a GraphQL Document optionally with custom validation rules.
*/
export function validateWithCustomRules(
schema: GraphQLSchema,
ast: DocumentNode,
customRules?: Array<ValidationRule> | null,
isRelayCompatMode?: boolean,
): Array<GraphQLError> {
const rules = specifiedRules.filter(rule => {
// Because every fragment is considered for determing model subsets that may
// be used anywhere in the codebase they're all technically "used" by clients
// of graphql-data. So we remove this rule from the validators.
if (rule === NoUnusedFragmentsRule || rule === ExecutableDefinitions) {
return false;
}
if (isRelayCompatMode && rule === KnownFragmentNamesRule) {
return false;
}
return true;
});
if (customRules) {
Array.prototype.push.apply(rules, customRules);
}
const errors = validate(schema, ast, rules);
return errors.filter(error => {
if (error.message.indexOf('Unknown directive') !== -1 && error.nodes) {
const node = error.nodes[0];
if (node && node.kind === Kind.DIRECTIVE) {
const name = node.name.value;
if (name === 'arguments' || name === 'argumentDefinitions') {
return false;
}
}
}
return true;
});
}