1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const graphql_1 = require("graphql");
|
4 | const errors_1 = require("apollo-codegen-core/lib/errors");
|
5 | function getValidationErrors(schema, document, fragments) {
|
6 | const specifiedRulesToBeRemoved = [
|
7 | graphql_1.NoUnusedFragmentsRule,
|
8 | graphql_1.KnownDirectivesRule
|
9 | ];
|
10 | const rules = [
|
11 | NoAnonymousQueries,
|
12 | NoTypenameAlias,
|
13 | ...graphql_1.specifiedRules.filter(rule => !specifiedRulesToBeRemoved.includes(rule))
|
14 | ];
|
15 | const typeInfo = new graphql_1.TypeInfo(schema);
|
16 | const context = new graphql_1.ValidationContext(schema, document, typeInfo);
|
17 | if (fragments) {
|
18 | context._fragments = fragments;
|
19 | }
|
20 | const visitors = rules.map(rule => rule(context));
|
21 | graphql_1.visit(document, graphql_1.visitWithTypeInfo(typeInfo, graphql_1.visitInParallel(visitors)));
|
22 | return context.getErrors();
|
23 | }
|
24 | exports.getValidationErrors = getValidationErrors;
|
25 | function validateQueryDocument(schema, document) {
|
26 | const validationErrors = getValidationErrors(schema, document);
|
27 | if (validationErrors && validationErrors.length > 0) {
|
28 | for (const error of validationErrors) {
|
29 | errors_1.logError(error);
|
30 | }
|
31 | throw new errors_1.ToolError("Validation of GraphQL query document failed");
|
32 | }
|
33 | }
|
34 | exports.validateQueryDocument = validateQueryDocument;
|
35 | function NoAnonymousQueries(context) {
|
36 | return {
|
37 | OperationDefinition(node) {
|
38 | if (!node.name) {
|
39 | context.reportError(new graphql_1.GraphQLError("Apollo does not support anonymous operations", [
|
40 | node
|
41 | ]));
|
42 | }
|
43 | return false;
|
44 | }
|
45 | };
|
46 | }
|
47 | exports.NoAnonymousQueries = NoAnonymousQueries;
|
48 | function NoTypenameAlias(context) {
|
49 | return {
|
50 | Field(node) {
|
51 | const aliasName = node.alias && node.alias.value;
|
52 | if (aliasName == "__typename") {
|
53 | context.reportError(new graphql_1.GraphQLError("Apollo needs to be able to insert __typename when needed, please do not use it as an alias", [node]));
|
54 | }
|
55 | }
|
56 | };
|
57 | }
|
58 | exports.NoTypenameAlias = NoTypenameAlias;
|
59 |
|
\ | No newline at end of file |