1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createDefaultRules = exports.validateGraphQlDocuments = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | function validateGraphQlDocuments(schema, documents, rules = createDefaultRules()) {
|
6 | const definitions = new Set();
|
7 | const fragmentsDefinitionsMap = new Map();
|
8 | for (const document of documents) {
|
9 | for (const docDefinition of document.definitions) {
|
10 | if (docDefinition.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
|
11 | fragmentsDefinitionsMap.set(docDefinition.name.value, docDefinition);
|
12 | }
|
13 | else {
|
14 | definitions.add(docDefinition);
|
15 | }
|
16 | }
|
17 | }
|
18 | const fullAST = {
|
19 | kind: graphql_1.Kind.DOCUMENT,
|
20 | definitions: Array.from([...definitions, ...fragmentsDefinitionsMap.values()]),
|
21 | };
|
22 | const errors = (0, graphql_1.validate)(schema, fullAST, rules);
|
23 | for (const error of errors) {
|
24 | error.stack = error.message;
|
25 | if (error.locations) {
|
26 | for (const location of error.locations) {
|
27 | error.stack += `\n at ${error.source?.name}:${location.line}:${location.column}`;
|
28 | }
|
29 | }
|
30 | }
|
31 | return errors;
|
32 | }
|
33 | exports.validateGraphQlDocuments = validateGraphQlDocuments;
|
34 | function createDefaultRules() {
|
35 | let ignored = ['NoUnusedFragmentsRule', 'NoUnusedVariablesRule', 'KnownDirectivesRule'];
|
36 | if (graphql_1.versionInfo.major < 15) {
|
37 | ignored = ignored.map(rule => rule.replace(/Rule$/, ''));
|
38 | }
|
39 | return graphql_1.specifiedRules.filter((f) => !ignored.includes(f.name));
|
40 | }
|
41 | exports.createDefaultRules = createDefaultRules;
|