Skip to content

Commit

Permalink
fix(InputerValueSerializer): allow scalars to be assignable from class (
Browse files Browse the repository at this point in the history
#555)

* fix(InputerValueSerializer): allow scalars to be assignable from class

resolves #1522

* lint
  • Loading branch information
Cole Turner authored May 15, 2023
1 parent c153b6f commit 2c85ef0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ class InputValueSerializer(private val scalars: Map<Class<*>, Coercing<*, *>> =
return input
}

if (input::class.java in scalars) {
return scalars.getValue(input::class.java).valueToLiteral(input)
for (scalar in scalars.keys) {
if (input::class.java == scalar || scalar.isAssignableFrom(input::class.java)) {
return scalars[scalar]!!.valueToLiteral(input)
}
}

if (input::class in toStringClasses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import graphql.schema.Coercing
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.time.LocalDate
import java.util.Optional
import java.util.UUID
import java.time.ZoneId
import java.util.*

class GraphQLQueryRequestTest {
@Test
Expand Down Expand Up @@ -160,15 +160,16 @@ class GraphQLQueryRequestTest {
val query = TestNamedGraphQLQuery().apply {
input["movie"] = Movie(123, "greatMovie")
input["dateRange"] = DateRange(LocalDate.of(2020, 1, 1), LocalDate.of(2021, 5, 11))
input["zoneId"] = ZoneId.of("Europe/Berlin")
}
val request =
GraphQLQueryRequest(query, MovieProjection(), mapOf(DateRange::class.java to DateRangeScalar()))
GraphQLQueryRequest(query, MovieProjection(), mapOf(DateRange::class.java to DateRangeScalar(), ZoneId::class.java to ZoneIdScalar()))

val result = request.serialize()
assertValidQuery(result)
assertThat(result).isEqualTo(
"""query TestNamedQuery {
| test(movie: {movieId : 123, name : "greatMovie"}, dateRange: "01/01/2020-05/11/2021")
| test(movie: {movieId : 123, name : "greatMovie"}, dateRange: "01/01/2020-05/11/2021", zoneId: "Europe/Berlin")
|}
""".trimMargin()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
*
* Copyright 2020 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

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

import com.netflix.graphql.dgs.DgsScalar
import graphql.language.StringValue
import graphql.language.Value
import graphql.schema.Coercing
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import java.time.ZoneId
import java.time.format.DateTimeParseException
import java.time.zone.ZoneRulesException

@DgsScalar(name = "ZoneId")
class ZoneIdScalar : Coercing<ZoneId, String> {
@Throws(CoercingSerializeException::class)
override fun serialize(dataFetcherResult: Any): String {
return dataFetcherResult as? String
?: if (dataFetcherResult is ZoneId) {
dataFetcherResult.id
} else {
throw CoercingSerializeException("Expected type 'ZoneId', but was " + dataFetcherResult.javaClass.name)
}
}

@Throws(CoercingParseValueException::class)
override fun parseValue(input: Any): ZoneId {
return try {
if (input is String) {
ZoneId.of(input)
} else {
throw CoercingParseValueException("Expected a String")
}
} catch (e: DateTimeParseException) {
throw CoercingParseValueException(
String.format(
"A valid ZoneId must be provided. I.e. 'Europe/Berlin' or 'UTC'. Was: '%s'.",
input
),
e
)
} catch (e: ZoneRulesException) {
throw CoercingParseValueException(
String.format(
"A valid ZoneId must be provided. I.e. 'Europe/Berlin' or 'UTC'. Was: '%s'.",
input
),
e
)
}
}

@Throws(CoercingParseLiteralException::class)
override fun parseLiteral(input: Any): ZoneId {
return if (input is StringValue) {
try {
ZoneId.of(input.getValue())
} catch (e: DateTimeParseException) {
throw CoercingParseValueException(
String.format(
"A valid ZoneId must be provided. I.e. 'Europe/Berlin' or 'UTC'. Was: '%s'.",
input
),
e
)
} catch (e: ZoneRulesException) {
throw CoercingParseValueException(
String.format(
"A valid ZoneId must be provided. I.e. 'Europe/Berlin' or 'UTC'. Was: '%s'.",
input
),
e
)
}
} else {
throw CoercingParseLiteralException("Expected a StringValue.")
}
}

@Throws(CoercingParseLiteralException::class)
override fun valueToLiteral(input: Any): Value<out Value<*>> {
return StringValue.of(input.toString())
}
}

0 comments on commit 2c85ef0

Please sign in to comment.