UNPKG

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