import type { Node } from 'web-tree-sitter'; import type { FieldNode } from './field.js'; import type { FragmentSpreadNode } from './fragment_spread.js'; import type { InlineFragmentNode } from './inline_fragment.js'; const TYPE = 'selection' as const; // ================================================================ // ================================================================ // // SelectionNode // // GraphQL selection node // // ================================================================ // ================================================================ /** * Represents a selection in the GraphQL AST. * * Children: * {@link FieldNode}, {@link FragmentSpreadNode}, {@link InlineFragmentNode} * */ export interface SelectionNode extends Node { type: typeof TYPE; } // ================================================================ // ================================================================ // // Type Guard // // ================================================================ // ================================================================ /** * Type guard to check if a node is a {@link SelectionNode}. * * @param node - The node to check * @returns True if the node is a {@link SelectionNode} * * @example * ```typescript * if (isSelectionNode(node)) { * // TypeScript now knows node is SelectionNode * console.log(node.type); // 'selection' * } * ``` */ export function isSelectionNode(node: unknown): node is SelectionNode { return (node as any)?.type === TYPE; } // ================================================================ // ================================================================ // // Constructor // // ================================================================ // ================================================================ /** * Creates a new {@link SelectionNode} with the specified properties. * * @param props - The node properties * @param props.field - {@link FieldNode} * @param props.fragmentspread - {@link FragmentSpreadNode} * @param props.inlinefragment - {@link InlineFragmentNode} * @returns A new {@link SelectionNode} * * @example * ```typescript * const node = SelectionNode({ * // properties... * }); * ``` */ export function SelectionNode(props: Omit): SelectionNode { return { type: TYPE, ...props }; }