Skip to content

[client 3.x.x] update ScalarConverter included in 3.x.x to accept any objects (#819) #926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/client/client-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ val customObjectMapper = jacksonObjectMapper()
val client = GraphQLClient(url = URL("http://localhost:8080/graphql"), mapper = customObjectMapper)
```

## Deprecated Field Usage
## Deprecated Field Usage

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

## Custom GraphQL Scalars
## Custom GraphQL Scalars

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

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/plugins/gradle-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/plugins/maven-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import com.expediagroup.graphql.directives.KotlinDirectiveWiringFactory
import com.expediagroup.graphql.hooks.SchemaGeneratorHooks
import graphql.language.StringValue
import graphql.schema.Coercing
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingSerializeException
import graphql.schema.GraphQLScalarType
import graphql.schema.GraphQLType
import org.springframework.beans.factory.BeanFactoryAware
Expand Down Expand Up @@ -68,16 +71,24 @@ internal val graphqlUUIDType = GraphQLScalarType.newScalar()
.build()

private object UUIDCoercing : Coercing<UUID, String> {
override fun parseValue(input: Any?): UUID = UUID.fromString(
serialize(
input
)
)
override fun parseValue(input: Any): UUID = runCatching {
UUID.fromString(serialize(input))
}.getOrElse {
throw CoercingParseValueException("Expected valid UUID but was $input")
}

override fun parseLiteral(input: Any?): UUID? {
override fun parseLiteral(input: Any): UUID? {
val uuidString = (input as? StringValue)?.value
return UUID.fromString(uuidString)
return runCatching {
UUID.fromString(uuidString)
}.getOrElse {
throw CoercingParseLiteralException("Expected valid UUID literal but was $uuidString")
}
}

override fun serialize(dataFetcherResult: Any?): String = dataFetcherResult.toString()
override fun serialize(dataFetcherResult: Any): String = runCatching {
dataFetcherResult.toString()
}.getOrElse {
throw CoercingSerializeException("Data fetcher result $dataFetcherResult cannot be serialized to a String")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ package com.expediagroup.graphql.client.converter
interface ScalarConverter<T> {

/**
* Deserialize raw JSON String value to a typesafe value.
* Deserialize raw JSON value to a typesafe value.
*/
fun toScalar(rawValue: String): T
fun toScalar(rawValue: Any): T

/**
* Serialize typesafe scalar value to a raw JSON string.
* Serialize typesafe scalar value to a raw JSON value.
*/
fun toJson(value: T): String
fun toJson(value: T): Any
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal fun generateGraphQLCustomScalarTypeSpec(context: GraphQLClientGenerator
FunSpec.builder("create")
.addAnnotation(JsonCreator::class.java)
.jvmStatic()
.addParameter("rawValue", String::class)
.addParameter("rawValue", Any::class)
.addStatement("return %L(%N.toScalar(rawValue))", customScalarName, converter)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class GenerateGraphQLCustomScalarTypeSpecIT {
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import io.ktor.client.request.HttpRequestBuilder
import kotlin.Any
import kotlin.String
import kotlin.Unit
import kotlin.jvm.JvmStatic
Expand Down Expand Up @@ -63,7 +64,7 @@ class GenerateGraphQLCustomScalarTypeSpecIT {

@JsonCreator
@JvmStatic
fun create(rawValue: String) = UUID(converter.toScalar(rawValue))
fun create(rawValue: Any) = UUID(converter.toScalar(rawValue))
}
}

Expand Down Expand Up @@ -117,6 +118,7 @@ class GenerateGraphQLCustomScalarTypeSpecIT {
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import io.ktor.client.request.HttpRequestBuilder
import kotlin.Any
import kotlin.Int
import kotlin.String
import kotlin.Unit
Expand Down Expand Up @@ -146,7 +148,7 @@ class GenerateGraphQLCustomScalarTypeSpecIT {

@JsonCreator
@JvmStatic
fun create(rawValue: String) = UUID(converter.toScalar(rawValue))
fun create(rawValue: Any) = UUID(converter.toScalar(rawValue))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
```

Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-3.5.0/plugins/gradle-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
```

Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-3.5.0/plugins/maven-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ import com.expediagroup.graphql.client.converter.ScalarConverter
import java.util.UUID

class UUIDScalarConverter : ScalarConverter<UUID> {
override fun toScalar(rawValue: String): UUID = UUID.fromString(rawValue)
override fun toJson(value: UUID): String = value.toString()
override fun toScalar(rawValue: Any): UUID = UUID.fromString(rawValue.toString())
override fun toJson(value: UUID): Any = value.toString()
}
```

Expand Down