Skip to content

Commit 2d29ccc

Browse files
committed
docs: add jsdoc to functions & interfaces
1 parent 99da8c2 commit 2d29ccc

File tree

5 files changed

+118
-9
lines changed

5 files changed

+118
-9
lines changed

src/astToSchema.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,41 @@ import { GraphQLObjectType } from 'graphql';
1616
import { FieldConfig, NamespaceConfig } from './typeDefs';
1717

1818
export interface AstToSchemaOptions {
19+
/**
20+
* Pass here already existed SchemaComposer instance
21+
* which already contains some types (eg with custom Scalars).
22+
*
23+
* Or new SchemaComposer instance will be created.
24+
*/
1925
schemaComposer?: SchemaComposer<any>;
2026
prefix?: string;
2127
suffix?: string;
2228
}
2329

30+
/**
31+
* Transform AST to GraphQL Schema.
32+
*
33+
* @example
34+
* // Scan some directory for getting Schema entrypoints as AST nodes.
35+
* const ast = directoryToAst(module);
36+
*
37+
* // [Optional] Combining severals ast into source
38+
* // Useful if some sub-schemas are delivered via packages
39+
* // or created n separate directories.
40+
* const newAST = astMerge(ast, ast1, ast2, ...);
41+
*
42+
* // [Optional] Some `ast` modifications
43+
* // Useful for writing some middlewares
44+
* // which transform FieldConfigs entrypoints.
45+
* astVisitor(newAST, visitorFns);
46+
*
47+
* // Create SchemaComposer instance with all populated types & fields
48+
* // It provides declarative programmatic access to modify you schema
49+
* const schemaComposer = astToSchema(newAST, opts);
50+
*
51+
* // Create GraphQLSchema instance which is ready for runtime.
52+
* const schema = schemaComposer.buildSchema();;
53+
*/
2454
export function astToSchema<TContext = any>(
2555
ast: AstRootNode,
2656
opts: AstToSchemaOptions = {}

src/astVisitor.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
1212
export 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
*/
1717
export const VISITOR_REMOVE_NODE = null;
1818

@@ -22,23 +22,44 @@ export type VisitorEmptyResult =
2222
| typeof VISITOR_SKIP_CHILDREN;
2323

2424
export 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+
*/
2934
export type AstVisitor = {
3035
DIR?: VisitKindFn<AstDirNode>;
3136
FILE?: VisitKindFn<AstFileNode>;
3237
ROOT_TYPE?: VisitKindFn<AstRootTypeNode>;
3338
};
3439

3540
export 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+
*/
4263
export 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];

src/directoryToAst.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import fs from 'fs';
22
import { join, resolve, dirname, basename } from 'path';
33
import { FieldConfig, NamespaceConfig } from './typeDefs';
44

5-
export interface DirectoryToAstOptions {
6-
relativePath?: string;
7-
extensions?: string[];
8-
include?: RegExp | ((path: string, kind: 'dir' | 'file', filename: string) => boolean);
9-
exclude?: RegExp | ((path: string, kind: 'dir' | 'file', filename: string) => boolean);
10-
}
11-
125
export type AstNodeKinds = 'rootType' | 'dir' | 'file' | 'root';
136

147
export interface AstBaseNode {
@@ -51,6 +44,23 @@ export const defaultOptions: DirectoryToAstOptions = {
5144
extensions: ['js', 'ts'],
5245
};
5346

47+
export interface DirectoryToAstOptions {
48+
/** Scan relative directory to `module` which was provided as a first arg to directoryToAst method. By default `.` */
49+
relativePath?: string;
50+
/** Which file extensions should be loaded. By default: .js, .ts */
51+
extensions?: string[];
52+
/** Regexp or custom function which determines should be loaded file/dir or not */
53+
include?: RegExp | ((path: string, kind: 'dir' | 'file', filename: string) => boolean);
54+
/** Regexp or custom function which determines should file/dir be skipped. It take precedence over `include` option. */
55+
exclude?: RegExp | ((path: string, kind: 'dir' | 'file', filename: string) => boolean);
56+
}
57+
58+
/**
59+
* Traverses directories and construct AST for your graphql entrypoints.
60+
*
61+
* @param m – is a NodeJS Module which provides a way to load modules from scanned dir in the regular nodejs way
62+
* @param options – set of options which helps to customize rules of what files/dirs should be loaded or not
63+
*/
5464
export function directoryToAst(
5565
m: NodeModule,
5666
options: DirectoryToAstOptions = defaultOptions

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ import { GraphQLSchema } from 'graphql';
55

66
export interface BuildOptions extends DirectoryToAstOptions, AstToSchemaOptions {}
77

8+
/**
9+
* Traverses directories and return GraphQLSchema instance from `graphql-js`.
10+
*
11+
* @param m – is a NodeJS Module which provides a way to load modules from scanned dir in the regular nodejs way
12+
* @param options – set of options which helps to customize rules of what files/dirs should be loaded or not
13+
*/
814
export function buildSchema(module: NodeModule, opts: BuildOptions = {}): GraphQLSchema {
915
return loadSchemaComposer(module, opts).buildSchema();
1016
}
1117

18+
/**
19+
* Traverses directories and return SchemaComposer instance from `graphql-compose`.
20+
*
21+
* @param m – is a NodeJS Module which provides a way to load modules from scanned dir in the regular nodejs way
22+
* @param options – set of options which helps to customize rules of what files/dirs should be loaded or not
23+
*/
1224
export function loadSchemaComposer<TContext = any>(
1325
module: NodeModule,
1426
opts: BuildOptions

src/typeDefs.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
11
import { ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
22

3+
/**
4+
* General type annotation for default export of your modules inside schema directory.
5+
*
6+
* @example schema/query/ping.ts
7+
* import { FieldConfig } from 'graphql-compose-modules';
8+
*
9+
* export default {
10+
* type: 'String',
11+
* resolve: () => 'pong',
12+
* } as FieldConfig;
13+
*/
314
export type FieldConfig<
415
TContext = any,
516
TArgs = any,
617
TSource = any
718
> = ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>;
819

20+
/**
21+
* Specific type annotation for index.ts files in you folders.
22+
* index.ts files have specific purpose to extend or override
23+
* automatically generated ObjectType for directory.
24+
*
25+
* For example you have the following structure in `schema` folder:
26+
* query/
27+
* viewer/
28+
* index.ts <--- will extend/override definition of `viewer/` type
29+
* articles.ts <--- just add a field with name `articles` to `viewer/` type
30+
*
31+
* So the next code extends logic of `viewer/` type
32+
* - provide custom name for generated type (instead of `Viewer` will be `AuthorizedUser`)
33+
* - if `context.user` is empty then return error in runtime to the client
34+
* @example query/viewer/index.ts
35+
* import { NamespaceConfig } from 'graphql-compose-modules';
36+
*
37+
* export default {
38+
* type: 'AuthorizedUser',
39+
* resolve: (parent, args, context, info) => {
40+
* if (!context.user) throw new Error('You should be authenticated user to access `viewer` field');
41+
* return {};
42+
* },
43+
* } as NamespaceConfig;
44+
*/
945
export type NamespaceConfig<TContext = any, TArgs = any, TSource = any> = Partial<
1046
ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>
1147
>;

0 commit comments

Comments
 (0)