Skip to content

Commit 04cbbd6

Browse files
committed
Improved access speed of isPossibleType - added javadoc and code tweaks
1 parent 88057b0 commit 04cbbd6

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,19 @@ private void checkFieldTypesPresent(TypeDefinitionRegistry typeRegistry, List<Gr
333333

334334
private Consumer<Type> checkTypeExists(String typeOfType, TypeDefinitionRegistry typeRegistry, List<GraphQLError> errors, TypeDefinition typeDefinition) {
335335
return t -> {
336-
TypeName unwrapped = TypeInfo.typeInfo(t).getTypeName();
337-
if (!typeRegistry.hasType(unwrapped)) {
336+
String name = TypeInfo.typeName(t);
337+
if (!typeRegistry.hasType(name)) {
338+
TypeName unwrapped = TypeInfo.typeInfo(t).getTypeName();
338339
errors.add(new MissingTypeError(typeOfType, typeDefinition, unwrapped));
339340
}
340341
};
341342
}
342343

343344
private Consumer<Type> checkTypeExists(TypeDefinitionRegistry typeRegistry, List<GraphQLError> errors, String typeOfType, Node element, String elementName) {
344345
return ivType -> {
345-
TypeName unwrapped = TypeInfo.typeInfo(ivType).getTypeName();
346-
if (!typeRegistry.hasType(unwrapped)) {
346+
String name = TypeInfo.typeName(ivType);
347+
if (!typeRegistry.hasType(name)) {
348+
TypeName unwrapped = TypeInfo.typeInfo(ivType).getTypeName();
347349
errors.add(new MissingTypeError(typeOfType, element, elementName, unwrapped));
348350
}
349351
};

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import static graphql.Assert.assertNotNull;
4646
import static graphql.schema.idl.SchemaExtensionsChecker.defineOperationDefs;
4747
import static graphql.schema.idl.SchemaExtensionsChecker.gatherOperationDefs;
48+
import static graphql.schema.idl.TypeInfo.typeName;
4849
import static java.util.Optional.ofNullable;
4950

5051
/**
@@ -492,19 +493,33 @@ public Map<String, DirectiveDefinition> getDirectiveDefinitions() {
492493
return new LinkedHashMap<>(directiveDefinitions);
493494
}
494495

496+
/**
497+
* Returns true if the registry has a type of the specified {@link TypeName}
498+
*
499+
* @param typeName the type name to check
500+
*
501+
* @return true if the registry has a type by that type name
502+
*/
495503
public boolean hasType(TypeName typeName) {
496504
String name = typeName.getName();
497-
return types.containsKey(name) || ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.containsKey(name) || scalarTypes.containsKey(name) || objectTypeExtensions.containsKey(name);
505+
return hasType(name);
498506
}
499507

500-
private static String typeName(Type type) {
501-
return TypeInfo.getTypeName(type).getName();
508+
/**
509+
* Returns true if the registry has a type of the specified name
510+
*
511+
* @param name the name to check
512+
*
513+
* @return true if the registry has a type by that name
514+
*/
515+
public boolean hasType(String name) {
516+
return types.containsKey(name) || ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.containsKey(name) || scalarTypes.containsKey(name) || objectTypeExtensions.containsKey(name);
502517
}
503518

504519
/**
505520
* Returns am optional {@link TypeDefinition} of the specified type or {@link Optional#empty()}
506521
*
507-
* @param type the type to check
522+
* @param type the type to check
508523
*
509524
* @return an optional {@link TypeDefinition} or empty if it's not found
510525
*/

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ public Type unwrapOneType() {
140140
}
141141

142142
/**
143-
* Gets the type name of a [Type], unwrapping any lists or non-null decorations
143+
* Gets the {@link TypeName} type name of a [Type], unwrapping any lists or non-null decorations
144144
*
145145
* @param type the Type
146146
*
147-
* @return the inner TypeName for this type
147+
* @return the inner {@link TypeName} for this type
148148
*/
149149
public static TypeName getTypeName(Type<?> type) {
150150
while (!(type instanceof TypeName)) {
@@ -158,6 +158,17 @@ public static TypeName getTypeName(Type<?> type) {
158158
return (TypeName) type;
159159
}
160160

161+
/**
162+
* Gets the string type name of a [Type], unwrapping any lists or non-null decorations
163+
*
164+
* @param type the Type
165+
*
166+
* @return the inner string name for this type
167+
*/
168+
public static String typeName(Type<?> type) {
169+
return getTypeName(type).getName();
170+
}
171+
161172
@Override
162173
public boolean equals(Object o) {
163174
if (this == o) return true;

0 commit comments

Comments
 (0)