getOperationAST.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getOperationAST = getOperationAST;
  6. var _kinds = require("../language/kinds");
  7. /**
  8. * Returns an operation AST given a document AST and optionally an operation
  9. * name. If a name is not provided, an operation is only returned if only one is
  10. * provided in the document.
  11. */
  12. function getOperationAST(documentAST, operationName) {
  13. var operation = null;
  14. for (var _i2 = 0, _documentAST$definiti2 = documentAST.definitions; _i2 < _documentAST$definiti2.length; _i2++) {
  15. var definition = _documentAST$definiti2[_i2];
  16. if (definition.kind === _kinds.Kind.OPERATION_DEFINITION) {
  17. if (!operationName) {
  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. operation = definition;
  25. } else if (definition.name && definition.name.value === operationName) {
  26. return definition;
  27. }
  28. }
  29. }
  30. return operation;
  31. }