Hello everyone! I'm trying to write some custom sc...
# graphql-kotlin
r
Hello everyone! I'm trying to write some custom scalar for LocalDate but I'm currently stuck with an issue I'm having some real trouble solving. I'm sending in some data to my mutator, along with it a date string formatted like "1989-04-24", and I'm met with an exception telling me `Cannot construct instance of
java.time.LocalDate
(no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)`
Copy code
object DateCoercing : Coercing<Date, String> {

  override fun parseValue(input: Any?): Date = try {
    LocalDate.parse(serialize(input))
  } catch (e: DateTimeParseException) {
    throw CoercingParseValueException("...", e)
  }

  override fun parseLiteral(input: Any?): Date? = try {
    (input as? StringValue)?.value?.let { LocalDate.parse(it) }
  } catch (e: DateTimeParseException) {
    throw CoercingParseLiteralException("...", e)
  }

  override fun serialize(dataFetcherResult: Any?): String = dataFetcherResult.toString()
}
I've tried most of the common solutions found by Googling to no avail...
r
you are mixing
java.util.Date
and
java.time.LocalDate
is that intendend?
r
Sorry, that's an alias for LocalDate
d
have you registered it in your hooks?
exception implies that Jackson attempts to deserialize it directly instead of using the coercing logic
r
Copy code
@Component
class CustomSchemaGeneratorHooks : SchemaGeneratorHooks {

  override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier as? KClass<*>) {
    Date::class -> graphqlDateType
    else -> null
  }
}

val graphqlDateType: GraphQLType? = GraphQLScalarType.newScalar()
  .name("Date")
  .description("...")
  .coercing(DateCoercing)
  .build()
This should be picked up automatically, correct?
d
your hook is for
Date::class
yet you are trying to coerce
java.time.LocalDate
are you sure you are not mixing the two?
r
As I mentioned above, Date is an alias for LocalDate as I'm also using
java.time.Instant
as a custom scalar that I've given the alias
DateTime
.
d
error implies that there is a mismatch somewhere so I’d double check on that
try removing aliases first and verify it works
then once it works you can try re-introducing them
r
I've removed type aliases now and the result is the same, sadly.
l
does it need to be
Date::class.java
in the
when
expression? i keep doing the wrong thing and immediately forgetting which after i figure it out
nope, it’s just
::class
… got it wrong again
d
well you can also try configuring jackson with https://github.com/FasterXML/jackson-modules-java8/tree/master/datetime but it should work without it
r
I was using version 1.4.2... for some reason. Updated to 3.1.1 to see if that fixed it and it's now working without needing to change anything!
d
👍