Skip to content

Commit 9bcd824

Browse files
committed
Polishing in SchemaMappingInspector
1 parent ac3fabb commit 9bcd824

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/SchemaMappingInspector.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,14 @@ else if (typePair.outputType() instanceof GraphQLInterfaceType interfaceType) {
220220
// Can we inspect GraphQL type?
221221
if (!(graphQlType instanceof GraphQLFieldsContainer fieldContainer)) {
222222
if (isNotScalarOrEnumType(graphQlType)) {
223-
FieldCoordinates coordinates = FieldCoordinates.coordinates(parent.getName(), field.getName());
224-
addSkippedType(graphQlType, coordinates, "Unsupported schema type");
223+
this.reportBuilder.skippedType(graphQlType, parent, field, "Unsupported schema type");
225224
}
226225
continue;
227226
}
228227

229228
// Can we inspect the Class?
230229
if (currentResolvableType.resolve(Object.class) == Object.class) {
231-
FieldCoordinates coordinates = FieldCoordinates.coordinates(parent.getName(), field.getName());
232-
addSkippedType(graphQlType, coordinates, "No class information");
230+
this.reportBuilder.skippedType(graphQlType, parent, field, "No class information");
233231
continue;
234232
}
235233

@@ -254,18 +252,6 @@ private static boolean isNotScalarOrEnumType(GraphQLType type) {
254252
return !(type instanceof GraphQLScalarType || type instanceof GraphQLEnumType);
255253
}
256254

257-
private void addSkippedType(GraphQLType type, FieldCoordinates coordinates, String reason) {
258-
String typeName = typeNameToString(type);
259-
this.reportBuilder.skippedType(type, coordinates);
260-
if (logger.isDebugEnabled()) {
261-
logger.debug("Skipped '" + typeName + "': " + reason);
262-
}
263-
}
264-
265-
private static String typeNameToString(GraphQLType type) {
266-
return (type instanceof GraphQLNamedType namedType) ? namedType.getName() : type.toString();
267-
}
268-
269255
private void checkDataFetcherRegistrations() {
270256
this.dataFetchers.forEach((typeName, registrations) ->
271257
registrations.forEach((fieldName, dataFetcher) -> {
@@ -469,6 +455,7 @@ public static ReflectionClassResolver create(
469455
Function<GraphQLObjectType, String> classNameFunction) {
470456

471457
MultiValueMap<String, String> classPrefixes = new LinkedMultiValueMap<>();
458+
472459
for (Map.Entry<String, Map<String, DataFetcher>> typeEntry : dataFetchers.entrySet()) {
473460
String typeName = typeEntry.getKey();
474461
GraphQLType parentType = schema.getType(typeName);
@@ -481,27 +468,33 @@ public static ReflectionClassResolver create(
481468
if (field == null) {
482469
continue; // Unmapped registration
483470
}
484-
TypePair pair = TypePair.resolveTypePair(parentType, field, fieldEntry.getValue(), schema);
471+
DataFetcher dataFetcher = fieldEntry.getValue();
472+
TypePair pair = TypePair.resolveTypePair(parentType, field, dataFetcher, schema);
485473
GraphQLType outputType = pair.outputType();
486474
if (outputType instanceof GraphQLUnionType || outputType instanceof GraphQLInterfaceType) {
487475
String outputTypeName = ((GraphQLNamedOutputType) outputType).getName();
488476
Class<?> clazz = pair.resolvableType().resolve(Object.class);
489477
if (PACKAGE_PREDICATE.test(clazz.getPackageName())) {
490-
int index = clazz.getName().indexOf(clazz.getSimpleName());
491-
classPrefixes.add(outputTypeName, clazz.getName().substring(0, index));
478+
addClassPrefix(outputTypeName, clazz, classPrefixes);
492479
}
493-
else if (fieldEntry.getValue() instanceof SelfDescribingDataFetcher<?> sddf) {
494-
if (sddf.getReturnType().getSource() instanceof MethodParameter param) {
495-
clazz = param.getDeclaringClass();
496-
int index = clazz.getName().indexOf(clazz.getSimpleName());
497-
classPrefixes.add(outputTypeName, clazz.getName().substring(0, index));
480+
else if (dataFetcher instanceof SelfDescribingDataFetcher<?> selfDescribing) {
481+
if (selfDescribing.getReturnType().getSource() instanceof MethodParameter param) {
482+
addClassPrefix(outputTypeName, param.getDeclaringClass(), classPrefixes);
498483
}
499484
}
500485
}
501486
}
502487
}
488+
503489
return new ReflectionClassResolver(classNameFunction, classPrefixes);
504490
}
491+
492+
private static void addClassPrefix(
493+
String unionOrInterfaceType, Class<?> aClass, MultiValueMap<String, String> classPrefixes) {
494+
495+
int index = aClass.getName().indexOf(aClass.getSimpleName());
496+
classPrefixes.add(unionOrInterfaceType, aClass.getName().substring(0, index));
497+
}
505498
}
506499

507500

@@ -744,8 +737,12 @@ void unmappedArgument(DataFetcher<?> dataFetcher, List<String> arguments) {
744737
this.unmappedArguments.put(dataFetcher, arguments);
745738
}
746739

747-
void skippedType(GraphQLType type, FieldCoordinates coordinates) {
748-
this.skippedTypes.add(new DefaultSkippedType(type, coordinates));
740+
void skippedType(GraphQLType type, GraphQLFieldsContainer parent, GraphQLFieldDefinition field, String reason) {
741+
DefaultSkippedType skippedType = DefaultSkippedType.create(type, parent, field);
742+
if (logger.isDebugEnabled()) {
743+
logger.debug("Skipping '" + skippedType + "': " + reason);
744+
}
745+
this.skippedTypes.add(skippedType);
749746
}
750747

751748
SchemaReport build() {
@@ -759,7 +756,7 @@ SchemaReport build() {
759756
/**
760757
* Default implementation of {@link SchemaReport}.
761758
*/
762-
private class DefaultSchemaReport implements SchemaReport {
759+
private final class DefaultSchemaReport implements SchemaReport {
763760

764761
private final List<FieldCoordinates> unmappedFields;
765762

@@ -841,9 +838,14 @@ private record DefaultSkippedType(
841838

842839
@Override
843840
public String toString() {
844-
return typeNameToString(this.type);
841+
return (type instanceof GraphQLNamedType namedType) ? namedType.getName() : type.toString();
845842
}
846843

844+
public static DefaultSkippedType create(
845+
GraphQLType type, GraphQLFieldsContainer parent, GraphQLFieldDefinition field) {
846+
847+
return new DefaultSkippedType(type, FieldCoordinates.coordinates(parent, field));
848+
}
847849
}
848850

849851
}

0 commit comments

Comments
 (0)