-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathvalidator.js
More file actions
90 lines (78 loc) · 2.1 KB
/
validator.js
File metadata and controls
90 lines (78 loc) · 2.1 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { parse } from 'graphql';
import { validate } from 'graphql/validation';
import { buildASTSchema } from 'graphql/utilities/buildASTSchema';
import { GraphQLError } from 'graphql/error';
import { validateSDL } from 'graphql/validation/validate';
import { validateSchema } from 'graphql/type/validate';
import { ValidationError } from './validation_error';
export function validateSchemaDefinition(
schemaDefinition,
rules,
configuration
) {
let ast;
let parseOptions = {};
if (configuration.getOldImplementsSyntax()) {
parseOptions.allowLegacySDLImplementsInterfaces = true;
}
try {
ast = parse(schemaDefinition, parseOptions);
} catch (e) {
if (e instanceof GraphQLError) {
e.ruleName = 'graphql-syntax-error';
return [e];
} else {
throw e;
}
}
let schemaErrors = validateSDL(ast);
if (schemaErrors.length > 0) {
return sortErrors(
schemaErrors.map(error => {
return new ValidationError(
'invalid-graphql-schema',
error.message,
error.nodes
);
})
);
}
const schema = buildASTSchema(ast, {
commentDescriptions: configuration.getCommentDescriptions(),
assumeValidSDL: true,
assumeValid: true,
});
schema.__validationErrors = undefined;
schemaErrors = validateSchema(schema);
if (schemaErrors.length > 0) {
return sortErrors(
schemaErrors.map(error => {
return new ValidationError(
'invalid-graphql-schema',
error.message,
error.nodes || ast
);
})
);
}
const rulesWithConfiguration = rules.map(rule => {
return ruleWithConfiguration(rule, configuration);
});
const errors = validate(schema, ast, rulesWithConfiguration);
const sortedErrors = sortErrors(errors);
return sortedErrors;
}
function sortErrors(errors) {
return errors.sort((a, b) => {
return a.locations[0].line - b.locations[0].line;
});
}
function ruleWithConfiguration(rule, configuration) {
if (rule.length == 2) {
return function(context) {
return rule(configuration, context);
};
} else {
return rule;
}
}