diff --git a/src/builder.ts b/src/builder.ts index 06088607..b23c3302 100644 --- a/src/builder.ts +++ b/src/builder.ts @@ -504,9 +504,6 @@ export class SchemaBuilder { /** All Schema Directives */ // private schemaDirectives: GraphQLDirective[] = [] - /** Whether we have used any custom directives within the schema construction */ - private hasSDLDirectives: boolean = false - /** All types that need to be traversed for children types */ private typesToWalk: TypeToWalk[] = [] @@ -999,14 +996,14 @@ export class SchemaBuilder { this.beforeBuildTypes() this.checkForInterfaceCircularDependencies() this.buildNexusTypes() + return { finalConfig: this.config, typeMap: this.finalTypeMap, - schemaExtension: this.schemaExtension!, + schemaExtension: this.schemaExtension, missingTypes: this.missingTypes, onAfterBuildFns: this.onAfterBuildFns, customDirectives: this.directivesMap, - hasSDLDirectives: this.hasSDLDirectives, schemaDirectives: this.maybeAddDirectiveUses('SCHEMA', this.config.schemaDirectives), } } @@ -1200,11 +1197,7 @@ export class SchemaBuilder { for (const directive of directives) { this.addDirective(directive) } - const result = maybeAddDirectiveUses(kind, directives, this.directivesMap) - if (result) { - this.hasSDLDirectives = true - } - return result + return maybeAddDirectiveUses(kind, directives, this.directivesMap) } private buildEnumType(config: NexusEnumTypeConfig) { @@ -1901,7 +1894,6 @@ export interface BuildTypes schemaExtension: NexusSchemaExtension onAfterBuildFns: SchemaBuilder['onAfterBuildFns'] customDirectives: Record - hasSDLDirectives: boolean schemaDirectives?: Partial<{ astNode: ASTKindToNode['SchemaDefinition'] }> } diff --git a/src/makeSchema.ts b/src/makeSchema.ts index b623d587..6781b1cd 100644 --- a/src/makeSchema.ts +++ b/src/makeSchema.ts @@ -19,14 +19,14 @@ import { assertNoMissingTypes, objValues, runAbstractTypeRuntimeChecks } from '. * Requires at least one type be named "Query", which will be used as the root query type. */ export function makeSchema(config: SchemaConfig): NexusGraphQLSchema { - const { schema, missingTypes, finalConfig, hasSDLDirectives } = makeSchemaInternal(config) + const { schema, missingTypes, finalConfig } = makeSchemaInternal(config) const typegenConfig = resolveTypegenConfig(finalConfig) const sdl = typegenConfig.outputs.schema const typegen = typegenConfig.outputs.typegen if (sdl || typegen) { // Generating in the next tick allows us to use the schema // in the optional thunk for the typegen config - const typegenPromise = new TypegenMetadata(typegenConfig).generateArtifacts(schema, hasSDLDirectives) + const typegenPromise = new TypegenMetadata(typegenConfig).generateArtifacts(schema) if (config.shouldExitAfterGenerateArtifacts) { let typegenPath = '(not enabled)' if (typegenConfig.outputs.typegen) { @@ -59,9 +59,9 @@ export function makeSchema(config: SchemaConfig): NexusGraphQLSchema { /** Like makeSchema except that typegen is always run and waited upon. */ export async function generateSchema(config: SchemaConfig): Promise { - const { schema, missingTypes, finalConfig, hasSDLDirectives } = makeSchemaInternal(config) + const { schema, missingTypes, finalConfig } = makeSchemaInternal(config) const typegenConfig = resolveTypegenConfig(finalConfig) - await new TypegenMetadata(typegenConfig).generateArtifacts(schema, hasSDLDirectives) + await new TypegenMetadata(typegenConfig).generateArtifacts(schema) assertNoMissingTypes(schema, missingTypes) runAbstractTypeRuntimeChecks(schema, finalConfig.features) return schema @@ -80,11 +80,11 @@ generateSchema.withArtifacts = async ( tsTypes: string globalTypes: string | null }> => { - const { schema, missingTypes, finalConfig, hasSDLDirectives } = makeSchemaInternal(config) + const { schema, missingTypes, finalConfig } = makeSchemaInternal(config) const typegenConfig = resolveTypegenConfig(finalConfig) const { schemaTypes, tsTypes, globalTypes } = await new TypegenMetadata( typegenConfig - ).generateArtifactContents(schema, typegen, hasSDLDirectives) + ).generateArtifactContents(schema, typegen) assertNoMissingTypes(schema, missingTypes) runAbstractTypeRuntimeChecks(schema, finalConfig.features) return { schema, schemaTypes, tsTypes, globalTypes } @@ -125,7 +125,6 @@ export function makeSchemaInternal(config: SchemaConfig) { onAfterBuildFns, customDirectives, schemaDirectives, - hasSDLDirectives, } = builder.getFinalTypeMap() const schema = new GraphQLSchema({ @@ -144,7 +143,7 @@ export function makeSchemaInternal(config: SchemaConfig) { onAfterBuildFns.forEach((fn) => fn(schema)) - return { schema, missingTypes, finalConfig, hasSDLDirectives } + return { schema, missingTypes, finalConfig } } type OmittedVals = Partial<{ [K in keyof MakeSchemaOptions]: never }> diff --git a/src/typegenMetadata.ts b/src/typegenMetadata.ts index da3f7e5a..6feee585 100644 --- a/src/typegenMetadata.ts +++ b/src/typegenMetadata.ts @@ -1,4 +1,4 @@ -import { GraphQLSchema, lexicographicSortSchema, printSchema } from 'graphql' +import { GraphQLSchema, lexicographicSortSchema } from 'graphql' import * as path from 'path' import type { BuilderConfigInput, TypegenInfo } from './builder' import type { ConfiguredTypegen } from './core' @@ -26,15 +26,11 @@ export class TypegenMetadata { constructor(protected config: TypegenMetadataConfig) {} /** Generates the artifacts of the build based on what we know about the schema and how it was defined. */ - async generateArtifacts(schema: NexusGraphQLSchema, hasSDLDirectives: boolean) { + async generateArtifacts(schema: NexusGraphQLSchema) { const sortedSchema = this.sortSchema(schema) const { typegen } = this.config.outputs if (this.config.outputs.schema || typegen) { - const { schemaTypes, tsTypes, globalTypes } = await this.generateArtifactContents( - sortedSchema, - typegen, - hasSDLDirectives - ) + const { schemaTypes, tsTypes, globalTypes } = await this.generateArtifactContents(sortedSchema, typegen) if (this.config.outputs.schema) { await this.writeFile('schema', schemaTypes, this.config.outputs.schema) } @@ -51,13 +47,9 @@ export class TypegenMetadata { } } - async generateArtifactContents( - schema: NexusGraphQLSchema, - typegen: string | null | ConfiguredTypegen, - hasSDLDirectives: boolean - ) { + async generateArtifactContents(schema: NexusGraphQLSchema, typegen: string | null | ConfiguredTypegen) { const result = { - schemaTypes: this.generateSchemaFile(schema, hasSDLDirectives), + schemaTypes: this.generateSchemaFile(schema), tsTypes: '', globalTypes: null as null | string, } @@ -128,11 +120,10 @@ export class TypegenMetadata { } /** Generates the schema, adding any directives as necessary */ - generateSchemaFile(schema: GraphQLSchema, hasSDLDirectives: boolean): string { - const printer = hasSDLDirectives ? printSchemaWithDirectives : printSchema + generateSchemaFile(schema: GraphQLSchema): string { let printedSchema = this.config.customPrintSchemaFn ? this.config.customPrintSchemaFn(schema) - : printer(schema) + : printSchemaWithDirectives(schema) return [SDL_HEADER, printedSchema].join('\n\n') }