UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

1 lines 2.3 kB
{"version":3,"file":"getOperationAST.js","sourceRoot":"","sources":["../../src/utilities/getOperationAST.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,IAAI,EAAE,8BAA6B;AAqB5C,MAAM,UAAU,eAAe,CAC7B,WAAyB,EACzB,aAA6B;IAE7B,IAAI,SAAS,GAAG,IAAI,CAAC;IACrB,KAAK,MAAM,UAAU,IAAI,WAAW,CAAC,WAAW,EAAE,CAAC;QACjD,IAAI,UAAU,CAAC,IAAI,KAAK,IAAI,CAAC,oBAAoB,EAAE,CAAC;YAClD,IAAI,aAAa,IAAI,IAAI,EAAE,CAAC;gBAI1B,IAAI,SAAS,EAAE,CAAC;oBACd,OAAO,IAAI,CAAC;gBACd,CAAC;gBACD,SAAS,GAAG,UAAU,CAAC;YACzB,CAAC;iBAAM,IAAI,UAAU,CAAC,IAAI,EAAE,KAAK,KAAK,aAAa,EAAE,CAAC;gBACpD,OAAO,UAAU,CAAC;YACpB,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["/** @category Operations */\n\nimport type { Maybe } from '../jsutils/Maybe.ts';\n\nimport type { DocumentNode, OperationDefinitionNode } from '../language/ast.ts';\nimport { Kind } from '../language/kinds.ts';\n\n/**\n * Returns an operation AST given a document AST and optionally an operation\n * name. If a name is not provided, an operation is only returned if only one is\n * provided in the document.\n * @param documentAST - The parsed GraphQL document AST.\n * @param operationName - The optional operation name to select.\n * @returns The resolved operation ast.\n * @example\n * ```ts\n * import { parse } from 'graphql/language';\n * import { getOperationAST } from 'graphql/utilities';\n *\n * const document = parse('query GetName { name }');\n * const operation = getOperationAST(document, 'GetName');\n *\n * operation.name.value; // => 'GetName'\n * getOperationAST(document, 'Missing'); // => undefined\n * ```\n */\nexport function getOperationAST(\n documentAST: DocumentNode,\n operationName?: Maybe<string>,\n): Maybe<OperationDefinitionNode> {\n let operation = null;\n for (const definition of documentAST.definitions) {\n if (definition.kind === Kind.OPERATION_DEFINITION) {\n if (operationName == null) {\n // If no operation name was provided, only return an Operation if there\n // is one defined in the document. Upon encountering the second, return\n // null.\n if (operation) {\n return null;\n }\n operation = definition;\n } else if (definition.name?.value === operationName) {\n return definition;\n }\n }\n }\n return operation;\n}\n"]}