getOperationAST.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import { Kind } from "../language/kinds.mjs";
  2. /**
  3. * Returns an operation AST given a document AST and optionally an operation
  4. * name. If a name is not provided, an operation is only returned if only one is
  5. * provided in the document.
  6. */
  7. export function getOperationAST(documentAST, operationName) {
  8. var operation = null;
  9. for (var _i2 = 0, _documentAST$definiti2 = documentAST.definitions; _i2 < _documentAST$definiti2.length; _i2++) {
  10. var definition = _documentAST$definiti2[_i2];
  11. if (definition.kind === Kind.OPERATION_DEFINITION) {
  12. var _definition$name;
  13. if (operationName == null) {
  14. // If no operation name was provided, only return an Operation if there
  15. // is one defined in the document. Upon encountering the second, return
  16. // null.
  17. if (operation) {
  18. return null;
  19. }
  20. operation = definition;
  21. } else if (((_definition$name = definition.name) === null || _definition$name === void 0 ? void 0 : _definition$name.value) === operationName) {
  22. return definition;
  23. }
  24. }
  25. }
  26. return operation;
  27. }