import type { Node } from 'web-tree-sitter'; import type { AliasNode } from './alias.js'; import type { ArgumentsNode } from './arguments.js'; import type { DirectiveNode } from './directive.js'; import type { NameNode } from './name.js'; import type { SelectionSetNode } from './selection_set.js'; const TYPE = 'field' as const; // ================================================================ // ================================================================ // // FieldNode // // GraphQL field node // // ================================================================ // ================================================================ /** * Represents a field in the GraphQL AST. * * Children: * {@link AliasNode}, {@link ArgumentsNode}, {@link DirectiveNode}, {@link NameNode}, {@link SelectionSetNode} * * @example * ```graphql * query { * user { * name # This is a FieldNode * } * } * ``` */ export interface FieldNode extends Node { type: typeof TYPE; } // ================================================================ // ================================================================ // // Type Guard // // ================================================================ // ================================================================ /** * Type guard to check if a node is a {@link FieldNode}. * * @param node - The node to check * @returns True if the node is a {@link FieldNode} * * @example * ```typescript * if (isFieldNode(node)) { * // TypeScript now knows node is FieldNode * console.log(node.type); // 'field' * } * ``` */ export function isFieldNode(node: unknown): node is FieldNode { return (node as any)?.type === TYPE; } // ================================================================ // ================================================================ // // Constructor // // ================================================================ // ================================================================ /** * Creates a new {@link FieldNode} with the specified properties. * * @param props - The node properties * @param props.alias - {@link AliasNode} * @param props.arguments - {@link ArgumentsNode} * @param props.directive - {@link DirectiveNode} * @param props.name - {@link NameNode} * @param props.selectionset - {@link SelectionSetNode} * @returns A new {@link FieldNode} * * @example * ```typescript * const field = FieldNode({ * name: NameNode("user"), * arguments: ArgumentsNode([...]) * }); * ``` */ export function FieldNode(props: Omit): FieldNode { return { type: TYPE, ...props }; }