Skip to content

Commit 930fa9f

Browse files
committed
feat: improve type definitions, expose FieldConfig & NamespaceConfig types
1 parent 485a284 commit 930fa9f

30 files changed

+99
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NamespaceConfig } from '../../../typeDefs';
2+
13
export default {
24
resolve: () => ({}),
3-
};
5+
} as NamespaceConfig;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
description: 'Login operation',
46
args: { email: 'String', password: 'String' },
57
resolve: () => true,
6-
};
8+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NamespaceConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'NamespaceCustomTypeName',
3-
};
5+
} as NamespaceConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: (sc) => sc.get('Boolean'),
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: 'String',
35
resolve: () => 'text',
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'String',
35
description: 'This file should be skipped',
46
resolve: () => 'ok',
5-
};
7+
} as FieldConfig;
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'String',
35
resolve: () => 'ok',
4-
};
6+
} as FieldConfig;
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NamespaceConfig } from '../../../typeDefs';
2+
13
export default {
24
resolve: () => ({}),
3-
};
5+
} as NamespaceConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: 'Boolean',
35
resolve: () => true,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { NamespaceConfig } from '../../../../typeDefs';
2+
13
export default {
24
args: {
35
arg: 'Int',
46
},
5-
resolve: (_: any, __: any, context: any) => {
7+
resolve: (_, __, context) => {
68
if (!context?.isAdmin) throw new Error('You should be the ADMIN');
79
return {};
810
},
9-
};
11+
} as NamespaceConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: 'String',
35
resolve: () => 'nodkz',
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: `
35
type SomeIndexFileType {
46
awesomeValue: String
57
}
68
`,
79
resolve: () => ({ awesomeValue: 'awesomeValue' }),
8-
};
10+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../typeDefs';
2+
13
export default {
24
type: 'Int',
35
resolve: () => 123,
4-
};
6+
} as FieldConfig;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: `
35
type UserExtendedData {
46
starsCount: Int
57
}
68
`,
79
resolve: () => ({ starsCount: 10 }),
8-
};
10+
} as FieldConfig;

src/__tests__/__testSchema__/query/user/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NamespaceConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: `
35
type UserAwesomeType {
@@ -12,4 +14,4 @@ export default {
1214
lastName: 'User',
1315
};
1416
},
15-
};
17+
} as NamespaceConfig;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FieldConfig } from '../../../../typeDefs';
2+
13
export default {
24
type: ['String'],
35
resolve: () => ['ADMIN', 'USER'],
4-
};
6+
} as FieldConfig;

src/__tests__/astVisitor-test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ describe('astVisitor', () => {
177177
}
178178
},
179179
FILE: (node) => {
180-
const currentResolve = node.code.default.resolve;
180+
const currentResolve = node.code.default?.resolve;
181181
if (currentResolve) {
182-
const description = node.code.default.description;
183-
node.code.default.resolve = (s: any, a: any, c: any, i: any) => {
182+
const description = node.code.default?.description;
183+
(node.code as any).default.resolve = (s: any, a: any, c: any, i: any) => {
184184
logs.push({
185185
description,
186186
args: a,

src/astToSchema.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import {
88
isComposeOutputType,
99
isSomeOutputTypeDefinitionString,
1010
inspect,
11+
isFunction,
1112
} from 'graphql-compose';
1213
import { AstRootNode, AstRootTypeNode, AstDirNode, AstFileNode } from './directoryToAst';
1314
import dedent from 'dedent';
1415
import { GraphQLObjectType } from 'graphql';
16+
import { FieldConfig, NamespaceConfig } from './typeDefs';
1517

1618
export interface AstToSchemaOptions {
1719
schemaComposer?: SchemaComposer<any>;
@@ -29,7 +31,7 @@ export function astToSchema<TContext = any>(
2931
if (!opts.schemaComposer) {
3032
throw new Error(dedent`
3133
Provided option 'schemaComposer' should be an instance of SchemaComposer class from 'graphql-compose' package.
32-
Recieved:
34+
Received:
3335
${inspect(opts.schemaComposer)}
3436
`);
3537
}
@@ -77,7 +79,7 @@ export function createFields(
7779

7880
if (ast.kind === 'file') {
7981
parent.addNestedFields({
80-
[name]: prepareFieldConfig(sc, ast),
82+
[name]: prepareFieldConfig(sc, ast) as FieldConfig,
8183
});
8284
return;
8385
}
@@ -190,8 +192,8 @@ function prepareNamespaceFieldConfig(
190192
function prepareFieldConfig(
191193
sc: SchemaComposer<any>,
192194
ast: AstFileNode
193-
): ObjectTypeComposerFieldConfig<any, any> {
194-
const fc = ast.code.default as any;
195+
): FieldConfig | NamespaceConfig {
196+
const fc = ast.code.default;
195197

196198
if (!fc) {
197199
throw new Error(dedent`

