getOperationAST.mjs 992 B

12345678910111213141516171819202122232425262728293031
  1. import { Kind } from '../language/kinds';
  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. if (!operationName) {
  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 (definition.name && definition.name.value === operationName) {
  21. return definition;
  22. }
  23. }
  24. }
  25. return operation;
  26. }