Skip to content

Commit

Permalink
fix: remove hasSDLDirectives internal state (#1091)
Browse files Browse the repository at this point in the history
* fix: remove hasSDLDirectives from schema construction
  • Loading branch information
tgriesser committed May 16, 2022
1 parent 7ee48c4 commit 8e65081
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
14 changes: 3 additions & 11 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []

Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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<any>) {
Expand Down Expand Up @@ -1901,7 +1894,6 @@ export interface BuildTypes<TypeMapDefs extends Record<string, GraphQLNamedType>
schemaExtension: NexusSchemaExtension
onAfterBuildFns: SchemaBuilder['onAfterBuildFns']
customDirectives: Record<string, GraphQLDirective>
hasSDLDirectives: boolean
schemaDirectives?: Partial<{ astNode: ASTKindToNode['SchemaDefinition'] }>
}

Expand Down
15 changes: 7 additions & 8 deletions src/makeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<NexusGraphQLSchema> {
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
Expand All @@ -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 }
Expand Down Expand Up @@ -125,7 +125,6 @@ export function makeSchemaInternal(config: SchemaConfig) {
onAfterBuildFns,
customDirectives,
schemaDirectives,
hasSDLDirectives,
} = builder.getFinalTypeMap()

const schema = new GraphQLSchema({
Expand All @@ -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 }>
Expand Down
23 changes: 7 additions & 16 deletions src/typegenMetadata.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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')
}

Expand Down

0 comments on commit 8e65081

Please sign in to comment.