Skip to content

Commit 6f5f2f5

Browse files
authored
Merge pull request #4529 from xuorig/fast-schema-generator-schema-directives
A few bug fixes for FastSchemaGenerator
2 parents 2b9580c + efd68c6 commit 6f5f2f5

3 files changed

Lines changed: 106 additions & 11 deletions

File tree

‎src/main/java/graphql/schema/idl/FastSchemaGenerator.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import graphql.language.OperationTypeDefinition;
66
import org.jspecify.annotations.NullMarked;
77
import org.jspecify.annotations.Nullable;
8+
import graphql.schema.GraphQLAppliedDirective;
89
import graphql.schema.GraphQLCodeRegistry;
910
import graphql.schema.GraphQLDirective;
1011
import graphql.schema.GraphQLNamedType;
1112
import graphql.schema.GraphQLObjectType;
1213
import graphql.schema.GraphQLSchema;
1314
import graphql.schema.idl.errors.SchemaProblem;
15+
import graphql.util.Pair;
1416
import java.util.LinkedHashSet;
1517
import java.util.List;
1618
import java.util.Map;
@@ -87,6 +89,9 @@ private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry t
8789
// Build directives
8890
Set<GraphQLDirective> additionalDirectives = schemaGeneratorHelper.buildAdditionalDirectiveDefinitions(buildCtx);
8991

92+
// Build directives applied to the schema (from both schema definition and schema extensions)
93+
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> schemaDirectives = schemaGeneratorHelper.buildSchemaAppliedDirectives(buildCtx);
94+
9095
// Use a dummy builder to trigger type building (this populates buildCtx)
9196
GraphQLSchema.Builder tempBuilder = GraphQLSchema.newSchema();
9297
schemaGeneratorHelper.buildOperations(buildCtx, tempBuilder);
@@ -132,12 +137,19 @@ private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry t
132137
// Add all directive definitions
133138
fastBuilder.additionalDirectives(additionalDirectives);
134139

135-
// Add schema description and definition if present
140+
// Add directives applied to the schema
141+
fastBuilder.withSchemaDirectives(schemaDirectives.first);
142+
fastBuilder.withSchemaAppliedDirectives(schemaDirectives.second);
143+
144+
// Add schema description, and AST definitions if captured
136145
typeRegistry.schemaDefinition().ifPresent(schemaDefinition -> {
137146
String description = buildDescription(buildCtx, schemaDefinition, schemaDefinition.getDescription());
138147
fastBuilder.description(description);
139-
fastBuilder.definition(schemaDefinition);
140148
});
149+
if (buildCtx.isCaptureAstDefinitions()) {
150+
typeRegistry.schemaDefinition().ifPresent(fastBuilder::definition);
151+
fastBuilder.extensionDefinitions(typeRegistry.getSchemaExtensionDefinitions());
152+
}
141153

142154
// Configure validation
143155
fastBuilder.withValidation(options.isWithValidation());

‎src/main/java/graphql/schema/idl/SchemaGeneratorHelper.java‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -960,21 +960,24 @@ void buildOperations(BuildContext buildCtx, GraphQLSchema.Builder schemaBuilder)
960960

