Skip to content

Commit 025cb3a

Browse files
committed
fix(codegen): make startCursor and endCursor optional, as the edges may be empty
1 parent aa8f6d1 commit 025cb3a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

graphql-kotlin-toolkit-codegen/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/generator/fieldResolver/PaginationFieldResolverGenerator.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,18 @@ internal class PaginationFieldResolverGenerator(
8888
)
8989
getFunSpec.addStatement("val result = resolve(${resolveArgs}env = internalEnv)")
9090

91-
getFunSpec.addStatement("val edges = result.data.map { %T(it, resolveCursor(it, internalEnv)) }", generatedMapper.getPaginationEdgeClassName())
9291
getFunSpec.addStatement(
93-
"val pageInfo = %T(result.hasNextPage, result.hasPreviousPage, edges.first().cursor, edges.last().cursor)",
92+
"val edges = result.data.map { %T(it, resolveCursor(it, internalEnv)) }",
93+
generatedMapper.getPaginationEdgeClassName()
94+
)
95+
getFunSpec.addStatement(
96+
"val pageInfo = %T(result.hasNextPage, result.hasPreviousPage, edges.firstOrNull()?.cursor, edges.lastOrNull()?.cursor)",
9497
generatedMapper.getPaginationPageInfoClassName()
9598
)
96-
getFunSpec.addStatement("return %T(edges, pageInfo)", generatedMapper.getPaginationConnectionClassName())
99+
getFunSpec.addStatement(
100+
"return %T(edges, pageInfo)",
101+
generatedMapper.getPaginationConnectionClassName()
102+
)
97103
}
98104
.build())
99105
}

graphql-kotlin-toolkit-codegen/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/generator/pagination/PaginationPageInfoGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal class PaginationPageInfoGenerator(
2020
override val dataProperties: List<DataProperty> = listOf(
2121
DataProperty("hasNextPage", GraphQLNonNull(Scalars.GraphQLBoolean)),
2222
DataProperty("hasPreviousPage", GraphQLNonNull(Scalars.GraphQLBoolean)),
23-
DataProperty("startCursor", GraphQLNonNull(Scalars.GraphQLString)),
24-
DataProperty("endCursor", GraphQLNonNull(Scalars.GraphQLString))
23+
DataProperty("startCursor", Scalars.GraphQLString),
24+
DataProperty("endCursor", Scalars.GraphQLString)
2525
)
2626
}

0 commit comments

Comments
 (0)