src/astVisitor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function visitNode(
5757
node: AstDirNode | AstFileNode | AstRootTypeNode,
5858
visitor: AstVisitor,
5959
info: VisitInfo
60-
) {
60+
): void {
6161
let result: VisitorEmptyResult | AstDirNode | AstFileNode | AstRootTypeNode;
6262
if (node.kind === 'dir') {
6363
if (visitor.DIR) result = visitor.DIR(node, info);

src/directoryToAst.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import { join, resolve, dirname, basename } from 'path';
3+
import { FieldConfig, NamespaceConfig } from './typeDefs';
34

45
export interface DirectoryToAstOptions {
56
relativePath?: string;
@@ -35,7 +36,7 @@ export interface AstDirNode extends AstBaseNode {
3536
export interface AstFileNode extends AstBaseNode {
3637
kind: 'file';
3738
code: {
38-
default?: any;
39+
default?: FieldConfig | NamespaceConfig;
3940
};
4041
}
4142

src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { directoryToAst, DirectoryToAstOptions } from './directoryToAst';
22
import { astToSchema, AstToSchemaOptions } from './astToSchema';
3+
import { SchemaComposer } from 'graphql-compose';
4+
import { GraphQLSchema } from 'graphql';
35

46
export interface BuildOptions extends DirectoryToAstOptions, AstToSchemaOptions {}
57

6-
export function buildSchema(module: NodeModule, opts: BuildOptions = {}) {
8+
export function buildSchema(module: NodeModule, opts: BuildOptions = {}): GraphQLSchema {
79
return loadSchemaComposer(module, opts).buildSchema();
810
}
911

10-
export function loadSchemaComposer(module: NodeModule, opts: BuildOptions) {
12+
export function loadSchemaComposer<TContext = any>(
13+
module: NodeModule,
14+
opts: BuildOptions
15+
): SchemaComposer<TContext> {
1116
const ast = directoryToAst(module, opts);
1217
const sc = astToSchema(ast, opts);
1318
return sc;
@@ -25,6 +30,7 @@ export {
2530
} from './directoryToAst';
2631
export { astToSchema, AstToSchemaOptions } from './astToSchema';
2732
export * from './testHelpers';
33+
export * from './typeDefs';
2834

2935
export {
3036
astVisitor,

src/testHelpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function testSDL(opts: {
128128
fc: ObjectTypeComposerFieldConfigAsObjectDefinition<any, any, any> | Resolver;
129129
schemaComposer?: SchemaComposer<any>;
130130
deep?: boolean;
131-
}) {
131+
}): string {
132132
const sc = opts.schemaComposer || new SchemaComposer();
133133
sc.Query.setField(FIELD, opts.fc);
134134
sc.buildSchema();

src/typeDefs.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
2+
3+
export type FieldConfig<TContext = any, TArgs = any, TSource = any> =
4+
ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>;
5+
6+
export type NamespaceConfig<TContext = any, TArgs = any, TSource = any> = Partial<
7+
ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>
8+
>;

0 commit comments

Comments
 (0)