961961
void buildSchemaDirectivesAndExtensions(BuildContext buildCtx, GraphQLSchema.Builder schemaBuilder) {
962962
TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
963-
List<Directive> schemaDirectiveList = SchemaExtensionsChecker.gatherSchemaDirectives(typeRegistry);
964-
Set<GraphQLDirective> runtimeDirectives = buildCtx.getDirectives();
965-
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> appliedDirectives = buildAppliedDirectives(
963+
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> appliedDirectives = buildSchemaAppliedDirectives(buildCtx);
964+
schemaBuilder.withSchemaDirectives(appliedDirectives.first);
965+
schemaBuilder.withSchemaAppliedDirectives(appliedDirectives.second);
966+
967+
schemaBuilder.definition(buildCtx.isCaptureAstDefinitions() ? typeRegistry.schemaDefinition().orElse(null) : null);
968+
schemaBuilder.extensionDefinitions(buildCtx.isCaptureAstDefinitions() ? typeRegistry.getSchemaExtensionDefinitions() : emptyList());
969+
}
970+
971+
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> buildSchemaAppliedDirectives(BuildContext buildCtx) {
972+
List<Directive> schemaDirectiveList = SchemaExtensionsChecker.gatherSchemaDirectives(buildCtx.getTypeRegistry());
973+
return buildAppliedDirectives(
966974
buildCtx,
967975
inputTypeFactory(buildCtx),
968976
schemaDirectiveList,
969977
emptyList(),
970978
DirectiveLocation.SCHEMA,
971-
runtimeDirectives,
979+
buildCtx.getDirectives(),
972980
buildCtx.getComparatorRegistry());
973-
schemaBuilder.withSchemaDirectives(appliedDirectives.first);
974-
schemaBuilder.withSchemaAppliedDirectives(appliedDirectives.second);
975-
976-
schemaBuilder.definition(buildCtx.isCaptureAstDefinitions() ? typeRegistry.schemaDefinition().orElse(null) : null);
977-
schemaBuilder.extensionDefinitions(buildCtx.isCaptureAstDefinitions() ? typeRegistry.getSchemaExtensionDefinitions() : emptyList());
978981
}
979982

980983
List<InputObjectTypeExtensionDefinition> inputObjectTypeExtensions(InputObjectTypeDefinition typeDefinition, BuildContext buildCtx) {

‎src/test/groovy/graphql/schema/idl/FastSchemaGeneratorTest.groovy‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,84 @@ class FastSchemaGeneratorTest extends Specification {
370370
schema != null
371371
}
372372

373+
def "schema directives from schema definition and schema extensions match standard SchemaGenerator"() {
374+
given:
375+
def sdl = '''
376+
directive @link(url: String!, purpose: Purpose, opts: LinkOpts) repeatable on SCHEMA
377+
directive @owner(team: String!) on SCHEMA
378+
379+
enum Purpose { SECURITY EXECUTION }
380+
input LinkOpts { strict: Boolean }
381+
382+
schema @link(url: "https://a.dev", purpose: SECURITY, opts: { strict: true }) {
383+
query: Query
384+
}
385+
386+
extend schema @owner(team: "graph") @link(url: "https://b.dev")
387+
388+
type Query { hello: String }
389+
'''
390+
def registry = new SchemaParser().parse(sdl)
391+
392+
when:
393+
def standardSchema = new SchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
394+
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
395+
396+
then:
397+
fastSchema.schemaAppliedDirectives.collect { it.name } == ["link", "owner", "link"]
398+
fastSchema.schemaAppliedDirectives.collect { it.name } == standardSchema.schemaAppliedDirectives.collect { it.name }
399+
fastSchema.getSchemaAppliedDirectives("link").collect { it.getArgument("url").value } == ["https://a.dev", "https://b.dev"]
400+
fastSchema.getSchemaAppliedDirective("owner").getArgument("team").value == "graph"
401+
fastSchema.schemaDirectives.collect { it.name } == standardSchema.schemaDirectives.collect { it.name }
402+
403+
def options = SchemaPrinter.Options.defaultOptions().includeSchemaDefinition(true)
404+
new SchemaPrinter(options).print(fastSchema) == new SchemaPrinter(options).print(standardSchema)
405+
}
406+
407+
def "schema extension definitions are captured like standard SchemaGenerator"() {
408+
given:
409+
def sdl = '''
410+
directive @owner(team: String!) on SCHEMA
411+
412+
schema { query: Query }
413+
extend schema @owner(team: "graph")
414+
extend schema { mutation: Mutation }
415+
416+
type Query { hello: String }
417+
type Mutation { bump: Int }
418+
'''
419+
def registry = new SchemaParser().parse(sdl)
420+
421+
when:
422+
def standardSchema = new SchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
423+
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
424+
425+
then:
426+
fastSchema.mutationType.name == "Mutation"
427+
fastSchema.definition != null
428+
fastSchema.extensionDefinitions.size() == 2
429+
fastSchema.extensionDefinitions == standardSchema.extensionDefinitions
430+
}
431+
432+
def "schema AST definitions are not captured when captureAstDefinitions is false"() {
433+
given:
434+
def sdl = '''
435+
directive @owner(team: String!) on SCHEMA
436+
437+
schema { query: Query }
438+
extend schema @owner(team: "graph")
439+
440+
type Query { hello: String }
441+
'''
442+
def options = SchemaGenerator.Options.defaultOptions().captureAstDefinitions(false)
443+
444+
when:
445+
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(options, new SchemaParser().parse(sdl), RuntimeWiring.MOCKED_WIRING)
446+
447+
then:
448+
fastSchema.definition == null
449+
fastSchema.extensionDefinitions.isEmpty()
450+
fastSchema.getSchemaAppliedDirective("owner") != null
451+
}
452+
373453
}

0 commit comments

Comments
 (0)