Skip to content

Commit 2ac3ac5

Browse files
committed
fix: expose TypeScript definitions
1 parent 0c1c891 commit 2ac3ac5

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/astToSchema.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import { AstResult, AstRootTypeNode, AstDirNode, AstFileNode } from './directory
1313
import dedent from 'dedent';
1414
import { GraphQLObjectType } from 'graphql';
1515

16-
export interface AstOptions {
16+
export interface AstToSchemaOptions {
1717
schemaComposer?: SchemaComposer<any>;
1818
prefix?: string;
1919
suffix?: string;
2020
}
2121

2222
export function astToSchema<TContext = any>(
2323
ast: AstResult,
24-
opts: AstOptions = {}
24+
opts: AstToSchemaOptions = {}
2525
): SchemaComposer<TContext> {
2626
let sc: SchemaComposer<any>;
2727

@@ -49,7 +49,7 @@ function populateRoot(
4949
sc: SchemaComposer<any>,
5050
rootName: 'Query' | 'Mutation' | 'Subscription',
5151
astRootNode: AstRootTypeNode,
52-
opts?: AstOptions
52+
opts?: AstToSchemaOptions
5353
) {
5454
const tc = sc[rootName];
5555
Object.keys(astRootNode.children).forEach((key) => {
@@ -62,7 +62,7 @@ export function createFields(
6262
ast: AstDirNode | AstFileNode | void,
6363
parent: ObjectTypeComposer,
6464
pathPrefix: string,
65-
opts: AstOptions = {}
65+
opts: AstToSchemaOptions = {}
6666
): void {
6767
if (!ast) return;
6868

@@ -105,7 +105,11 @@ export function createFields(
105105
}
106106
}
107107

108-
function getTypename(ast: AstDirNode | AstFileNode, pathPrefix: string, opts: AstOptions): string {
108+
function getTypename(
109+
ast: AstDirNode | AstFileNode,
110+
pathPrefix: string,
111+
opts: AstToSchemaOptions
112+
): string {
109113
const name = ast.name;
110114

111115
let typename = pathPrefix;

src/directoryToAst.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import fs from 'fs';
22
import { join, resolve, dirname, basename } from 'path';
33

4-
export interface Options {
4+
export interface DirectoryToAstOptions {
55
relativePath?: string;
66
extensions?: string[];
77
include?: RegExp | ((path: string, kind: 'dir' | 'file', filename: string) => boolean);
88
exclude?: RegExp | ((path: string, kind: 'dir' | 'file', filename: string) => boolean);
99
}
1010

11-
type AstNodeKinds = 'rootType' | 'dir' | 'file';
11+
export type AstNodeKinds = 'rootType' | 'dir' | 'file';
1212

13-
interface AstBaseNode {
13+
export interface AstBaseNode {
1414
kind: AstNodeKinds;
1515
name: string;
1616
absPath: string;
@@ -45,11 +45,14 @@ export interface AstResult {
4545
subscription?: AstRootTypeNode;
4646
}
4747

48-
export const defaultOptions: Options = {
48+
export const defaultOptions: DirectoryToAstOptions = {
4949
extensions: ['js', 'ts'],
5050
};
5151

52-
export function directoryToAst(m: NodeModule, options: Options = defaultOptions): AstResult {
52+
export function directoryToAst(
53+
m: NodeModule,
54+
options: DirectoryToAstOptions = defaultOptions
55+
): AstResult {
5356
// if no path was passed in, assume the equivelant of __dirname from caller
5457
// otherwise, resolve path relative to the equivalent of __dirname
5558
const schemaPath = options?.relativePath
@@ -110,7 +113,7 @@ export function directoryToAst(m: NodeModule, options: Options = defaultOptions)
110113
export function getAstForDir(
111114
m: NodeModule,
112115
absPath: string,
113-
options: Options = defaultOptions
116+
options: DirectoryToAstOptions = defaultOptions
114117
): AstDirNode | void {
115118
const name = basename(absPath);
116119

@@ -162,7 +165,7 @@ export function getAstForDir(
162165
export function getAstForFile(
163166
m: NodeModule,
164167
absPath: string,
165-
options: Options = defaultOptions
168+
options: DirectoryToAstOptions = defaultOptions
166169
): AstFileNode | void {
167170
const filename = basename(absPath);
168171
if (absPath !== m.filename && checkInclusion(absPath, 'file', filename, options)) {
@@ -181,7 +184,7 @@ function checkInclusion(
181184
absPath: string,
182185
kind: 'dir' | 'file',
183186
filename: string,
184-
options: Options
187+
options: DirectoryToAstOptions
185188
): boolean {
186189
// Skip dir/files started from double underscore
187190
if (/^__.*/i.test(filename)) {

src/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { directoryToAst, Options } from './directoryToAst';
2-
import { astToSchema, AstOptions } from './astToSchema';
1+
import { directoryToAst, DirectoryToAstOptions } from './directoryToAst';
2+
import { astToSchema, AstToSchemaOptions } from './astToSchema';
33

4-
export interface BuildOptions extends Options, AstOptions {}
4+
export interface BuildOptions extends DirectoryToAstOptions, AstToSchemaOptions {}
55

66
export function buildSchema(module: NodeModule, opts: BuildOptions = {}) {
77
return loadSchemaComposer(module, opts).buildSchema();
@@ -13,5 +13,15 @@ export function loadSchemaComposer(module: NodeModule, opts: BuildOptions) {
1313
return sc;
1414
}
1515

16-
export { directoryToAst, astToSchema };
16+
export {
17+
directoryToAst,
18+
DirectoryToAstOptions,
19+
AstNodeKinds,
20+
AstBaseNode,
21+
AstRootTypeNode,
22+
AstDirNode,
23+
AstFileNode,
24+
AstResult,
25+
} from './directoryToAst';
26+
export { astToSchema, AstToSchemaOptions } from './astToSchema';
1727
export * from './testHelpers';

0 commit comments

Comments
 (0)