Skip to content

Commit

Permalink
Replace schema inspection assertions with log messages
Browse files Browse the repository at this point in the history
See gh-678
  • Loading branch information
rstoyanchev committed Apr 26, 2023
1 parent 3df8fcb commit c7c75da
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import graphql.schema.DataFetcher;
import graphql.schema.FieldCoordinates;
Expand Down Expand Up @@ -147,11 +148,8 @@ private void inspectFields(GraphQLFieldsContainer fields, @Nullable ResolvableTy
(fields == this.schema.getSubscriptionType()));
}
else if (isNotScalarOrEnumType(field.getType())) {
if (logger.isDebugEnabled()) {
logger.debug("Skipped '" + typeNameToString(field.getType()) + "': " +
fetcher.getClass().getName() + " does not implement SelfDescribingDataFetcher.");
}
this.reportBuilder.addSkippedType(typeNameToString(field.getType()));
addSkippedType(field.getType(), () ->
fetcher.getClass().getName() + " does not implement SelfDescribingDataFetcher.");
}
}
else if (resolvableType == null || !hasProperty(resolvableType, fieldName)) {
Expand All @@ -170,8 +168,8 @@ private void inspectFieldType(GraphQLType outputType, ResolvableType resolvableT

// Remove GraphQL type wrappers, and nest within Java generic types
outputType = unwrapIfNonNull(outputType);
if (isConnectionType(outputType)) {
outputType = getConnectionNodeType(outputType);
if (isPaginatedType(outputType)) {
outputType = getPaginatedType((GraphQLObjectType) outputType);
resolvableType = nestForConnection(resolvableType);
}
else if (outputType instanceof GraphQLList listType) {
Expand All @@ -190,22 +188,15 @@ else if (outputType instanceof GraphQLList listType) {
// Can we inspect GraphQL type?
if (!(outputType instanceof GraphQLFieldsContainer fieldContainer)) {
if (isNotScalarOrEnumType(outputType)) {
if (logger.isDebugEnabled()) {
logger.debug("Skipped '" + typeNameToString(outputType) + "': " +
"inspection does not support " + outputType.getClass().getSimpleName() + ".");
}
this.reportBuilder.addSkippedType(typeNameToString(outputType));
String schemaTypeName = outputType.getClass().getSimpleName();
addSkippedType(outputType, () -> "inspection does not support " + schemaTypeName + ".");
}
return;
}

// Can we inspect Java type?
if (resolvableType.resolve(Object.class) == Object.class) {
if (logger.isDebugEnabled()) {
logger.debug("Skipped '" + typeNameToString(outputType) + "': " +
"inspection could not determine the Java object return type.");
}
this.reportBuilder.addSkippedType(typeNameToString(outputType));
addSkippedType(outputType, () -> "inspection could not determine the Java object return type.");
return;
}

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

private boolean isConnectionType(GraphQLType type) {
private boolean isPaginatedType(GraphQLType type) {
return (type instanceof GraphQLObjectType objectType &&
objectType.getName().endsWith("Connection") &&
objectType.getField("edges") != null && objectType.getField("pageInfo") != null);
}

private GraphQLType getConnectionNodeType(GraphQLType type) {
String name = ((GraphQLObjectType) type).getName();
name = name.substring(0, name.length() - 10);
type = this.schema.getType(name);
Assert.state(type != null, "No '" + name + "' type");
return type;
private GraphQLType getPaginatedType(GraphQLObjectType type) {
String name = type.getName().substring(0, type.getName().length() - 10);
GraphQLType nodeType = this.schema.getType(name);
Assert.state(nodeType != null, "No node type for '" + type.getName() + "'");
return nodeType;
}

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

private ResolvableType nestIfReactive(ResolvableType type) {
Class<?> clazz = type.resolve(Object.class);
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(clazz);
if (adapter != null) {
Assert.state(!adapter.isNoValue(), "Expected value producing type: " + type);
if (logger.isDebugEnabled() && adapter.isNoValue()) {
logger.debug("Expected reactive/async return type that can produce value(s): " + type);
}
return type.getNested(2);
}
return type;
Expand All @@ -250,13 +244,17 @@ private ResolvableType nestIfReactive(ResolvableType type) {
private ResolvableType nestForList(ResolvableType type, boolean subscription) {
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(type.resolve(Object.class));
if (adapter != null) {
Assert.state(!adapter.isNoValue(), "Expected List compatible type: " + type);
if (logger.isDebugEnabled() && adapter.isNoValue()) {
logger.debug("Expected List compatible type: " + type);
}
type = type.getNested(2);
if (adapter.isMultiValue() && !subscription) {
return type;
}
}
Assert.state(type.isArray() || type.hasGenerics(), "Expected List compatible type: " + type);
if (logger.isDebugEnabled() && (!type.isArray() && type.getGenerics().length != 1)) {
logger.debug("Expected List compatible type: " + type);
}
return type.getNested(2);
}

Expand All @@ -283,6 +281,14 @@ private boolean hasProperty(ResolvableType resolvableType, String fieldName) {
}
}

private void addSkippedType(GraphQLType type, Supplier<String> reason) {
String typeName = typeNameToString(type);
this.reportBuilder.addSkippedType(typeName);
if (logger.isDebugEnabled()) {
logger.debug("Skipped '" + typeName + "': " + reason.get());
}
}

@SuppressWarnings("rawtypes")
private void inspectDataFetcherRegistrations() {
this.runtimeWiring.getDataFetchers().forEach((typeName, registrations) ->
Expand Down

0 comments on commit c7c75da

Please sign in to comment.