Skip to content

Commit 16a8fc2

Browse files
committed
Change FastBuilder.addType to accept GraphQLNamedType
- addType() and addTypes() now take GraphQLNamedType instead of GraphQLType - Remove unnecessary unwrapping logic since only named types are accepted - Remove null checks from builder methods (fail fast on null) - Update FastSchemaGenerator to use GraphQLNamedType - Update FastBuilderComparisonTest to use GraphQLNamedType
1 parent c446304 commit 16a8fc2

3 files changed

Lines changed: 31 additions & 51 deletions

File tree

src/main/java/graphql/schema/GraphQLSchema.java

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ private GraphQLSchema validateSchema(GraphQLSchema graphQLSchema) {
10881088
* <li>There's no need to clear/reset the builder state midstream.</li>
10891089
* <li>The code registry builder is complete and available when FastBuilder is constructed</li>
10901090
* </ul>
1091-
* FastBuilder also can optionally skip schema validation, which can save time and
1091+
* FastBuilder also can optionally skip schema validation, which can save time and
10921092
* memory for large schemas that have been previously validated (eg, in build tool chains).
10931093
*
10941094
* @see GraphQLSchema.Builder for standard schema construction
@@ -1146,24 +1146,19 @@ public FastBuilder(GraphQLCodeRegistry.Builder codeRegistryBuilder,
11461146
}
11471147

11481148
/**
1149-
* Adds a type to the schema. The type must be a named type (not a wrapper like List or NonNull).
1149+
* Adds a named type to the schema.
11501150
* All non-root types added via this method will be included in {@link GraphQLSchema#getAdditionalTypes()}.
11511151
*
1152-
* @param type the type to add
1152+
* @param type the named type to add
11531153
* @return this builder for chaining
11541154
*/
1155-
public FastBuilder addType(GraphQLType type) {
1156-
if (type == null) {
1157-
return this;
1158-
}
1155+
public FastBuilder addType(GraphQLNamedType type) {
11591156

1160-
// Unwrap to named type
1161-
GraphQLUnmodifiedType namedType = GraphQLTypeUtil.unwrapAll(type);
1162-
String name = namedType.getName();
1157+
String name = type.getName();
11631158

11641159
// Enforce uniqueness by name
11651160
GraphQLNamedType existing = typeMap.get(name);
1166-
if (existing != null && existing != namedType) {
1161+
if (existing != null && existing != type) {
11671162
throw new AssertException(String.format("Type '%s' already exists with a different instance", name));
11681163
}
11691164

@@ -1173,23 +1168,23 @@ public FastBuilder addType(GraphQLType type) {
11731168
}
11741169

11751170
// Insert into typeMap
1176-
typeMap.put(name, namedType);
1171+
typeMap.put(name, type);
11771172

11781173
// Shallow scan via ShallowTypeRefCollector (also tracks interface implementations)
1179-
shallowTypeRefCollector.handleTypeDef(namedType);
1174+
shallowTypeRefCollector.handleTypeDef(type);
11801175

11811176
// For interface types, wire type resolver if present
1182-
if (namedType instanceof GraphQLInterfaceType) {
1183-
GraphQLInterfaceType interfaceType = (GraphQLInterfaceType) namedType;
1177+
if (type instanceof GraphQLInterfaceType) {
1178+
GraphQLInterfaceType interfaceType = (GraphQLInterfaceType) type;
11841179
TypeResolver resolver = interfaceType.getTypeResolver();
11851180
if (resolver != null) {
11861181
codeRegistryBuilder.typeResolverIfAbsent(interfaceType, resolver);
11871182
}
11881183
}
11891184

11901185
// For union types, wire type resolver if present
1191-
if (namedType instanceof GraphQLUnionType) {
1192-
GraphQLUnionType unionType = (GraphQLUnionType) namedType;
1186+
if (type instanceof GraphQLUnionType) {
1187+
GraphQLUnionType unionType = (GraphQLUnionType) type;
11931188
TypeResolver resolver = unionType.getTypeResolver();
11941189
if (resolver != null) {
11951190
codeRegistryBuilder.typeResolverIfAbsent(unionType, resolver);
@@ -1200,16 +1195,14 @@ public FastBuilder addType(GraphQLType type) {
12001195
}
12011196

12021197
/**
1203-
* Adds multiple types to the schema.
1198+
* Adds multiple named types to the schema.
12041199
* All non-root types added via this method will be included in {@link GraphQLSchema#getAdditionalTypes()}.
12051200
*
1206-
* @param types the types to add
1201+
* @param types the named types to add
12071202
* @return this builder for chaining
12081203
*/
1209-
public FastBuilder addTypes(Collection<? extends GraphQLType> types) {
1210-
if (types != null) {
1211-
types.forEach(this::addType);
1212-
}
1204+
public FastBuilder addTypes(Collection<? extends GraphQLNamedType> types) {
1205+
types.forEach(this::addType);
12131206
return this;
12141207
}
12151208

@@ -1220,10 +1213,6 @@ public FastBuilder addTypes(Collection<? extends GraphQLType> types) {
12201213
* @return this builder for chaining
12211214
*/
12221215
public FastBuilder additionalDirective(GraphQLDirective directive) {
1223-
if (directive == null) {
1224-
return this;
1225-
}
1226-
12271216
String name = directive.getName();
12281217
GraphQLDirective existing = directiveMap.get(name);
12291218
if (existing != null && existing != directive) {
@@ -1245,9 +1234,7 @@ public FastBuilder additionalDirective(GraphQLDirective directive) {
12451234
* @return this builder for chaining
12461235
*/
12471236
public FastBuilder additionalDirectives(Collection<? extends GraphQLDirective> directives) {
1248-
if (directives != null) {
1249-
directives.forEach(this::additionalDirective);
1250-
}
1237+
directives.forEach(this::additionalDirective);
12511238
return this;
12521239
}
12531240

@@ -1258,9 +1245,7 @@ public FastBuilder additionalDirectives(Collection<? extends GraphQLDirective> d
12581245
* @return this builder for chaining
12591246
*/
12601247
public FastBuilder withSchemaDirective(GraphQLDirective directive) {
1261-
if (directive != null) {
1262-
schemaDirectives.add(directive);
1263-
}
1248+
schemaDirectives.add(directive);
12641249
return this;
12651250
}
12661251

@@ -1271,9 +1256,7 @@ public FastBuilder withSchemaDirective(GraphQLDirective directive) {
12711256
* @return this builder for chaining
12721257
*/
12731258
public FastBuilder withSchemaDirectives(Collection<? extends GraphQLDirective> directives) {
1274-
if (directives != null) {
1275-
schemaDirectives.addAll(directives);
1276-
}
1259+
schemaDirectives.addAll(directives);
12771260
return this;
12781261
}
12791262

@@ -1284,11 +1267,9 @@ public FastBuilder withSchemaDirectives(Collection<? extends GraphQLDirective> d
12841267
* @return this builder for chaining
12851268
*/
12861269
public FastBuilder withSchemaAppliedDirective(GraphQLAppliedDirective applied) {
1287-
if (applied != null) {
1288-
schemaAppliedDirectives.add(applied);
1289-
// Scan applied directive arguments for type references
1290-
shallowTypeRefCollector.scanAppliedDirectives(singletonList(applied));
1291-
}
1270+
schemaAppliedDirectives.add(applied);
1271+
// Scan applied directive arguments for type references
1272+
shallowTypeRefCollector.scanAppliedDirectives(singletonList(applied));
12921273
return this;
12931274
}
12941275

@@ -1299,9 +1280,7 @@ public FastBuilder withSchemaAppliedDirective(GraphQLAppliedDirective applied) {
12991280
* @return this builder for chaining
13001281
*/
13011282
public FastBuilder withSchemaAppliedDirectives(Collection<? extends GraphQLAppliedDirective> appliedList) {
1302-
if (appliedList != null) {
1303-
schemaAppliedDirectives.addAll(appliedList);
1304-
}
1283+
schemaAppliedDirectives.addAll(appliedList);
13051284
return this;
13061285
}
13071286

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import graphql.schema.GraphQLNamedType;
88
import graphql.schema.GraphQLObjectType;
99
import graphql.schema.GraphQLSchema;
10-
import graphql.schema.GraphQLType;
11-
1210
import java.util.Map;
1311
import java.util.Set;
12+
import java.util.stream.Collectors;
1413

1514
import static graphql.schema.idl.SchemaGeneratorHelper.buildDescription;
1615

@@ -81,8 +80,10 @@ private GraphQLSchema makeExecutableSchemaImpl(ImmutableTypeDefinitionRegistry t
8180
// Build the code registry
8281
GraphQLCodeRegistry codeRegistry = buildCtx.getCodeRegistry().build();
8382

84-
// Extract operation types by name from built types
85-
Set<GraphQLType> allBuiltTypes = buildCtx.getTypes();
83+
// Extract operation types by name from built types (all types from buildCtx are named types)
84+
Set<GraphQLNamedType> allBuiltTypes = buildCtx.getTypes().stream()
85+
.map(t -> (GraphQLNamedType) t)
86+
.collect(Collectors.toSet());
8687

8788
// Get the actual type names from operationTypeDefinitions, defaulting to standard names
8889
String queryTypeName = getOperationTypeName(operationTypeDefinitions, "query", "Query");
@@ -136,8 +137,8 @@ private String getOperationTypeName(Map<String, OperationTypeDefinition> operati
136137
return defaultTypeName;
137138
}
138139

139-
private GraphQLObjectType findOperationType(Set<GraphQLType> types, String typeName) {
140-
for (GraphQLType type : types) {
140+
private GraphQLObjectType findOperationType(Set<GraphQLNamedType> types, String typeName) {
141+
for (GraphQLNamedType type : types) {
141142
if (type instanceof GraphQLObjectType) {
142143
GraphQLObjectType objectType = (GraphQLObjectType) type;
143144
if (objectType.getName().equals(typeName)) {

src/test/groovy/graphql/schema/FastBuilderComparisonTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class FastBuilderComparisonTest extends Specification {
5353
GraphQLObjectType queryType,
5454
GraphQLObjectType mutationType = null,
5555
GraphQLObjectType subscriptionType = null,
56-
List<GraphQLType> additionalTypes = [],
56+
List<GraphQLNamedType> additionalTypes = [],
5757
List<GraphQLDirective> additionalDirectives = [],
5858
GraphQLCodeRegistry.Builder codeRegistry = null
5959
) {

0 commit comments

Comments
 (0)