UNPKG

842 BJavaScriptView Raw
1import { GraphQLError } from "../../error/GraphQLError.mjs";
2import { Kind } from "../../language/kinds.mjs";
3
4/**
5 * Lone anonymous operation
6 *
7 * A GraphQL document is only valid if when it contains an anonymous operation
8 * (the query short-hand) that it contains only that one operation definition.
9 */
10export function LoneAnonymousOperationRule(context) {
11 var operationCount = 0;
12 return {
13 Document: function Document(node) {
14 operationCount = node.definitions.filter(function (definition) {
15 return definition.kind === Kind.OPERATION_DEFINITION;
16 }).length;
17 },
18 OperationDefinition: function OperationDefinition(node) {
19 if (!node.name && operationCount > 1) {
20 context.reportError(new GraphQLError('This anonymous operation must be the only defined operation.', node));
21 }
22 }
23 };
24}