Skip to content

Commit 75255e1

Browse files
committed
fix: allow to provide Options in buildSchema(module, opts) method
1 parent 1fc8867 commit 75255e1

File tree

7 files changed

+51
-20
lines changed

7 files changed

+51
-20
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
"javascript.validate.enable": false,
44
"javascript.autoClosingTags": false,
55
"eslint.autoFixOnSave": true,
6+
"typescript.tsdk": "node_modules/typescript/lib",
67
}

examples/forTests/schema/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
import { buildSchema } from '../../../src';
2+
import { SchemaComposer } from 'graphql-compose';
23

3-
export const schema = buildSchema(module);
4+
const schemaComposer = new SchemaComposer();
5+
schemaComposer.Query.addFields({
6+
time: {
7+
type: 'String',
8+
resolve: () => Date.now(),
9+
},
10+
});
11+
12+
export const schema = buildSchema(module, { schemaComposer });

src/__tests__/requireAstToSchema-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { printSchema } from 'graphql/utilities';
55

66
describe('requireAstToSchema()', () => {
77
describe('Schema ../../examples/forTests/schema', () => {
8-
const ast = requireSchemaDirectory(module, '../../examples/forTests/schema');
8+
const ast = requireSchemaDirectory(module, { relativePath: '../../examples/forTests/schema' });
99
const sc = requireAstToSchema(ast);
1010

1111
it('should return schema composer', () => {

src/__tests__/requireSchemaDirectory-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { requireSchemaDirectory } from '../requireSchemaDirectory';
22

33
describe('requireSchemaDirectory()', () => {
44
describe('Schema ../../examples/forTests/schema', () => {
5-
const ast = requireSchemaDirectory(module, '../../examples/forTests/schema');
5+
const ast = requireSchemaDirectory(module, { relativePath: '../../examples/forTests/schema' });
66

77
it('should return root types', () => {
88
expect(Object.keys(ast)).toEqual(expect.arrayContaining(['query', 'mutation']));

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { requireSchemaDirectory } from './requireSchemaDirectory';
2-
import { requireAstToSchema } from './requireAstToSchema';
1+
import { requireSchemaDirectory, RequireOptions } from './requireSchemaDirectory';
2+
import { requireAstToSchema, AstOptions } from './requireAstToSchema';
33

4-
export function buildSchema(module: NodeModule) {
5-
return loadSchemaComposer(module).buildSchema();
4+
export interface BuildOptions extends RequireOptions, AstOptions {}
5+
6+
export function buildSchema(module: NodeModule, opts: BuildOptions = {}) {
7+
return loadSchemaComposer(module, opts).buildSchema();
68
}
79

8-
export function loadSchemaComposer(module: NodeModule) {
9-
const ast = requireSchemaDirectory(module);
10-
const sc = requireAstToSchema(ast);
10+
export function loadSchemaComposer(module: NodeModule, opts: BuildOptions) {
11+
const ast = requireSchemaDirectory(module, opts);
12+
const sc = requireAstToSchema(ast, opts);
1113
return sc;
1214
}
1315

src/requireAstToSchema.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,28 @@ import {
1818
import dedent from 'dedent';
1919
import { GraphQLObjectType } from 'graphql';
2020

21+
export interface AstOptions {
22+
schemaComposer?: SchemaComposer<any>;
23+
}
24+
2125
export function requireAstToSchema<TContext = any>(
2226
ast: RequireAstResult,
23-
schemaComposer?: SchemaComposer<TContext>
27+
opts: AstOptions = {}
2428
): SchemaComposer<TContext> {
25-
const sc = schemaComposer || new SchemaComposer<TContext>();
29+
let sc: SchemaComposer<any>;
30+
31+
if (opts?.schemaComposer) {
32+
if (!opts.schemaComposer) {
33+
throw new Error(dedent`
34+
Provided option 'schemaComposer' should be an instance of SchemaComposer class from 'graphql-compose' package.
35+
Recieved:
36+
${inspect(opts.schemaComposer)}
37+
`);
38+
}
39+
sc = opts.schemaComposer;
40+
} else {
41+
sc = new SchemaComposer();
42+
}
2643

2744
if (ast.query) populateRoot(sc, 'Query', ast.query);
2845
if (ast.mutation) populateRoot(sc, 'Mutation', ast.mutation);

src/requireSchemaDirectory.ts

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

4-
interface Options {
5-
extensions: string[];
4+
export interface RequireOptions {
5+
relativePath?: string;
6+
extensions?: string[];
67
include?: RegExp | ((path: string, filename: string) => boolean);
78
exclude?: RegExp | ((path: string, filename: string) => boolean);
89
}
@@ -42,18 +43,19 @@ export interface RequireAstResult {
4243
subscription?: RequireAstRootTypeNode;
4344
}
4445

45-
export const defaultOptions: Options = {
46+
export const defaultOptions: RequireOptions = {
4647
extensions: ['js', 'ts'],
4748
};
4849

4950
export function requireSchemaDirectory(
5051
m: NodeModule,
51-
path?: string,
52-
options: Options = defaultOptions
52+
options: RequireOptions = defaultOptions
5353
): RequireAstResult {
5454
// if no path was passed in, assume the equivelant of __dirname from caller
5555
// otherwise, resolve path relative to the equivalent of __dirname
56-
const schemaPath = !path ? dirname(m.filename) : resolve(dirname(m.filename), path);
56+
const schemaPath = options?.relativePath
57+
? resolve(dirname(m.filename), options.relativePath)
58+
: dirname(m.filename);
5759

5860
const result = {} as RequireAstResult;
5961

@@ -94,7 +96,7 @@ export function requireSchemaDirectory(
9496
export function requireSubDirectory(
9597
m: NodeModule,
9698
path: string,
97-
options: Options = defaultOptions
99+
options: RequireOptions = defaultOptions
98100
): RequireAstDirNode {
99101
const result: RequireAstDirNode = {
100102
kind: 'dir',
@@ -153,7 +155,7 @@ function skipName(filename: string): boolean {
153155
return /^__.*/i.test(filename);
154156
}
155157

156-
function checkFileInclusion(absPath: string, filename: string, options: Options): boolean {
158+
function checkFileInclusion(absPath: string, filename: string, options: RequireOptions): boolean {
157159
return (
158160
// verify file has valid extension
159161
new RegExp('\\.(' + options.extensions.join('|') + ')$', 'i').test(filename) &&

0 commit comments

Comments
 (0)