import type { Node } from 'web-tree-sitter'; import type { ArgumentsDefinitionNode } from './arguments_definition.js'; import type { DescriptionNode } from './description.js'; import type { DirectivesNode } from './directives.js'; import type { NameNode } from './name.js'; import type { TypeNode } from './type.js'; const TYPE = 'field_definition' as const; // ================================================================ // ================================================================ // // FieldDefinitionNode // // GraphQL field definition node // // ================================================================ // ================================================================ /** * Represents a field definition in the GraphQL AST. * * Children: * {@link ArgumentsDefinitionNode}, {@link DescriptionNode}, {@link DirectivesNode}, {@link NameNode}, {@link TypeNode} * */ export interface FieldDefinitionNode extends Node { type: typeof TYPE; } // ================================================================ // ================================================================ // // Type Guard // // ================================================================ // ================================================================ /** * Type guard to check if a node is a {@link FieldDefinitionNode}. * * @param node - The node to check * @returns True if the node is a {@link FieldDefinitionNode} * * @example * ```typescript * if (isFieldDefinitionNode(node)) { * // TypeScript now knows node is FieldDefinitionNode * console.log(node.type); // 'field_definition' * } * ``` */ export function isFieldDefinitionNode(node: unknown): node is FieldDefinitionNode { return (node as any)?.type === TYPE; } // ================================================================ // ================================================================ // // Constructor // // ================================================================ // ================================================================ /** * Creates a new {@link FieldDefinitionNode} with the specified properties. * * @param props - The node properties * @param props.argumentsdefinition - {@link ArgumentsDefinitionNode} * @param props.description - {@link DescriptionNode} * @param props.directives - {@link DirectivesNode} * @param props.name - {@link NameNode} * @param props.type - {@link TypeNode} * @returns A new {@link FieldDefinitionNode} * * @example * ```typescript * const node = FieldDefinitionNode({ * // properties... * }); * ``` */ export function FieldDefinitionNode(props: Omit): FieldDefinitionNode { return { type: TYPE, ...props }; }