Skip to content

Commit 736c6d9

Browse files
committed
feat(schema): unify generateFromMetadata and check schema synchronously
1 parent df3b4d2 commit 736c6d9

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66
- **Breaking Change**: `AuthChecker` type is now "function or class" - update to `AuthCheckerFn` if the function form is needed in the code
77
- **Breaking Change**: update `graphql-js` peer dependency to `^16.6.0`
8+
- **Breaking Change**: `buildSchemaSync` is now also checking the generated schema for errors
89
- support class-based auth checker, which allows for dependency injection
910
- allow defining directives for interface types and theirs fields, with inheritance for object types fields (#744)
1011
- allow deprecating input fields and args (#794)

src/schema/schema-generator.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
GraphQLInputType,
1010
GraphQLInputFieldConfigMap,
1111
GraphQLInterfaceType,
12-
graphql,
12+
graphqlSync,
1313
getIntrospectionQuery,
1414
GraphQLEnumType,
1515
GraphQLEnumValueConfigMap,
@@ -108,18 +108,7 @@ export abstract class SchemaGenerator {
108108
private static unionTypesInfo: UnionTypeInfo[] = [];
109109
private static usedInterfaceTypes = new Set<Function>();
110110

111-
static async generateFromMetadata(options: SchemaGeneratorOptions): Promise<GraphQLSchema> {
112-
const schema = this.generateFromMetadataSync(options);
113-
if (!options.skipCheck) {
114-
const { errors } = await graphql({ schema, source: getIntrospectionQuery() });
115-
if (errors) {
116-
throw new GeneratingSchemaError(errors);
117-
}
118-
}
119-
return schema;
120-
}
121-
122-
static generateFromMetadataSync(options: SchemaGeneratorOptions): GraphQLSchema {
111+
static generateFromMetadata(options: SchemaGeneratorOptions): GraphQLSchema {
123112
this.checkForErrors(options);
124113
BuildContext.create(options);
125114
getMetadataStorage().build(options);
@@ -140,6 +129,14 @@ export abstract class SchemaGenerator {
140129

141130
BuildContext.reset();
142131
this.usedInterfaceTypes = new Set<Function>();
132+
133+
if (!options.skipCheck) {
134+
const { errors } = graphqlSync({ schema: finalSchema, source: getIntrospectionQuery() });
135+
if (errors) {
136+
throw new GeneratingSchemaError(errors);
137+
}
138+
}
139+
143140
return finalSchema;
144141
}
145142

src/utils/buildSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface BuildSchemaOptions extends Omit<SchemaGeneratorOptions, "resolv
2727

2828
export async function buildSchema(options: BuildSchemaOptions): Promise<GraphQLSchema> {
2929
const resolvers = loadResolvers(options);
30-
const schema = await SchemaGenerator.generateFromMetadata({ ...options, resolvers });
30+
const schema = SchemaGenerator.generateFromMetadata({ ...options, resolvers });
3131
if (options.emitSchemaFile) {
3232
const { schemaFileName, printSchemaOptions } = getEmitSchemaDefinitionFileOptions(options);
3333
await emitSchemaDefinitionFile(schemaFileName, schema, printSchemaOptions);
@@ -37,7 +37,7 @@ export async function buildSchema(options: BuildSchemaOptions): Promise<GraphQLS
3737

3838
export function buildSchemaSync(options: BuildSchemaOptions): GraphQLSchema {
3939
const resolvers = loadResolvers(options);
40-
const schema = SchemaGenerator.generateFromMetadataSync({ ...options, resolvers });
40+
const schema = SchemaGenerator.generateFromMetadata({ ...options, resolvers });
4141
if (options.emitSchemaFile) {
4242
const { schemaFileName, printSchemaOptions } = getEmitSchemaDefinitionFileOptions(options);
4343
emitSchemaDefinitionFileSync(schemaFileName, schema, printSchemaOptions);

0 commit comments

Comments
 (0)