Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main/java/graphql/schema/idl/FastSchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import graphql.language.OperationTypeDefinition;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLNamedType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.errors.SchemaProblem;
import graphql.util.Pair;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -87,6 +89,9 @@ private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry t
// Build directives
Set<GraphQLDirective> additionalDirectives = schemaGeneratorHelper.buildAdditionalDirectiveDefinitions(buildCtx);

// Build directives applied to the schema (from both schema definition and schema extensions)
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> schemaDirectives = schemaGeneratorHelper.buildSchemaAppliedDirectives(buildCtx);

// Use a dummy builder to trigger type building (this populates buildCtx)
GraphQLSchema.Builder tempBuilder = GraphQLSchema.newSchema();
schemaGeneratorHelper.buildOperations(buildCtx, tempBuilder);
Expand Down Expand Up @@ -132,12 +137,19 @@ private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry t
// Add all directive definitions
fastBuilder.additionalDirectives(additionalDirectives);

// Add schema description and definition if present
// Add directives applied to the schema
fastBuilder.withSchemaDirectives(schemaDirectives.first);
fastBuilder.withSchemaAppliedDirectives(schemaDirectives.second);

// Add schema description, and AST definitions if captured
typeRegistry.schemaDefinition().ifPresent(schemaDefinition -> {
String description = buildDescription(buildCtx, schemaDefinition, schemaDefinition.getDescription());
fastBuilder.description(description);
fastBuilder.definition(schemaDefinition);
});
if (buildCtx.isCaptureAstDefinitions()) {
typeRegistry.schemaDefinition().ifPresent(fastBuilder::definition);
fastBuilder.extensionDefinitions(typeRegistry.getSchemaExtensionDefinitions());
}

// Configure validation
fastBuilder.withValidation(options.isWithValidation());
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/graphql/schema/idl/SchemaGeneratorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -960,21 +960,24 @@ void buildOperations(BuildContext buildCtx, GraphQLSchema.Builder schemaBuilder)

void buildSchemaDirectivesAndExtensions(BuildContext buildCtx, GraphQLSchema.Builder schemaBuilder) {
TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
List<Directive> schemaDirectiveList = SchemaExtensionsChecker.gatherSchemaDirectives(typeRegistry);
Set<GraphQLDirective> runtimeDirectives = buildCtx.getDirectives();
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> appliedDirectives = buildAppliedDirectives(
Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> appliedDirectives = buildSchemaAppliedDirectives(buildCtx);
schemaBuilder.withSchemaDirectives(appliedDirectives.first);
schemaBuilder.withSchemaAppliedDirectives(appliedDirectives.second);

schemaBuilder.definition(buildCtx.isCaptureAstDefinitions() ? typeRegistry.schemaDefinition().orElse(null) : null);
schemaBuilder.extensionDefinitions(buildCtx.isCaptureAstDefinitions() ? typeRegistry.getSchemaExtensionDefinitions() : emptyList());
}

Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> buildSchemaAppliedDirectives(BuildContext buildCtx) {
List<Directive> schemaDirectiveList = SchemaExtensionsChecker.gatherSchemaDirectives(buildCtx.getTypeRegistry());
return buildAppliedDirectives(
buildCtx,
inputTypeFactory(buildCtx),
schemaDirectiveList,
emptyList(),
DirectiveLocation.SCHEMA,
runtimeDirectives,
buildCtx.getDirectives(),
buildCtx.getComparatorRegistry());
schemaBuilder.withSchemaDirectives(appliedDirectives.first);
schemaBuilder.withSchemaAppliedDirectives(appliedDirectives.second);

schemaBuilder.definition(buildCtx.isCaptureAstDefinitions() ? typeRegistry.schemaDefinition().orElse(null) : null);
schemaBuilder.extensionDefinitions(buildCtx.isCaptureAstDefinitions() ? typeRegistry.getSchemaExtensionDefinitions() : emptyList());
}

List<InputObjectTypeExtensionDefinition> inputObjectTypeExtensions(InputObjectTypeDefinition typeDefinition, BuildContext buildCtx) {
Expand Down
80 changes: 80 additions & 0 deletions src/test/groovy/graphql/schema/idl/FastSchemaGeneratorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,84 @@ class FastSchemaGeneratorTest extends Specification {
schema != null
}

def "schema directives from schema definition and schema extensions match standard SchemaGenerator"() {
given:
def sdl = '''
directive @link(url: String!, purpose: Purpose, opts: LinkOpts) repeatable on SCHEMA
directive @owner(team: String!) on SCHEMA

enum Purpose { SECURITY EXECUTION }
input LinkOpts { strict: Boolean }

schema @link(url: "https://a.dev", purpose: SECURITY, opts: { strict: true }) {
query: Query
}

extend schema @owner(team: "graph") @link(url: "https://b.dev")

type Query { hello: String }
'''
def registry = new SchemaParser().parse(sdl)

when:
def standardSchema = new SchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)

then:
fastSchema.schemaAppliedDirectives.collect { it.name } == ["link", "owner", "link"]
fastSchema.schemaAppliedDirectives.collect { it.name } == standardSchema.schemaAppliedDirectives.collect { it.name }
fastSchema.getSchemaAppliedDirectives("link").collect { it.getArgument("url").value } == ["https://a.dev", "https://b.dev"]
fastSchema.getSchemaAppliedDirective("owner").getArgument("team").value == "graph"
fastSchema.schemaDirectives.collect { it.name } == standardSchema.schemaDirectives.collect { it.name }

def options = SchemaPrinter.Options.defaultOptions().includeSchemaDefinition(true)
new SchemaPrinter(options).print(fastSchema) == new SchemaPrinter(options).print(standardSchema)
}

def "schema extension definitions are captured like standard SchemaGenerator"() {
given:
def sdl = '''
directive @owner(team: String!) on SCHEMA

schema { query: Query }
extend schema @owner(team: "graph")
extend schema { mutation: Mutation }

type Query { hello: String }
type Mutation { bump: Int }
'''
def registry = new SchemaParser().parse(sdl)

when:
def standardSchema = new SchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(registry, RuntimeWiring.MOCKED_WIRING)

then:
fastSchema.mutationType.name == "Mutation"
fastSchema.definition != null
fastSchema.extensionDefinitions.size() == 2
fastSchema.extensionDefinitions == standardSchema.extensionDefinitions
}

def "schema AST definitions are not captured when captureAstDefinitions is false"() {
given:
def sdl = '''
directive @owner(team: String!) on SCHEMA

schema { query: Query }
extend schema @owner(team: "graph")

type Query { hello: String }
'''
def options = SchemaGenerator.Options.defaultOptions().captureAstDefinitions(false)

when:
def fastSchema = new FastSchemaGenerator().makeExecutableSchema(options, new SchemaParser().parse(sdl), RuntimeWiring.MOCKED_WIRING)

then:
fastSchema.definition == null
fastSchema.extensionDefinitions.isEmpty()
fastSchema.getSchemaAppliedDirective("owner") != null
}

}
Loading