Skip to content

Commit 6117ba6

Browse files
author
Marc-Andre Giroux
committed
Apply schema directives and extension definitions in FastSchemaGenerator
FastSchemaGenerator never called buildSchemaDirectivesAndExtensions, so directives applied via `schema @foo` or `extend schema @foo` were missing from getSchemaDirectives()/getSchemaAppliedDirectives(), and getExtensionDefinitions() was always empty. The schema definition was also captured regardless of captureAstDefinitions. Extract buildSchemaAppliedDirectives from SchemaGeneratorHelper so both generators share it, and match SchemaGenerator's AST capture behaviour.
1 parent 3208646 commit 6117ba6

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.List;
1517
import java.util.Map;
1618
import java.util.Set;
@@ -86,6 +88,9 @@ private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry t
8688
// Build directives
8789
Set<GraphQLDirective> additionalDirectives = schemaGeneratorHelper.buildAdditionalDirectiveDefinitions(buildCtx);
8890

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

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

141153
// Configure validation
142154
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
@@ -330,4 +330,84 @@ class FastSchemaGeneratorTest extends Specification {
330330
schema != null
331331
}
332332

333+
def "schema directives from schema definition and schema extensions match standard SchemaGenerator"() {
334+
given:
335+
def sdl = '''
336+
directive @link(url: String!, purpose: Purpose, opts: LinkOpts) repeatable on SCHEMA
337+
directive @owner(team: String!) on SCHEMA
338+
339+
enum Purpose { SECURITY EXECUTION }
340+
input LinkOpts { strict: Boolean }
341+
342+
schema @link(url: "https://a.dev", purpose: SECURITY, opts: { strict: true }) {
343+
query: Query
344+
}
345+
346+
extend schema @owner(team: "graph") @link(url: "https://b.dev")
347+
348+
type Query { hello: String }
349+
'''
350+
def registry = new SchemaParser().parse(sdl)
351+
352+
when:
353+
def standardSchema = new SchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
354+
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
355+
356+
then:
357+
fastSchema.schemaAppliedDirectives.collect { it.name } == ["link", "owner", "link"]
358+
fastSchema.schemaAppliedDirectives.collect { it.name } == standardSchema.schemaAppliedDirectives.collect { it.name }
359+
fastSchema.getSchemaAppliedDirectives("link").collect { it.getArgument("url").value } == ["https://a.dev", "https://b.dev"]
360+
fastSchema.getSchemaAppliedDirective("owner").getArgument("team").value == "graph"
361+
fastSchema.schemaDirectives.collect { it.name } == standardSchema.schemaDirectives.collect { it.name }
362+
363+
def options = SchemaPrinter.Options.defaultOptions().includeSchemaDefinition(true)
364+
new SchemaPrinter(options).print(fastSchema) == new SchemaPrinter(options).print(standardSchema)
365+
}
366+
367+
def "schema extension definitions are captured like standard SchemaGenerator"() {
368+
given:
369+
def sdl = '''
370+
directive @owner(team: String!) on SCHEMA
371+
372+
schema { query: Query }
373+
extend schema @owner(team: "graph")
374+
extend schema { mutation: Mutation }
375+
376+
type Query { hello: String }
377+
type Mutation { bump: Int }
378+
'''
379+
def registry = new SchemaParser().parse(sdl)
380+
381+
when:
382+
def standardSchema = new SchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
383+
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
384+
385+
then:
386+
fastSchema.mutationType.name == "Mutation"
387+
fastSchema.definition != null
388+
fastSchema.extensionDefinitions.size() == 2
389+
fastSchema.extensionDefinitions == standardSchema.extensionDefinitions
390+
}
391+
392+
def "schema AST definitions are not captured when captureAstDefinitions is false"() {
393+
given:
394+
def sdl = '''
395+
directive @owner(team: String!) on SCHEMA
396+
397+
schema { query: Query }
398+
extend schema @owner(team: "graph")
399+
400+
type Query { hello: String }
401+
'''
402+
def options = SchemaGenerator.Options.defaultOptions().captureAstDefinitions(false)
403+
404+
when:
405+
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(options, new SchemaParser().parse(sdl), RuntimeWiring.MOCKED_WIRING)
406+
407+
then:
408+
fastSchema.definition == null
409+
fastSchema.extensionDefinitions.isEmpty()
410+
fastSchema.getSchemaAppliedDirective("owner") != null
411+
}
412+
333413
}

0 commit comments

Comments
 (0)