import type { Node } from 'web-tree-sitter'; import type { DirectivesNode } from './directives.js'; import type { NameNode } from './name.js'; import type { OperationTypeNode } from './operation_type.js'; import type { SelectionSetNode } from './selection_set.js'; import type { VariableDefinitionsNode } from './variable_definitions.js'; const TYPE = 'operation_definition' as const; // ================================================================ // ================================================================ // // OperationDefinitionNode // // GraphQL operation definition node // // ================================================================ // ================================================================ /** * Represents a operation definition in the GraphQL AST. * * Children: * {@link DirectivesNode}, {@link NameNode}, {@link OperationTypeNode}, {@link SelectionSetNode}, {@link VariableDefinitionsNode} * */ export interface OperationDefinitionNode extends Node { type: typeof TYPE; } // ================================================================ // ================================================================ // // Type Guard // // ================================================================ // ================================================================ /** * Type guard to check if a node is a {@link OperationDefinitionNode}. * * @param node - The node to check * @returns True if the node is a {@link OperationDefinitionNode} * * @example * ```typescript * if (isOperationDefinitionNode(node)) { * // TypeScript now knows node is OperationDefinitionNode * console.log(node.type); // 'operation_definition' * } * ``` */ export function isOperationDefinitionNode(node: unknown): node is OperationDefinitionNode { return (node as any)?.type === TYPE; } // ================================================================ // ================================================================ // // Constructor // // ================================================================ // ================================================================ /** * Creates a new {@link OperationDefinitionNode} with the specified properties. * * @param props - The node properties * @param props.directives - {@link DirectivesNode} * @param props.name - {@link NameNode} * @param props.operationtype - {@link OperationTypeNode} * @param props.selectionset - {@link SelectionSetNode} * @param props.variabledefinitions - {@link VariableDefinitionsNode} * @returns A new {@link OperationDefinitionNode} * * @example * ```typescript * const node = OperationDefinitionNode({ * // properties... * }); * ``` */ export function OperationDefinitionNode(props: Omit): OperationDefinitionNode { return { type: TYPE, ...props }; }