Skip to content

Commit 0d6ced0

Browse files
author
Shane Myrick
committed
Revert "[client] update ScalarConverter to accept any objects (ExpediaGroup#819)"
This reverts commit 1804e6a
1 parent 447d651 commit 0d6ced0

File tree

8 files changed

+25
-43
lines changed

8 files changed

+25
-43
lines changed

clients/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 value to a typesafe value.
25+
* Deserialize raw JSON String value to a typesafe value.
2626
*/
27-
fun toScalar(rawValue: Any): T
27+
fun toScalar(rawValue: String): T
2828

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

docs/client/client-customization.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ val customObjectMapper = jacksonObjectMapper()
150150
val client = GraphQLClient(url = URL("http://localhost:8080/graphql"), mapper = customObjectMapper)
151151
```
152152

153-
## Deprecated Field Usage
153+
## Deprecated Field Usage
154154

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

159-
## Custom GraphQL Scalars
159+
## Custom GraphQL Scalars
160160

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

173173
class UUIDScalarConverter : ScalarConverter<UUID> {
174-
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
175-
override fun toJson(value: UUID): Any = value.toString()
174+
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
175+
override fun toJson(value: UUID): String = value.toString()
176176
}
177177
```
178178

@@ -189,4 +189,6 @@ graphql {
189189
}
190190
```
191191

192-
See [Gradle](../plugins/gradle-plugin.md) and [Maven](../plugins/maven-plugin.md) plugin documentation for additional details.
192+
See [Gradle](../plugins/gradle-plugin.md)
193+
and [Maven](../plugins/maven-plugin.md)
194+
plugin documentation for additional details.

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ package com.expediagroup.graphql.examples.hooks
1818

1919
import com.expediagroup.graphql.directives.KotlinDirectiveWiringFactory
2020
import com.expediagroup.graphql.hooks.SchemaGeneratorHooks
21-
import graphql.Scalars
2221
import graphql.language.StringValue
2322
import graphql.schema.Coercing
24-
import graphql.schema.CoercingParseLiteralException
25-
import graphql.schema.CoercingParseValueException
26-
import graphql.schema.CoercingSerializeException
2723
import graphql.schema.GraphQLScalarType
2824
import graphql.schema.GraphQLType
2925
import org.springframework.beans.factory.BeanFactoryAware
3026
import reactor.core.publisher.Mono
31-
import java.math.BigDecimal
3227
import java.util.UUID
3328
import kotlin.reflect.KClass
3429
import kotlin.reflect.KType
@@ -44,7 +39,6 @@ class CustomSchemaGeneratorHooks(override val wiringFactory: KotlinDirectiveWiri
4439
*/
4540
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
4641
UUID::class -> graphqlUUIDType
47-
BigDecimal::class -> Scalars.GraphQLBigDecimal
4842
else -> null
4943
}
5044

@@ -74,24 +68,16 @@ internal val graphqlUUIDType = GraphQLScalarType.newScalar()
7468
.build()
7569

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

83-
override fun parseLiteral(input: Any): UUID? {
77+
override fun parseLiteral(input: Any?): UUID? {
8478
val uuidString = (input as? StringValue)?.value
85-
return runCatching {
86-
UUID.fromString(uuidString)
87-
}.getOrElse {
88-
throw CoercingParseLiteralException("Expected valid UUID literal but was $uuidString")
89-
}
79+
return UUID.fromString(uuidString)
9080
}
9181

92-
override fun serialize(dataFetcherResult: Any): String = runCatching {
93-
dataFetcherResult.toString()
94-
}.getOrElse {
95-
throw CoercingSerializeException("Data fetcher result $dataFetcherResult cannot be serialized to a String")
96-
}
82+
override fun serialize(dataFetcherResult: Any?): String = dataFetcherResult.toString()
9783
}

examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/query/ScalarQuery.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import com.expediagroup.graphql.spring.operations.Mutation
2121
import com.expediagroup.graphql.spring.operations.Query
2222
import com.expediagroup.graphql.scalars.ID
2323
import org.springframework.stereotype.Component
24-
import java.math.BigDecimal
2524
import java.util.UUID
26-
import kotlin.random.Random
2725

2826
/**
2927
* Simple query that exposes custom scalar.
@@ -41,9 +39,6 @@ class ScalarQuery : Query {
4139

4240
@GraphQLDescription("generates random GraphQL ID")
4341
fun generateRandomId() = ID(UUID.randomUUID().toString())
44-
45-
@GraphQLDescription("generates random BigDecimal")
46-
fun generateRandomBigDecimal(): BigDecimal = BigDecimal(Random.nextLong())
4742
}
4843

4944
@Component

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: Any): UUID = UUID.fromString(rawValue.toString())
8-
override fun toJson(value: UUID): Any = value.toString()
7+
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
8+
override fun toJson(value: UUID): String = value.toString()
99
}

plugins/graphql-kotlin-maven-plugin/src/integration/complete-setup/src/main/kotlin/com/example/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: Any): UUID = UUID.fromString(rawValue.toString())
8-
override fun toJson(value: UUID): Any = value.toString()
7+
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
8+
override fun toJson(value: UUID): String = 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", Any::class)
74+
.addParameter("rawValue", String::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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class GenerateGraphQLCustomScalarTypeSpecIT {
3535
import com.expediagroup.graphql.types.GraphQLResponse
3636
import com.fasterxml.jackson.annotation.JsonCreator
3737
import com.fasterxml.jackson.annotation.JsonValue
38-
import kotlin.Any
3938
import kotlin.String
4039
import kotlin.jvm.JvmStatic
4140
@@ -62,7 +61,7 @@ class GenerateGraphQLCustomScalarTypeSpecIT {
6261
6362
@JsonCreator
6463
@JvmStatic
65-
fun create(rawValue: Any) = UUID(converter.toScalar(rawValue))
64+
fun create(rawValue: String) = UUID(converter.toScalar(rawValue))
6665
}
6766
}
6867

0 commit comments

Comments
 (0)