UNPKG

1.25 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 for (var i = 0; i < documentAST.definitions.length; i++) {
18 var definition = documentAST.definitions[i];
19 if (definition.kind === _kinds.OPERATION_DEFINITION) {
20 if (!operationName) {
21 // If no operation name was provided, only return an Operation if there
22 // is one defined in the document. Upon encountering the second, return
23 // null.
24 if (operation) {
25 return null;
26 }
27 operation = definition;
28 } else if (definition.name && definition.name.value === operationName) {
29 return definition;
30 }
31 }
32 }
33 return operation;
34} /**
35 * Copyright (c) 2015-present, Facebook, Inc.
36 *
37 * This source code is licensed under the MIT license found in the
38 * LICENSE file in the root directory of this source tree.
39 *
40 *
41 */
\No newline at end of file