UNPKG

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