UNPKG

1 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.LoneAnonymousOperationRule = LoneAnonymousOperationRule;
7
8var _GraphQLError = require("../../error/GraphQLError.js");
9
10var _kinds = require("../../language/kinds.js");
11
12/**
13 * Lone anonymous operation
14 *
15 * A GraphQL document is only valid if when it contains an anonymous operation
16 * (the query short-hand) that it contains only that one operation definition.
17 */
18function LoneAnonymousOperationRule(context) {
19 var operationCount = 0;
20 return {
21 Document: function Document(node) {
22 operationCount = node.definitions.filter(function (definition) {
23 return definition.kind === _kinds.Kind.OPERATION_DEFINITION;
24 }).length;
25 },
26 OperationDefinition: function OperationDefinition(node) {
27 if (!node.name && operationCount > 1) {
28 context.reportError(new _GraphQLError.GraphQLError('This anonymous operation must be the only defined operation.', node));
29 }
30 }
31 };
32}