UNPKG

1.12 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getOperationAST = getOperationAST;
7
8var _kinds = require("../language/kinds");
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 if (!operationName) {
23 // If no operation name was provided, only return an Operation if there
24 // is one defined in the document. Upon encountering the second, return
25 // null.
26 if (operation) {
27 return null;
28 }
29
30 operation = definition;
31 } else if (definition.name && definition.name.value === operationName) {
32 return definition;
33 }
34 }
35 }
36
37 return operation;
38}