Skip to content

Commit

Permalink
feat: improve type definitions, expose FieldConfig & `NamespaceConf…
Browse files Browse the repository at this point in the history
…ig` types
  • Loading branch information
nodkz committed May 11, 2021
1 parent 485a284 commit 930fa9f
Show file tree
Hide file tree
Showing 30 changed files with 99 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/mutation.auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NamespaceConfig } from '../../../typeDefs';

export default {
resolve: () => ({}),
};
} as NamespaceConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/mutation.auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: 'Boolean',
description: 'Login operation',
args: { email: 'String', password: 'String' },
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/mutation.auth/logout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/mutation.auth/nested/method.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/mutation.user/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/mutation.user/update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/mutation/logs.nested/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query.auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NamespaceConfig } from '../../../typeDefs';

export default {
type: 'NamespaceCustomTypeName',
};
} as NamespaceConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query.auth/isLoggedIn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: (sc) => sc.get('Boolean'),
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query.auth/nested/method.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/__skip/field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: 'String',
resolve: () => 'text',
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/field.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: 'String',
description: 'This file should be skipped',
resolve: () => 'ok',
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: 'String',
resolve: () => 'ok',
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NamespaceConfig } from '../../../typeDefs';

export default {
resolve: () => ({}),
};
} as NamespaceConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/me/address.city.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/me/address.street.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: 'Boolean',
resolve: () => true,
};
} as FieldConfig;
6 changes: 4 additions & 2 deletions src/__tests__/__testSchema__/query/me/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { NamespaceConfig } from '../../../../typeDefs';

export default {
args: {
arg: 'Int',
},
resolve: (_: any, __: any, context: any) => {
resolve: (_, __, context) => {
if (!context?.isAdmin) throw new Error('You should be the ADMIN');
return {};
},
};
} as NamespaceConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/me/name.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: 'String',
resolve: () => 'nodkz',
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/some.index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: `
type SomeIndexFileType {
awesomeValue: String
}
`,
resolve: () => ({ awesomeValue: 'awesomeValue' }),
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/some.nested.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../typeDefs';

export default {
type: 'Int',
resolve: () => 123,
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/user/extendedData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: `
type UserExtendedData {
starsCount: Int
}
`,
resolve: () => ({ starsCount: 10 }),
};
} as FieldConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NamespaceConfig } from '../../../../typeDefs';

