Skip to content

Commit 27d139f

Browse files
committed
feat: add namespaceConfig to AstDirNode which populates only by index.ts file in current folder. Other occurrences field.index, dir.index are perceive like a regular sub-field with index name.
1 parent 2a9d58a commit 27d139f

File tree

7 files changed

+42
-49
lines changed

7 files changed

+42
-49
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ type QueryMeAddress {
6464
}
6565
6666
type QuerySome {
67+
index: SomeIndexFileType
6768
nested: Int
68-
type: SomeIndexFileType
6969
}
7070
7171
type SomeIndexFileType {

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ Object {
77
"auth": Object {
88
"absPath": Any<String>,
99
"children": Object {
10-
"index": ObjectContaining {
11-
"kind": "file",
12-
},
1310
"login": ObjectContaining {
1411
"kind": "file",
1512
},
@@ -22,6 +19,9 @@ Object {
2219
},
2320
"kind": "dir",
2421
"name": "auth",
22+
"namespaceConfig": ObjectContaining {
23+
"kind": "file",
24+
},
2525
},
2626
"logs.nested": Object {
2727
"absPath": Any<String>,
@@ -62,12 +62,6 @@ Object {
6262
"field": ObjectContaining {
6363
"kind": "file",
6464
},
65-
"index": Object {
66-
"absPath": Any<String>,
67-
"code": Any<Object>,
68-
"kind": "file",
69-
"name": "index",
70-
},
7165
"me": Object {
7266
"absPath": Any<String>,
7367
"children": Object {
@@ -77,35 +71,36 @@ Object {
7771
"address.street": ObjectContaining {
7872
"kind": "file",
7973
},
80-
"index": Object {
81-
"absPath": Any<String>,
82-
"code": Any<Object>,
83-
"kind": "file",
84-
"name": "index",
85-
},
8674
"name": ObjectContaining {
8775
"kind": "file",
8876
},
8977
},
9078
"kind": "dir",
9179
"name": "me",
80+
"namespaceConfig": Object {
81+
"absPath": Any<String>,
82+
"code": Any<Object>,
83+
"kind": "file",
84+
"name": "index",
85+
},
9286
},
87+
"some.index": Any<Object>,
9388
"some.nested": ObjectContaining {
9489
"kind": "file",
9590
},
96-
"some.type.index": Any<Object>,
9791
"user": Object {
9892
"absPath": Any<String>,
9993
"children": Object {
10094
"extendedData": Any<Object>,
101-
"index": Any<Object>,
10295
"roles": Any<Object>,
10396
},
10497
"kind": "dir",
10598
"name": "user",
99+
"namespaceConfig": Any<Object>,
106100
},
107101
},
108102
"kind": "rootType",
109103
"name": "query",
104+
"namespaceConfig": Any<Object>,
110105
}
111106
`;
File renamed without changes.

src/__tests__/astToSchema-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ describe('astToSchema()', () => {
5050
expect(sc.Query.getFieldOTC('some').getFieldTypeName('nested')).toBe('Int');
5151
});
5252

53-
it('file: query/some.type.index.ts', () => {
53+
it('file: query/some.index.ts should be as regular field', () => {
5454
createFields(
5555
sc,
56-
getAstForFile(module, path.resolve(__dirname, './__testSchema__/query/some.type.index.ts')),
56+
getAstForFile(module, path.resolve(__dirname, './__testSchema__/query/some.index.ts')),
5757
sc.Query,
5858
'Query'
5959
);
6060
expect(sc.Query.hasField('some')).toBeTruthy();
6161
expect(sc.Query.getFieldTypeName('some')).toBe('QuerySome');
62-
expect(sc.Query.getFieldOTC('some').hasField('type')).toBeTruthy();
63-
expect(sc.Query.getFieldOTC('some').getFieldTypeName('type')).toBe('SomeIndexFileType');
64-
expect((sc.Query.getFieldOTC('some').getFieldConfig('type') as any).resolve()).toEqual({
62+
expect(sc.Query.getFieldOTC('some').hasField('index')).toBeTruthy();
63+
expect(sc.Query.getFieldOTC('some').getFieldTypeName('index')).toBe('SomeIndexFileType');
64+
expect((sc.Query.getFieldOTC('some').getFieldConfig('index') as any).resolve()).toEqual({
6565
awesomeValue: 'awesomeValue',
6666
});
6767
});

src/__tests__/directoryToAst-test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ describe('directoryToAst()', () => {
2222
'address.city': expect.objectContaining({ kind: 'file' }),
2323
'address.street': expect.objectContaining({ kind: 'file' }),
2424
name: expect.objectContaining({ kind: 'file' }),
25-
index: {
26-
kind: 'file',
27-
name: 'index',
28-
absPath: expect.any(String),
29-
code: expect.any(Object),
30-
},
25+
},
26+
namespaceConfig: {
27+
kind: 'file',
28+
name: 'index',
29+
absPath: expect.any(String),
30+
code: expect.any(Object),
3131
},
3232
},
33-
index: { kind: 'file', absPath: expect.any(String), code: expect.any(Object) },
3433
'some.nested': expect.objectContaining({ kind: 'file' }),
35-
'some.type.index': expect.any(Object),
34+
'some.index': expect.any(Object),
3635
user: {
3736
absPath: expect.any(String),
3837
children: {
3938
extendedData: expect.any(Object),
40-
index: expect.any(Object),
4139
roles: expect.any(Object),
4240
},
41+
namespaceConfig: expect.any(Object),
4342
},
4443
},
44+
namespaceConfig: expect.any(Object),
4545
});
4646
});
4747

@@ -54,11 +54,11 @@ describe('directoryToAst()', () => {
5454
auth: {
5555
absPath: expect.any(String),
5656
children: {
57-
index: expect.objectContaining({ kind: 'file' }),
5857
login: expect.objectContaining({ kind: 'file' }),
5958
logout: expect.objectContaining({ kind: 'file' }),
6059
nested: expect.objectContaining({ kind: 'dir' }),
6160
},
61+
namespaceConfig: expect.objectContaining({ kind: 'file' }),
6262
},
6363
user: {
6464
absPath: expect.any(String),

src/astToSchema.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,17 @@ export function createFields(
7676
}
7777

7878
if (ast.kind === 'file') {
79-
if (name !== 'index') {
80-
if (name.endsWith('.index')) {
81-
const fieldName = name.slice(0, -6); // remove ".index" from field name
82-
parent.addNestedFields({
83-
[fieldName]: prepareNamespaceFieldConfig(sc, ast, getTypename(ast, pathPrefix, opts)),
84-
});
85-
} else {
86-
parent.addNestedFields({
87-
[name]: prepareFieldConfig(sc, ast),
88-
});
89-
}
90-
}
79+
parent.addNestedFields({
80+
[name]: prepareFieldConfig(sc, ast),
81+
});
9182
return;
9283
}
9384

9485
if (ast.kind === 'dir') {
9586
const typename = getTypename(ast, pathPrefix, opts);
9687
let fc: ObjectTypeComposerFieldConfig<any, any>;
97-
if (ast.children['index'] && ast.children['index'].kind === 'file') {
98-
fc = prepareNamespaceFieldConfig(sc, ast.children['index'], typename);
88+
if (ast.namespaceConfig) {
89+
fc = prepareNamespaceFieldConfig(sc, ast.namespaceConfig, typename);
9990
} else {
10091
fc = { type: sc.createObjectTC(typename) };
10192
}

src/directoryToAst.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ export interface AstRootTypeNode extends AstBaseNode {
2121
children: {
2222
[key: string]: AstDirNode | AstFileNode;
2323
};
24+
namespaceConfig?: AstFileNode;
2425
}
2526

2627
export interface AstDirNode extends AstBaseNode {
2728
kind: 'dir';
2829
children: {
2930
[key: string]: AstDirNode | AstFileNode;
3031
};
32+
namespaceConfig?: AstFileNode;
3133
}
3234

3335
export interface AstFileNode extends AstBaseNode {
@@ -92,6 +94,9 @@ export function directoryToAst(m: NodeModule, options: Options = defaultOptions)
9294
};
9395
} else {
9496
rootTypeAst.children = astDir.children;
97+
if (astDir.namespaceConfig) {
98+
rootTypeAst.namespaceConfig = astDir.namespaceConfig;
99+
}
95100
}
96101
result[opType] = rootTypeAst;
97102
}
@@ -138,7 +143,9 @@ export function getAstForDir(
138143
// this node is a file
139144
const fileAst = getAstForFile(m, absFilePath, options);
140145
if (fileAst) {
141-
if (result.children[fileAst.name]) {
146+
if (fileAst.name === 'index') {
147+
result.namespaceConfig = fileAst;
148+
} else if (result.children[fileAst.name]) {
142149
throw new Error(
143150
`You have a folder and file with same name "${fileAst.name}" by the following path ${absPath}. Please remove one of them.`
144151
);
@@ -159,7 +166,7 @@ export function getAstForFile(
159166
): AstFileNode | void {
160167
const filename = basename(absPath);
161168
if (absPath !== m.filename && checkInclusion(absPath, 'file', filename, options)) {
162-
// hash node key shouldn't include file extension
169+
// module name shouldn't include file extension
163170
const moduleName = filename.substring(0, filename.lastIndexOf('.'));
164171
return {
165172
kind: 'file',

0 commit comments

Comments
 (0)