Skip to content

Commit 6093f9e

Browse files
Merge pull request #560 from Shalima/master
Support to use SelectionSet in GraphqlQueryRequest to serialize
2 parents bb27d82 + 1830a86 commit 6093f9e

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

graphql-dgs-codegen-shared-core/src/main/kotlin/com/netflix/graphql/dgs/client/codegen/GraphQLQueryRequest.kt

+16-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ import graphql.language.OperationDefinition
2323
import graphql.language.SelectionSet
2424
import graphql.schema.Coercing
2525

26-
class GraphQLQueryRequest(
26+
class GraphQLQueryRequest @JvmOverloads constructor(
2727
val query: GraphQLQuery,
28-
val projection: BaseProjectionNode?,
29-
scalars: Map<Class<*>, Coercing<*, *>>?
28+
val projection: BaseProjectionNode? = null,
29+
scalars: Map<Class<*>, Coercing<*, *>>? = null
3030
) {
3131

32-
constructor(query: GraphQLQuery) : this(query, null, null)
33-
constructor(query: GraphQLQuery, projection: BaseProjectionNode?) : this(query, projection, null)
32+
private var selectionSet: SelectionSet? = null
33+
34+
@JvmOverloads constructor(query: GraphQLQuery, selectionSet: SelectionSet, scalars: Map<Class<*>, Coercing<*, *>>? = null) : this(query = query, scalars = scalars) {
35+
this.selectionSet = selectionSet
36+
}
37+
3438
val inputValueSerializer = InputValueSerializer(scalars ?: emptyMap())
3539
val projectionSerializer = ProjectionSerializer(inputValueSerializer)
3640

@@ -54,16 +58,20 @@ class GraphQLQueryRequest(
5458
}
5559

5660
if (projection != null) {
57-
val selectionSet = if (projection is BaseSubProjectionNode<*, *> && projection.root() != null) {
61+
val selectionSetFromProjection = if (projection is BaseSubProjectionNode<*, *> && projection.root() != null) {
5862
projectionSerializer.toSelectionSet(projection.root() as BaseProjectionNode)
5963
} else {
6064
projectionSerializer.toSelectionSet(projection)
6165
}
62-
if (selectionSet.selections.isNotEmpty()) {
63-
selection.selectionSet(selectionSet)
66+
if (selectionSetFromProjection.selections.isNotEmpty()) {
67+
selection.selectionSet(selectionSetFromProjection)
6468
}
6569
}
6670

71+
if (selectionSet != null) {
72+
selection.selectionSet(selectionSet)
73+
}
74+
6775
operationDef.selectionSet(SelectionSet.newSelectionSet().selection(selection.build()).build())
6876

6977
return AstPrinter.printAst(operationDef.build())

graphql-dgs-codegen-shared-core/src/test/kotlin/com/netflix/graphql/dgs/client/codegen/GraphQLQueryRequestTest.kt

+49
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,55 @@ class GraphQLQueryRequestTest {
155155
)
156156
}
157157

158+
@Test
159+
fun serializeWithSelectionSet() {
160+
val query = TestNamedGraphQLQuery().apply {
161+
input["movie"] = Movie(123, "greatMovie")
162+
}
163+
val inputValueSerializer = InputValueSerializer(emptyMap())
164+
val projectionSerializer = ProjectionSerializer(inputValueSerializer)
165+
val selectionSet = projectionSerializer.toSelectionSet(MovieProjection().name().movieId())
166+
val request = GraphQLQueryRequest(query, selectionSet)
167+
val result = request.serialize()
168+
assertValidQuery(result)
169+
assertThat(result).isEqualTo(
170+
"""query TestNamedQuery {
171+
| test(movie: {movieId : 123, name : "greatMovie"}) {
172+
| name
173+
| movieId
174+
| }
175+
|}
176+
""".trimMargin()
177+
)
178+
}
179+
180+
@Test
181+
fun serializeWithSelectionSetAndScalars() {
182+
val query = TestNamedGraphQLQuery().apply {
183+
input["movie"] = Movie(123, "greatMovie")
184+
input["dateRange"] = DateRange(LocalDate.of(2020, 1, 1), LocalDate.of(2021, 5, 11))
185+
input["zoneId"] = ZoneId.of("Europe/Berlin")
186+
}
187+
val scalars = mapOf(DateRange::class.java to DateRangeScalar(), ZoneId::class.java to ZoneIdScalar())
188+
val inputValueSerializer = InputValueSerializer(scalars)
189+
val projectionSerializer = ProjectionSerializer(inputValueSerializer)
190+
val selectionSet = projectionSerializer.toSelectionSet(MovieProjection().name().movieId())
191+
val request =
192+
GraphQLQueryRequest(query, selectionSet, scalars)
193+
194+
val result = request.serialize()
195+
assertValidQuery(result)
196+
assertThat(result).isEqualTo(
197+
"""query TestNamedQuery {
198+
| test(movie: {movieId : 123, name : "greatMovie"}, dateRange: "01/01/2020-05/11/2021", zoneId: "Europe/Berlin") {
199+
| name
200+
| movieId
201+
| }
202+
|}
203+
""".trimMargin()
204+
)
205+
}
206+
158207
@Test
159208
fun serializeWithScalar() {
160209
val query = TestNamedGraphQLQuery().apply {

0 commit comments

Comments
 (0)