@@ -7,12 +7,12 @@ import {
77} from './directoryToAst' ;
88
99/**
10- * Do not traverse children
10+ * Do not traverse children, and go to next sibling.
1111 */
1212export const VISITOR_SKIP_CHILDREN = false ;
1313
1414/**
15- * Means remove Node from Ast and do not traverse children
15+ * Remove Node from AST and do not traverse children.
1616 */
1717export const VISITOR_REMOVE_NODE = null ;
1818
@@ -22,23 +22,44 @@ export type VisitorEmptyResult =
2222 | typeof VISITOR_SKIP_CHILDREN ;
2323
2424export type VisitKindFn < NodeKind > = (
25+ /** Information about `node` obtained in `directoryToAst()` */
2526 node : NodeKind ,
27+ /** Information from visitor during traversing AST tree */
2628 info : VisitInfo
2729) => VisitorEmptyResult | NodeKind ;
2830
31+ /**
32+ * Functions for every type of AST nodes which will be called by visitor.
33+ */
2934export type AstVisitor = {
3035 DIR ?: VisitKindFn < AstDirNode > ;
3136 FILE ?: VisitKindFn < AstFileNode > ;
3237 ROOT_TYPE ?: VisitKindFn < AstRootTypeNode > ;
3338} ;
3439
3540export interface VisitInfo {
41+ /** Brunch of schema under which is working visitor. Can be: query, mutation, subscription */
3642 operation : RootTypeNames ;
43+ /** Parent AST node from directoryToAst */
3744 parent : AstDirNode | AstRootTypeNode | AstRootNode ;
45+ /** Name of field for current FieldConfig */
3846 name : string ;
47+ /** List of parent names starting from root */
3948 path : string [ ] ;
4049}
4150
51+ /**
52+ * Traverse AST for applying modifications to DIRs, FILEs & ROOT_TYPEs
53+ * Useful for writing middlewares which transform FieldConfigs entrypoints.
54+ *
55+ * @example
56+ * const ast = directoryToAst(module);
57+ * astVisitor(ast, {
58+ * ROOT_TYPE: () => {}, // run for query, mutation, subscription
59+ * DIR: () => {}, // executes on visiting DIR node
60+ * FILE: () => {}, // executes on visiting FILE node
61+ * });
62+ */
4263export function astVisitor ( ast : AstRootNode , visitor : AstVisitor ) : void {
4364 ( Object . keys ( ast . children ) as Array < keyof typeof ast . children > ) . forEach ( ( operation ) => {
4465 const rootNode = ast . children [ operation ] ;
0 commit comments