Skip to content

Commit 8fd7495

Browse files
committed
[client] update ScalarConverter for 3.x.x to accept any objects (ExpediaGroup#819)
1 parent f553dd2 commit 8fd7495

File tree

6 files changed

+28
-20
lines changed

6 files changed

+28
-20
lines changed

docs/client/client-customization.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ val customObjectMapper = jacksonObjectMapper()
8383
val client = GraphQLClient(url = URL("http://localhost:8080/graphql"), mapper = customObjectMapper)
8484
```
8585

86-
## Deprecated Field Usage
86+
## Deprecated Field Usage
8787

8888
Build plugins will automatically fail generation of a client if any of the specified query files are referencing
8989
deprecated fields. This ensures that your clients have to explicitly opt-in into deprecated usage by specifying
9090
`allowDeprecatedFields` configuration option.
9191

92-
## Custom GraphQL Scalars
92+
## Custom GraphQL Scalars
9393

9494
By default, custom GraphQL scalars are serialized and [type-aliased](https://kotlinlang.org/docs/reference/type-aliases.html)
9595
to a String. GraphQL Kotlin plugins also support custom serialization based on provided configuration.
@@ -104,8 +104,8 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
104104
import java.util.UUID
105105

106106
class UUIDScalarConverter : ScalarConverter<UUID> {
107-
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
108-
override fun toJson(value: UUID): String = value.toString()
107+
override fun toScalar(rawValue: Any ): UUID = UUID.fromString(rawValue)
108+
override fun toJson(value: UUID): Any = value.toString()
109109
}
110110
```
111111

examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/hooks/CustomSchemaGeneratorHooks.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,24 @@ internal val graphqlUUIDType = GraphQLScalarType.newScalar()
6868
.build()
6969

7070
private object UUIDCoercing : Coercing<UUID, String> {
71-
override fun parseValue(input: Any?): UUID = UUID.fromString(
72-
serialize(
73-
input
74-
)
75-
)
71+
override fun parseValue(input: Any): UUID = runCatching {
72+
UUID.fromString(serialize(input))
73+
}.getOrElse {
74+
throw CoercingParseValueException("Expected valid UUID but was $input")
75+
}
7676

77-
override fun parseLiteral(input: Any?): UUID? {
77+
override fun parseLiteral(input: Any): UUID? {
7878
val uuidString = (input as? StringValue)?.value
79-
return UUID.fromString(uuidString)
79+
return runCatching {
80+
UUID.fromString(uuidString)
81+
}.getOrElse {
82+
throw CoercingParseLiteralException("Expected valid UUID literal but was $uuidString")
83+
}
8084
}
8185

82-
override fun serialize(dataFetcherResult: Any?): String = dataFetcherResult.toString()
86+
override fun serialize(dataFetcherResult: Any): String = runCatching {
87+
dataFetcherResult.toString()
88+
}.getOrElse {
89+
throw CoercingSerializeException("Data fetcher result $dataFetcherResult cannot be serialized to a String")
90+
}
8391
}

graphql-kotlin-client/src/main/kotlin/com/expediagroup/graphql/client/converter/ScalarConverter.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ package com.expediagroup.graphql.client.converter
2222
interface ScalarConverter<T> {
2323

2424
/**
25-
* Deserialize raw JSON String value to a typesafe value.
25+
* Deserialize raw JSON value to a typesafe value.
2626
*/
27-
fun toScalar(rawValue: String): T
27+
fun toScalar(rawValue: Any): T
2828

2929
/**
30-
* Serialize typesafe scalar value to a raw JSON string.
30+
* Serialize typesafe scalar value to a raw JSON value.
3131
*/
32-
fun toJson(value: T): String
32+
fun toJson(value: T): Any
3333
}

plugins/graphql-kotlin-gradle-plugin/src/test/resources/mocks/UUIDScalarConverter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
44
import java.util.UUID
55

66
class UUIDScalarConverter : ScalarConverter<UUID> {
7-
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
8-
override fun toJson(value: UUID): String = value.toString()
7+
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
8+
override fun toJson(value: UUID): Any = value.toString()
99
}

plugins/graphql-kotlin-plugin-core/src/main/kotlin/com/expediagroup/graphql/plugin/generator/types/generateGraphQLCustomScalarTypeSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal fun generateGraphQLCustomScalarTypeSpec(context: GraphQLClientGenerator
7171
FunSpec.builder("create")
7272
.addAnnotation(JsonCreator::class.java)
7373
.jvmStatic()
74-
.addParameter("rawValue", String::class)
74+
.addParameter("rawValue", Any::class)
7575
.addStatement("return %L(%N.toScalar(rawValue))", customScalarName, converter)
7676
.build()
7777
)

plugins/graphql-kotlin-plugin-core/src/test/kotlin/com/expediagroup/graphql/plugin/generator/types/GenerateGraphQLCustomScalarTypeSpecIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class GenerateGraphQLCustomScalarTypeSpecIT {
6363
6464
@JsonCreator
6565
@JvmStatic
66-
fun create(rawValue: String) = UUID(converter.toScalar(rawValue))
66+
fun create(rawValue: Any) = UUID(converter.toScalar(rawValue))
6767
}
6868
}
6969

0 commit comments

Comments
 (0)