UNPKG

1.1 kBJavaScriptView Raw
1import { Kind } from "../language/kinds.mjs";
2/**
3 * Returns an operation AST given a document AST and optionally an operation
4 * name. If a name is not provided, an operation is only returned if only one is
5 * provided in the document.
6 */
7
8export function getOperationAST(documentAST, operationName) {
9 var operation = null;
10
11 for (var _i2 = 0, _documentAST$definiti2 = documentAST.definitions; _i2 < _documentAST$definiti2.length; _i2++) {
12 var definition = _documentAST$definiti2[_i2];
13
14 if (definition.kind === Kind.OPERATION_DEFINITION) {
15 var _definition$name;
16
17 if (operationName == null) {
18 // If no operation name was provided, only return an Operation if there
19 // is one defined in the document. Upon encountering the second, return
20 // null.
21 if (operation) {
22 return null;
23 }
24
25 operation = definition;
26 } else if (((_definition$name = definition.name) === null || _definition$name === void 0 ? void 0 : _definition$name.value) === operationName) {
27 return definition;
28 }
29 }
30 }
31
32 return operation;
33}