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
20 changes: 14 additions & 6 deletions src/main/java/graphql/schema/GraphQLSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Consumer;

import static graphql.Assert.assertNotNull;
Expand Down Expand Up @@ -173,7 +174,8 @@ public GraphQLSchema(BuilderWithoutTypes builder) {
@Internal
private GraphQLSchema(FastBuilder fastBuilder) {
// Build immutable collections from FastBuilder's mutable state
ImmutableMap<String, GraphQLNamedType> finalTypeMap = ImmutableMap.copyOf(fastBuilder.typeMap);
// sorted by name to match the standard Builder (see GraphQLTypeCollectingVisitor.getResult)
ImmutableMap<String, GraphQLNamedType> finalTypeMap = ImmutableMap.copyOf(new TreeMap<>(fastBuilder.typeMap));
ImmutableList<GraphQLDirective> finalDirectives = ImmutableList.copyOf(fastBuilder.directiveMap.values());

// Get interface-to-object-type-names map from collector (already sorted)
Expand Down Expand Up @@ -1504,8 +1506,7 @@ public GraphQLSchema build() {
// Replace type references
shallowTypeRefCollector.replaceTypes(typeMap);

// Add built-in directives if missing
Directives.BUILT_IN_DIRECTIVES.forEach(this::addDirectiveIfMissing);
ensureBuiltInDirectives();

// Create schema via private constructor
GraphQLSchema schema = new GraphQLSchema(this);
Expand All @@ -1521,10 +1522,17 @@ public GraphQLSchema build() {
return schema;
}

private void addDirectiveIfMissing(GraphQLDirective directive) {
if (!directiveMap.containsKey(directive.getName())) {
directiveMap.put(directive.getName(), directive);
// same ordering as Builder.ensureBuiltInDirectives: built-ins first, user overrides keep their position
private void ensureBuiltInDirectives() {
Map<String, GraphQLDirective> ordered = new LinkedHashMap<>();
for (GraphQLDirective builtIn : Directives.BUILT_IN_DIRECTIVES) {
if (!directiveMap.containsKey(builtIn.getName())) {
ordered.put(builtIn.getName(), builtIn);
}
}
ordered.putAll(directiveMap);
directiveMap.clear();
directiveMap.putAll(ordered);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/graphql/schema/idl/FastSchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.errors.SchemaProblem;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -102,7 +103,7 @@ private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry t
// Extract operation types by name from built types (all types from buildCtx are named types)
Set<GraphQLNamedType> allBuiltTypes = buildCtx.getTypes().stream()
.map(t -> (GraphQLNamedType) t)
.collect(Collectors.toSet());
.collect(Collectors.toCollection(LinkedHashSet::new));

// Get the actual type names from operationTypeDefinitions, defaulting to standard names
String queryTypeName = getOperationTypeName(operationTypeDefinitions, "query", "Query");
Expand Down
43 changes: 43 additions & 0 deletions src/test/groovy/graphql/schema/FastBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,49 @@ class FastBuilderTest extends Specification {
thrown(AssertException)
}

def "typeMap is sorted by name regardless of insertion order"() {
given:
def queryType = newObject()
.name("Query")
.field(newFieldDefinition().name("value").type(GraphQLString))
.build()
def scalars = ["Zebra", "Mango", "Apple"].collect {
newScalar().name(it).coercing(GraphQLString.getCoercing()).build()
}

when:
def schema = new GraphQLSchema.FastBuilder(GraphQLCodeRegistry.newCodeRegistry(), queryType, null, null)
.addTypes(scalars)
.build()

then:
def names = schema.typeMap.keySet().toList()
names == names.toSorted()
schema.additionalTypes*.name == schema.additionalTypes*.name.toSorted()
}

def "built-in directives come before additional directives, matching standard Builder"() {
given:
def queryType = newObject()
.name("Query")
.field(newFieldDefinition().name("value").type(GraphQLString))
.build()
def custom = newDirective().name("custom").validLocation(Introspection.DirectiveLocation.FIELD_DEFINITION).build()

when:
def fastSchema = new GraphQLSchema.FastBuilder(GraphQLCodeRegistry.newCodeRegistry(), queryType, null, null)
.additionalDirective(custom)
.build()
def standardSchema = GraphQLSchema.newSchema()
.query(queryType)
.additionalDirective(custom)
.build()

then:
fastSchema.directives*.name == standardSchema.directives*.name
fastSchema.directives*.name.last() == "custom"
}

def "same type instance can be added multiple times"() {
given: "a scalar type"
def scalar = newScalar()
Expand Down
40 changes: 40 additions & 0 deletions src/test/groovy/graphql/schema/idl/FastSchemaGeneratorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@ class FastSchemaGeneratorTest extends Specification {
standardUser.fieldDefinitions.size() == fastUser.fieldDefinitions.size()
}

def "typeMap order matches standard SchemaGenerator"() {
given:
def sdl = '''
type Query { z: Zebra, a: Apple, m: Mango, n: Node }
interface Node { id: ID! }
type Zebra implements Node { id: ID! }
type Apple implements Node { id: ID! }
type Mango { id: ID! }
enum Color { RED }
input Filter { c: Color }
'''
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.typeMap.keySet().toList() == standardSchema.typeMap.keySet().toList()
}

def "directive order matches standard SchemaGenerator"() {
given:
def sdl = '''
directive @zeta on FIELD_DEFINITION
directive @alpha on FIELD_DEFINITION
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
directive @mid on OBJECT
type Query { a: String @alpha, z: String @zeta }
'''
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.directives*.name == standardSchema.directives*.name
}

def "handles schema with interfaces"() {
given:
def sdl = '''
Expand Down
Loading