Skip to content

Commit 5b2100f

Browse files
committed
refactor: add AstRootNode node with children for better compatibility with visitor pattern
1 parent 2ac3ac5 commit 5b2100f

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

src/__tests__/__snapshots__/astToSchema-test.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type Mutation {
1414
}
1515
1616
type MutationAuth {
17-
login: Boolean
17+
# Login operation
18+
login(email: String, password: String): Boolean
1819
logout: Boolean
1920
nested: MutationAuthNested
2021
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export default {
22
type: 'Boolean',
3+
description: 'Login operation',
4+
args: { email: 'String', password: 'String' },
35
resolve: () => true,
46
};

src/__tests__/directoryToAst-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ describe('directoryToAst()', () => {
55
const ast = directoryToAst(module, { relativePath: './__testSchema__' });
66

77
it('should return root types', () => {
8-
expect(Object.keys(ast)).toEqual(expect.arrayContaining(['query', 'mutation']));
8+
expect(Object.keys(ast.children)).toEqual(expect.arrayContaining(['query', 'mutation']));
99
});
1010

1111
it('query has proper values', () => {
12-
expect(ast.query).toMatchSnapshot({
12+
expect(ast.children.query).toMatchSnapshot({
1313
name: 'query',
1414
kind: 'rootType',
1515
absPath: expect.any(String),
@@ -46,7 +46,7 @@ describe('directoryToAst()', () => {
4646
});
4747

4848
it('mutation has proper values', () => {
49-
expect(ast.mutation).toMatchSnapshot({
49+
expect(ast.children.mutation).toMatchSnapshot({
5050
name: 'mutation',
5151
kind: 'rootType',
5252
absPath: expect.any(String),

src/astToSchema.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
isSomeOutputTypeDefinitionString,
1010
inspect,
1111
} from 'graphql-compose';
12-
import { AstResult, AstRootTypeNode, AstDirNode, AstFileNode } from './directoryToAst';
12+
import { AstRootNode, AstRootTypeNode, AstDirNode, AstFileNode } from './directoryToAst';
1313
import dedent from 'dedent';
1414
import { GraphQLObjectType } from 'graphql';
1515

@@ -20,7 +20,7 @@ export interface AstToSchemaOptions {
2020
}
2121

2222
export function astToSchema<TContext = any>(
23-
ast: AstResult,
23+
ast: AstRootNode,
2424
opts: AstToSchemaOptions = {}
2525
): SchemaComposer<TContext> {
2626
let sc: SchemaComposer<any>;
@@ -38,9 +38,9 @@ export function astToSchema<TContext = any>(
3838
sc = new SchemaComposer();
3939
}
4040

41-
if (ast.query) populateRoot(sc, 'Query', ast.query, opts);
42-
if (ast.mutation) populateRoot(sc, 'Mutation', ast.mutation, opts);
43-
if (ast.subscription) populateRoot(sc, 'Subscription', ast.subscription, opts);
41+
if (ast.children.query) populateRoot(sc, 'Query', ast.children.query, opts);
42+
if (ast.children.mutation) populateRoot(sc, 'Mutation', ast.children.mutation, opts);
43+
if (ast.children.subscription) populateRoot(sc, 'Subscription', ast.children.subscription, opts);
4444

4545
return sc;
4646
}

src/directoryToAst.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface DirectoryToAstOptions {
88
exclude?: RegExp | ((path: string, kind: 'dir' | 'file', filename: string) => boolean);
99
}
1010

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

1313
export interface AstBaseNode {
1414
kind: AstNodeKinds;
@@ -39,10 +39,13 @@ export interface AstFileNode extends AstBaseNode {
3939
};
4040
}
4141

42-
export interface AstResult {
43-
query?: AstRootTypeNode;
44-
mutation?: AstRootTypeNode;
45-
subscription?: AstRootTypeNode;
42+
type RootTypeNames = 'query' | 'mutation' | 'subscription';
43+
44+
export interface AstRootNode extends AstBaseNode {
45+
kind: 'root';
46+
children: {
47+
[T in RootTypeNames]?: AstRootTypeNode;
48+
};
4649
}
4750

4851
export const defaultOptions: DirectoryToAstOptions = {
@@ -52,7 +55,7 @@ export const defaultOptions: DirectoryToAstOptions = {
5255
export function directoryToAst(
5356
m: NodeModule,
5457
options: DirectoryToAstOptions = defaultOptions
55-
): AstResult {
58+
): AstRootNode {
5659
// if no path was passed in, assume the equivelant of __dirname from caller
5760
// otherwise, resolve path relative to the equivalent of __dirname
5861
const schemaPath = options?.relativePath
@@ -66,7 +69,12 @@ export function directoryToAst(
6669
}
6770
});
6871

69-
const result = {} as AstResult;
72+
const result = {
73+
kind: 'root',
74+
name: basename(schemaPath),
75+
absPath: schemaPath,
76+
children: {},
77+
} as AstRootNode;
7078

7179
fs.readdirSync(schemaPath).forEach((filename) => {
7280
const absPath = join(schemaPath, filename);
@@ -76,8 +84,8 @@ export function directoryToAst(
7684
const re = /^(query|mutation|subscription)(\.(.*))?$/i;
7785
const found = dirName.match(re);
7886
if (found) {
79-
const opType = found[1].toLowerCase() as keyof AstResult;
80-
let rootTypeAst = result[opType];
87+
const opType = found[1].toLowerCase() as keyof AstRootNode['children'];
88+
let rootTypeAst = result.children[opType];
8189
if (!rootTypeAst)
8290
rootTypeAst = {
8391
kind: 'rootType',
@@ -101,7 +109,7 @@ export function directoryToAst(
101109
rootTypeAst.namespaceConfig = astDir.namespaceConfig;
102110
}
103111
}
104-
result[opType] = rootTypeAst;
112+
result.children[opType] = rootTypeAst;
105113
}
106114
}
107115
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export {
2121
AstRootTypeNode,
2222
AstDirNode,
2323
AstFileNode,
24-
AstResult,
24+
AstRootNode,
2525
} from './directoryToAst';
2626
export { astToSchema, AstToSchemaOptions } from './astToSchema';
2727
export * from './testHelpers';

0 commit comments

Comments
 (0)