57 lines
1.6 KiB
JavaScript
57 lines
1.6 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.getOperationAST = getOperationAST;
|
|
|
|
var _kinds = require("../language/kinds");
|
|
|
|
/**
|
|
* Returns an operation AST given a document AST and optionally an operation
|
|
* name. If a name is not provided, an operation is only returned if only one is
|
|
* provided in the document.
|
|
*/
|
|
function getOperationAST(documentAST, operationName) {
|
|
var operation = null;
|
|
var _iteratorNormalCompletion = true;
|
|
var _didIteratorError = false;
|
|
var _iteratorError = undefined;
|
|
|
|
try {
|
|
for (var _iterator = documentAST.definitions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var definition = _step.value;
|
|
|
|
if (definition.kind === _kinds.Kind.OPERATION_DEFINITION) {
|
|
if (!operationName) {
|
|
// If no operation name was provided, only return an Operation if there
|
|
// is one defined in the document. Upon encountering the second, return
|
|
// null.
|
|
if (operation) {
|
|
return null;
|
|
}
|
|
|
|
operation = definition;
|
|
} else if (definition.name && definition.name.value === operationName) {
|
|
return definition;
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
|
|
return operation;
|
|
}
|