getOperationAST.mjs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  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. let operation = null;
  9. for (const definition of documentAST.definitions) {
  10. if (definition.kind === Kind.OPERATION_DEFINITION) {
  11. var _definition$name;
  12. if (operationName == null) {
  13. // If no operation name was provided, only return an Operation if there
  14. // is one defined in the document. Upon encountering the second, return
  15. // null.
  16. if (operation) {
  17. return null;
  18. }
  19. operation = definition;
  20. } else if (
  21. ((_definition$name = definition.name) === null ||
  22. _definition$name === void 0
  23. ? void 0
  24. : _definition$name.value) === operationName
  25. ) {
  26. return definition;
  27. }
  28. }
  29. }
  30. return operation;
  31. }