Skip to content

Commit c7c75da

Browse files
committed
Replace schema inspection assertions with log messages
See gh-678
1 parent 3df8fcb commit c7c75da

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

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

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.LinkedHashSet;
2222
import java.util.Map;
2323
import java.util.Set;
24+
import java.util.function.Supplier;
2425

2526
import graphql.schema.DataFetcher;
2627
import graphql.schema.FieldCoordinates;
@@ -147,11 +148,8 @@ private void inspectFields(GraphQLFieldsContainer fields, @Nullable ResolvableTy
147148
(fields == this.schema.getSubscriptionType()));
148149
}
149150
else if (isNotScalarOrEnumType(field.getType())) {
150-
if (logger.isDebugEnabled()) {
151-
logger.debug("Skipped '" + typeNameToString(field.getType()) + "': " +
152-
fetcher.getClass().getName() + " does not implement SelfDescribingDataFetcher.");
153-
}
154-
this.reportBuilder.addSkippedType(typeNameToString(field.getType()));
151+
addSkippedType(field.getType(), () ->
152+
fetcher.getClass().getName() + " does not implement SelfDescribingDataFetcher.");
155153
}
156154
}
157155
else if (resolvableType == null || !hasProperty(resolvableType, fieldName)) {
@@ -170,8 +168,8 @@ private void inspectFieldType(GraphQLType outputType, ResolvableType resolvableT
170168

171169
// Remove GraphQL type wrappers, and nest within Java generic types
172170
outputType = unwrapIfNonNull(outputType);
173-
if (isConnectionType(outputType)) {
174-
outputType = getConnectionNodeType(outputType);
171+
if (isPaginatedType(outputType)) {
172+
outputType = getPaginatedType((GraphQLObjectType) outputType);
175173
resolvableType = nestForConnection(resolvableType);
176174
}
177175
else if (outputType instanceof GraphQLList listType) {
@@ -190,22 +188,15 @@ else if (outputType instanceof GraphQLList listType) {
190188
// Can we inspect GraphQL type?
191189
if (!(outputType instanceof GraphQLFieldsContainer fieldContainer)) {
192190
if (isNotScalarOrEnumType(outputType)) {
193-
if (logger.isDebugEnabled()) {
194-
logger.debug("Skipped '" + typeNameToString(outputType) + "': " +
195-
"inspection does not support " + outputType.getClass().getSimpleName() + ".");
196-
}
197-
this.reportBuilder.addSkippedType(typeNameToString(outputType));
191+
String schemaTypeName = outputType.getClass().getSimpleName();
192+
addSkippedType(outputType, () -> "inspection does not support " + schemaTypeName + ".");
198193
}
199194
return;
200195
}
201196

202197
// Can we inspect Java type?
203198
if (resolvableType.resolve(Object.class) == Object.class) {
204-
if (logger.isDebugEnabled()) {
205-
logger.debug("Skipped '" + typeNameToString(outputType) + "': " +
206-
"inspection could not determine the Java object return type.");
207-
}
208-
this.reportBuilder.addSkippedType(typeNameToString(outputType));
199+
addSkippedType(outputType, () -> "inspection could not determine the Java object return type.");
209200
return;
210201
}
211202

@@ -217,31 +208,34 @@ private GraphQLType unwrapIfNonNull(GraphQLType type) {
217208
return (type instanceof GraphQLNonNull graphQLNonNull ? graphQLNonNull.getWrappedType() : type);
218209
}
219210

220-
private boolean isConnectionType(GraphQLType type) {
211+
private boolean isPaginatedType(GraphQLType type) {
221212
return (type instanceof GraphQLObjectType objectType &&
222213
objectType.getName().endsWith("Connection") &&
223214
objectType.getField("edges") != null && objectType.getField("pageInfo") != null);
224215
}
225216

226-
private GraphQLType getConnectionNodeType(GraphQLType type) {
227-
String name = ((GraphQLObjectType) type).getName();
228-
name = name.substring(0, name.length() - 10);
229-
type = this.schema.getType(name);
230-
Assert.state(type != null, "No '" + name + "' type");
231-
return type;
217+
private GraphQLType getPaginatedType(GraphQLObjectType type) {
218+
String name = type.getName().substring(0, type.getName().length() - 10);
219+
GraphQLType nodeType = this.schema.getType(name);
220+
Assert.state(nodeType != null, "No node type for '" + type.getName() + "'");
221+
return nodeType;
232222
}
233223

234224
private ResolvableType nestForConnection(ResolvableType type) {
235225
type = nestIfReactive(type);
236-
Assert.state(type.hasGenerics(), "Expected type with generics: " + type);
226+
if (logger.isDebugEnabled() && type.getGenerics().length != 1) {
227+
logger.debug("Expected Connection type to have a generic parameter: " + type);
228+
}
237229
return type.getNested(2);
238230
}
239231

240232
private ResolvableType nestIfReactive(ResolvableType type) {
241233
Class<?> clazz = type.resolve(Object.class);
242234
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(clazz);
243235
if (adapter != null) {
244-
Assert.state(!adapter.isNoValue(), "Expected value producing type: " + type);
236+
if (logger.isDebugEnabled() && adapter.isNoValue()) {
237+
logger.debug("Expected reactive/async return type that can produce value(s): " + type);
238+
}
245239
return type.getNested(2);
246240
}
247241
return type;
@@ -250,13 +244,17 @@ private ResolvableType nestIfReactive(ResolvableType type) {
250244
private ResolvableType nestForList(ResolvableType type, boolean subscription) {
251245
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(type.resolve(Object.class));
252246
if (adapter != null) {
253-
Assert.state(!adapter.isNoValue(), "Expected List compatible type: " + type);
247+
if (logger.isDebugEnabled() && adapter.isNoValue()) {
248+
logger.debug("Expected List compatible type: " + type);
249+
}
254250
type = type.getNested(2);
255251
if (adapter.isMultiValue() && !subscription) {
256252
return type;
257253
}
258254
}
259-
Assert.state(type.isArray() || type.hasGenerics(), "Expected List compatible type: " + type);
255+
if (logger.isDebugEnabled() && (!type.isArray() && type.getGenerics().length != 1)) {
256+
logger.debug("Expected List compatible type: " + type);
257+
}
260258
return type.getNested(2);
261259
}
262260

@@ -283,6 +281,14 @@ private boolean hasProperty(ResolvableType resolvableType, String fieldName) {
283281
}
284282
}
285283

284+
private void addSkippedType(GraphQLType type, Supplier<String> reason) {
285+
String typeName = typeNameToString(type);
286+
this.reportBuilder.addSkippedType(typeName);
287+
if (logger.isDebugEnabled()) {
288+
logger.debug("Skipped '" + typeName + "': " + reason.get());
289+
}
290+
}
291+
286292
@SuppressWarnings("rawtypes")
287293
private void inspectDataFetcherRegistrations() {
288294
this.runtimeWiring.getDataFetchers().forEach((typeName, registrations) ->

0 commit comments

Comments
 (0)