Skip to content

Commit

Permalink
feat: allow to set default InputValueSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkolek committed Apr 18, 2024
1 parent 95f27dc commit 2085141
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,34 @@

package com.netflix.graphql.dgs.codegen.cases.input.test

import com.netflix.graphql.dgs.client.codegen.InputValueSerializer
import com.netflix.graphql.dgs.codegen.InputValueSerializerProvider
import com.netflix.graphql.dgs.codegen.cases.input.expected.DgsClient
import com.netflix.graphql.dgs.codegen.cases.input.expected.types.MovieFilter
import graphql.language.StringValue
import graphql.language.Value
import graphql.schema.Coercing
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class QueryTest {
private val movieCoercing = object : Coercing<MovieFilter, String> {
override fun serialize(filter: Any): String {
return (filter as? MovieFilter)?.genre ?: ""
}

override fun parseValue(input: Any): MovieFilter {
return MovieFilter(input.toString())
}

override fun parseLiteral(input: Any): MovieFilter {
return MovieFilter(input.toString())
}

override fun valueToLiteral(input: Any): Value<*> {
return StringValue.of(serialize(input))
}
}

@Test
fun testQueryWithNoFilter() {
Expand Down Expand Up @@ -109,4 +131,53 @@ class QueryTest {
query
)
}

@Test
fun testQueryWithFilterAndCustomCoercing() {
try {
InputValueSerializerProvider.serializer = InputValueSerializer(
mapOf(MovieFilter::class.java to movieCoercing)
)
val query = DgsClient.buildQuery {
movies(filter = MovieFilter(genre = "horror"))
}

Assertions.assertEquals(
"""{
| __typename
| movies(filter: "horror")
|}
|
""".trimMargin(),
query
)
} finally {
InputValueSerializerProvider.reset()
}
}

@Test
fun testResetInputValueSerializerProvider() {
try {
InputValueSerializerProvider.serializer = InputValueSerializer(
mapOf(MovieFilter::class.java to movieCoercing)
)
InputValueSerializerProvider.reset()

val query = DgsClient.buildQuery {
movies(filter = MovieFilter(genre = "horror"))
}
Assertions.assertEquals(
"""{
| __typename
| movies(filter: {genre : "horror"})
|}
|
""".trimMargin(),
query
)
} finally {
InputValueSerializerProvider.reset()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.netflix.graphql.dgs.codegen

import com.netflix.graphql.dgs.client.codegen.InputValue
import com.netflix.graphql.dgs.client.codegen.InputValueSerializer
import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface
import graphql.language.Argument
import graphql.language.AstPrinter
import graphql.language.Document
Expand Down Expand Up @@ -50,12 +51,25 @@ object DefaultTracker {
}
}

object InputValueSerializerProvider {
private val default: ThreadLocal<InputValueSerializerInterface> = ThreadLocal.withInitial { InputValueSerializer() }

var serializer
get(): InputValueSerializerInterface = default.get()
set(value) = default.set(value)

fun reset() {
serializer = InputValueSerializer()
}
}

@QueryProjectionMarker
abstract class GraphQLProjection(defaultFields: Set<String> = setOf("__typename")) {

companion object {
private val inputSerializer: InputValueSerializerInterface
get() = InputValueSerializerProvider.serializer

private val inputSerializer = InputValueSerializer()
companion object {

@JvmStatic
protected fun <T> default0(arg: String): T? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package com.netflix.graphql.dgs.client.codegen

import graphql.scalars.id.UUIDScalar
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.UUID

class InputValueSerializerTest {

Expand Down Expand Up @@ -151,6 +153,15 @@ class InputValueSerializerTest {
assertThat(serialize).isEqualTo("ACTION")
}

@Test
fun `UUID value`() {
val expected = UUID.randomUUID()
val actual = InputValueSerializer(
mapOf(UUID::class.java to UUIDScalar.INSTANCE.coercing)
).serialize(expected)
assertThat(actual).isEqualTo(""""$expected"""")
}

@Test
fun `overridden properties are serialized`() {
abstract class Base {
Expand Down

0 comments on commit 2085141

Please sign in to comment.