export default {
type: `
type UserAwesomeType {
Expand All @@ -12,4 +14,4 @@ export default {
lastName: 'User',
};
},
};
} as NamespaceConfig;
4 changes: 3 additions & 1 deletion src/__tests__/__testSchema__/query/user/roles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FieldConfig } from '../../../../typeDefs';

export default {
type: ['String'],
resolve: () => ['ADMIN', 'USER'],
};
} as FieldConfig;
6 changes: 3 additions & 3 deletions src/__tests__/astVisitor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ describe('astVisitor', () => {
}
},
FILE: (node) => {
const currentResolve = node.code.default.resolve;
const currentResolve = node.code.default?.resolve;
if (currentResolve) {
const description = node.code.default.description;
node.code.default.resolve = (s: any, a: any, c: any, i: any) => {
const description = node.code.default?.description;
(node.code as any).default.resolve = (s: any, a: any, c: any, i: any) => {
logs.push({
description,
args: a,
Expand Down
10 changes: 6 additions & 4 deletions src/astToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
isComposeOutputType,
isSomeOutputTypeDefinitionString,
inspect,
isFunction,
} from 'graphql-compose';
import { AstRootNode, AstRootTypeNode, AstDirNode, AstFileNode } from './directoryToAst';
import dedent from 'dedent';
import { GraphQLObjectType } from 'graphql';
import { FieldConfig, NamespaceConfig } from './typeDefs';

export interface AstToSchemaOptions {
schemaComposer?: SchemaComposer<any>;
Expand All @@ -29,7 +31,7 @@ export function astToSchema<TContext = any>(
if (!opts.schemaComposer) {
throw new Error(dedent`
Provided option 'schemaComposer' should be an instance of SchemaComposer class from 'graphql-compose' package.
Recieved:
Received:
${inspect(opts.schemaComposer)}
`);
}
Expand Down Expand Up @@ -77,7 +79,7 @@ export function createFields(

if (ast.kind === 'file') {
parent.addNestedFields({
[name]: prepareFieldConfig(sc, ast),
[name]: prepareFieldConfig(sc, ast) as FieldConfig,
});
return;
}
Expand Down Expand Up @@ -190,8 +192,8 @@ function prepareNamespaceFieldConfig(
function prepareFieldConfig(
sc: SchemaComposer<any>,
ast: AstFileNode
): ObjectTypeComposerFieldConfig<any, any> {
const fc = ast.code.default as any;
): FieldConfig | NamespaceConfig {
const fc = ast.code.default;

if (!fc) {
throw new Error(dedent`
Expand Down
2 changes: 1 addition & 1 deletion src/astVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function visitNode(
node: AstDirNode | AstFileNode | AstRootTypeNode,
visitor: AstVisitor,
info: VisitInfo
) {
): void {
let result: VisitorEmptyResult | AstDirNode | AstFileNode | AstRootTypeNode;
if (node.kind === 'dir') {
if (visitor.DIR) result = visitor.DIR(node, info);
Expand Down
3 changes: 2 additions & 1 deletion src/directoryToAst.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import { join, resolve, dirname, basename } from 'path';
import { FieldConfig, NamespaceConfig } from './typeDefs';

export interface DirectoryToAstOptions {
relativePath?: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ export interface AstDirNode extends AstBaseNode {
export interface AstFileNode extends AstBaseNode {
kind: 'file';
code: {
default?: any;
default?: FieldConfig | NamespaceConfig;
};
}

Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { directoryToAst, DirectoryToAstOptions } from './directoryToAst';
import { astToSchema, AstToSchemaOptions } from './astToSchema';
import { SchemaComposer } from 'graphql-compose';
import { GraphQLSchema } from 'graphql';

export interface BuildOptions extends DirectoryToAstOptions, AstToSchemaOptions {}

export function buildSchema(module: NodeModule, opts: BuildOptions = {}) {
export function buildSchema(module: NodeModule, opts: BuildOptions = {}): GraphQLSchema {
return loadSchemaComposer(module, opts).buildSchema();
}

export function loadSchemaComposer(module: NodeModule, opts: BuildOptions) {
export function loadSchemaComposer<TContext = any>(
module: NodeModule,
opts: BuildOptions
): SchemaComposer<TContext> {
const ast = directoryToAst(module, opts);
const sc = astToSchema(ast, opts);
return sc;
Expand All @@ -25,6 +30,7 @@ export {
} from './directoryToAst';
export { astToSchema, AstToSchemaOptions } from './astToSchema';
export * from './testHelpers';
export * from './typeDefs';

export {
astVisitor,
Expand Down
2 changes: 1 addition & 1 deletion src/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function testSDL(opts: {
fc: ObjectTypeComposerFieldConfigAsObjectDefinition<any, any, any> | Resolver;
schemaComposer?: SchemaComposer<any>;
deep?: boolean;
}) {
}): string {
const sc = opts.schemaComposer || new SchemaComposer();
sc.Query.setField(FIELD, opts.fc);
sc.buildSchema();
Expand Down
8 changes: 8 additions & 0 deletions src/typeDefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';

export type FieldConfig<TContext = any, TArgs = any, TSource = any> =
ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>;

export type NamespaceConfig<TContext = any, TArgs = any, TSource = any> = Partial<
ObjectTypeComposerFieldConfigAsObjectDefinition<TSource, TContext, TArgs>
>;

0 comments on commit 930fa9f

Please sign in to comment.