Skip to content

Commit 4a0d01a

Browse files
committed
Fix auto-registration issue with non-null List
Closes gh-661
1 parent db55ee2 commit 4a0d01a

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/query/AutoRegistrationRuntimeWiringConfigurer.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,16 +112,21 @@ public boolean providesDataFetcher(FieldWiringEnvironment environment) {
112112

113113
@Nullable
114114
private String getOutputTypeName(FieldWiringEnvironment environment) {
115-
GraphQLType outputType = (environment.getFieldType() instanceof GraphQLList ?
116-
((GraphQLList) environment.getFieldType()).getWrappedType() :
117-
environment.getFieldType());
115+
GraphQLType outputType = removeNonNullWrapper(environment.getFieldType());
118116

119-
if (outputType instanceof GraphQLNonNull) {
120-
outputType = ((GraphQLNonNull) outputType).getWrappedType();
117+
if (outputType instanceof GraphQLList) {
118+
outputType = removeNonNullWrapper(((GraphQLList) outputType).getWrappedType());
121119
}
122120

123-
return (outputType instanceof GraphQLNamedOutputType ?
124-
((GraphQLNamedOutputType) outputType).getName() : null);
121+
if (outputType instanceof GraphQLNamedOutputType namedType) {
122+
return namedType.getName();
123+
}
124+
125+
return null;
126+
}
127+
128+
private GraphQLType removeNonNullWrapper(GraphQLType outputType) {
129+
return (outputType instanceof GraphQLNonNull wrapper ? wrapper.getWrappedType() : outputType);
125130
}
126131

127132
private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) {
@@ -152,8 +157,8 @@ public DataFetcher<?> getDataFetcher(FieldWiringEnvironment environment) {
152157
Function<Boolean, DataFetcher<?>> factory = dataFetcherFactories.get(outputTypeName);
153158
Assert.notNull(factory, "Expected DataFetcher factory for typeName '" + outputTypeName + "'");
154159

155-
boolean single = !(environment.getFieldType() instanceof GraphQLList);
156-
return factory.apply(single);
160+
GraphQLType type = removeNonNullWrapper(environment.getFieldType());
161+
return factory.apply(!(type instanceof GraphQLList));
157162
}
158163

159164
}

spring-graphql/src/test/resources/books/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type Query {
22
bookById(id: ID): Book
33
booksById(id: [ID]): [Book]
4-
books(id: ID, name: String, author: String): [Book!]
4+
books(id: ID, name: String, author: String): [Book!]!
55
booksByCriteria(criteria:BookCriteria): [Book]
66
booksByProjectedArguments(name: String, author: String): [Book]
77
booksByProjectedCriteria(criteria:BookCriteria): [Book]

0 commit comments

Comments
 (0)