Skip to content

Commit b8e7a95

Browse files
committed
Fix NonNull wrapper types support in PropertySelection
This commit ensures that `PropertySelection` supports NonNull wrapper types like `BookConnection!` when inspecting property paths for connection edges and nodes. Fixes gh-1298
1 parent 5e3cdeb commit b8e7a95

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import graphql.schema.DataFetchingFieldSelectionSet;
2626
import graphql.schema.GraphQLNamedOutputType;
27+
import graphql.schema.GraphQLNonNull;
28+
import graphql.schema.GraphQLType;
2729
import graphql.schema.SelectedField;
2830

2931
import org.springframework.data.mapping.PropertyPath;
@@ -108,9 +110,16 @@ else if (isConnectionEdgeNode(selectedField)) {
108110
}
109111

110112
private static boolean isConnectionEdges(SelectedField selectedField) {
111-
return selectedField.getName().equals("edges") &&
112-
selectedField.getParentField().getType() instanceof GraphQLNamedOutputType namedType &&
113-
namedType.getName().endsWith("Connection");
113+
if (selectedField.getName().equals("edges")) {
114+
GraphQLType fieldType = selectedField.getParentField().getType();
115+
if (fieldType instanceof GraphQLNonNull nonNullType) {
116+
fieldType = nonNullType.getWrappedType();
117+
}
118+
if (fieldType instanceof GraphQLNamedOutputType namedType) {
119+
return namedType.getName().endsWith("Connection");
120+
}
121+
}
122+
return false;
114123
}
115124

116125
private static boolean isConnectionEdgeNode(SelectedField selectedField) {

spring-graphql/src/test/java/org/springframework/graphql/data/query/PropertySelectionTests.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616

1717
package org.springframework.graphql.data.query;
1818

19+
import java.nio.charset.StandardCharsets;
1920
import java.util.List;
2021
import java.util.concurrent.atomic.AtomicReference;
22+
import java.util.stream.Stream;
2123

2224
import graphql.schema.DataFetcher;
2325
import graphql.schema.DataFetchingFieldSelectionSet;
24-
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.Arguments;
28+
import org.junit.jupiter.params.provider.MethodSource;
2529

30+
import org.springframework.core.io.ByteArrayResource;
31+
import org.springframework.core.io.Resource;
2632
import org.springframework.data.util.TypeInformation;
2733
import org.springframework.graphql.BookSource;
2834
import org.springframework.graphql.GraphQlSetup;
@@ -34,19 +40,21 @@
3440
* Unit test for {@link PropertySelection}.
3541
*
3642
* @author Rossen Stoyanchev
43+
* @author Brian Clozel
3744
*/
3845
class PropertySelectionTests {
3946

40-
@Test
41-
void propertySelectionWithConnection() {
47+
@ParameterizedTest
48+
@MethodSource("schemaResource")
49+
void propertySelectionWithConnection(Resource schemaResource) {
4250

4351
AtomicReference<DataFetchingFieldSelectionSet> ref = new AtomicReference<>();
4452
DataFetcher<?> dataFetcher = environment -> {
4553
ref.set(environment.getSelectionSet());
4654
return null;
4755
};
4856

49-
GraphQlSetup.schemaResource(BookSource.paginationSchema)
57+
GraphQlSetup.schemaResource(schemaResource)
5058
.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer())
5159
.dataFetcher("Query", "books", dataFetcher)
5260
.toGraphQlService()
@@ -59,4 +67,20 @@ void propertySelectionWithConnection() {
5967
assertThat(list).containsExactly("id", "name");
6068
}
6169

70+
static Stream<Arguments> schemaResource() {
71+
return Stream.of(
72+
Arguments.of(BookSource.paginationSchema),
73+
Arguments.of(new ByteArrayResource("""
74+
type Query {
75+
books(first:Int, after:String): BookConnection!
76+
}
77+
78+
type Book {
79+
id: ID
80+
name: String
81+
}
82+
""".getBytes(StandardCharsets.UTF_8)))
83+
);
84+
}
85+
6286
}

0 commit comments

Comments
 (0)