Skip to content

Commit 579bb64

Browse files
committed
Improve paginated type check in schema inspection
Closes gh-1053
1 parent 1979e43 commit 579bb64

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

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

+23-13
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,9 @@ public static TypePair resolveTypePair(
684684

685685
// Remove GraphQL type wrappers, and nest within Java generic types
686686
GraphQLType outputType = unwrapIfNonNull(field.getType());
687-
if (isPaginatedType(outputType)) {
688-
outputType = getPaginatedType((GraphQLObjectType) outputType, schema);
687+
GraphQLType paginatedType = getPaginatedType(outputType);
688+
if (paginatedType != null) {
689+
outputType = paginatedType;
689690
resolvableType = nestForConnection(resolvableType);
690691
}
691692
else if (outputType instanceof GraphQLList listType) {
@@ -702,17 +703,26 @@ private static GraphQLType unwrapIfNonNull(GraphQLType type) {
702703
return (type instanceof GraphQLNonNull graphQLNonNull) ? graphQLNonNull.getWrappedType() : type;
703704
}
704705

705-
private static boolean isPaginatedType(GraphQLType type) {
706-
return (type instanceof GraphQLObjectType objectType &&
707-
objectType.getName().endsWith("Connection") &&
708-
objectType.getField("edges") != null && objectType.getField("pageInfo") != null);
709-
}
710-
711-
private static GraphQLType getPaginatedType(GraphQLObjectType type, GraphQLSchema schema) {
712-
String name = type.getName().substring(0, type.getName().length() - 10);
713-
GraphQLType nodeType = schema.getType(name);
714-
Assert.state(nodeType != null, "No node type for '" + type.getName() + "'");
715-
return nodeType;
706+
@Nullable
707+
private static GraphQLType getPaginatedType(GraphQLType type) {
708+
if (!(type instanceof GraphQLObjectType cot && cot.getName().endsWith("Connection"))) {
709+
return null;
710+
}
711+
GraphQLFieldDefinition edges = cot.getField("edges");
712+
if (edges == null) {
713+
return null;
714+
}
715+
if (!(unwrapIfNonNull(edges.getType()) instanceof GraphQLList lt)) {
716+
return null;
717+
}
718+
if (!(lt.getWrappedType() instanceof GraphQLObjectType eot)) {
719+
return null;
720+
}
721+
GraphQLFieldDefinition node = eot.getField("node");
722+
if (node == null) {
723+
return null;
724+
}
725+
return unwrapIfNonNull(node.getType());
716726
}
717727

718728
private static ResolvableType nestForConnection(ResolvableType type) {

spring-graphql/src/test/java/org/springframework/graphql/execution/SchemaMappingInspectorTests.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void reportWorksForQueryWithOptional() {
113113
}
114114

115115
@Test
116-
void reportWorksForQueryWithConnection() {
116+
void reportWorksForConnectionType() {
117117
String schema = """
118118
type Query {
119119
paginatedBooks: BookConnection
@@ -124,7 +124,7 @@ void reportWorksForQueryWithConnection() {
124124
}
125125
type BookEdge {
126126
cursor: String!
127-
# ...
127+
node: Book!
128128
}
129129
type PageInfo {
130130
startCursor: String
@@ -140,6 +140,34 @@ void reportWorksForQueryWithConnection() {
140140
assertThatReport(report).hasUnmappedFieldCount(1).containsUnmappedFields("Book", "missing");
141141
}
142142

143+
@Test // gh-1053
144+
void reportWorksForConnectionWithCustomNodeTypeName() {
145+
String schema = """
146+
type Query {
147+
paginatedBooks: BookConnection
148+
}
149+
type BookConnection {
150+
edges: [BookEdge]!
151+
pageInfo: PageInfo!
152+
}
153+
type BookEdge {
154+
cursor: String!
155+
node: MyBook!
156+
}
157+
type PageInfo {
158+
startCursor: String
159+
# ...
160+
}
161+
type MyBook {
162+
id: ID
163+
name: String
164+
missing: Boolean
165+
}
166+
""";
167+
SchemaReport report = inspectSchema(schema, BookController.class);
168+
assertThatReport(report).hasUnmappedFieldCount(1).containsUnmappedFields("MyBook", "missing");
169+
}
170+
143171
@Test
144172
void reportWorksForQueryWithExtensionType() {
145173
String schema = """

0 commit comments

Comments
 (0)