Jilles van Gurp
04/08/2021, 2:04 PM@Component
to the example for custom scalars. Without that, it doesn't work for obvious reasons.
• The documentation suggests this should only work with things like strings, which seems to be not the case
• Maybe generalize the approach below to have kotlinx.serialization and maybe jackson strategies for coercion. Mostly that should work and users can simply reuse their existing serialization/deserialization logic that way. Or if that is supported some other way, maybe document that?
@Component
class GeometryGraphqlScalar : SchemaGeneratorHooks {
override fun willGenerateGraphQLType(type: KType): GraphQLType? {
return when (type.classifier as? KClass<*>) {
Geometry::class -> geometryScalar
else -> null
}
}
}
private val geometryScalar = GraphQLScalarType
.newScalar()
.name("Geometry")
.description("json serialized geometry")
.coercing(GeometryCoercing)
.build()
object GeometryCoercing : Coercing<Geometry, Geometry?> {
override fun serialize(input: Any): Geometry {
// little hack, causes toString to be called, which we implemented using Json.encodeToString ...
return input as Geometry
}
override fun parseValue(input: Any): Geometry {
error("we don't support parameter scalars currently but we could ${input::class.qualifiedName}")
}
override fun parseLiteral(input: Any): Geometry {
error("we don't support parameter scalars currently but we could ${input::class.qualifiedName}")
}
}
Dariusz Kuc
04/08/2021, 3:11 PMBigInteger
and various Datetime
implementations, see https://github.com/graphql-java/graphql-java-extended-scalars) but they have to serialize as Strings (https://spec.graphql.org/June2018/#sec-Scalars)Dariusz Kuc
04/08/2021, 3:14 PMDariusz Kuc
04/08/2021, 3:15 PMDariusz Kuc
04/08/2021, 3:17 PMDariusz Kuc
04/08/2021, 3:20 PMJilles van Gurp
04/08/2021, 3:35 PMDariusz Kuc
04/08/2021, 3:48 PMDariusz Kuc
04/08/2021, 3:48 PMDariusz Kuc
04/08/2021, 3:49 PMDariusz Kuc
04/08/2021, 3:50 PMJilles van Gurp
04/09/2021, 12:35 PMJilles van Gurp
04/09/2021, 12:48 PMDariusz Kuc
04/09/2021, 5:43 PM