why does `query {sessions(dateRange: {range: "2024...
# graphql-kotlin
s
why does
query {sessions(dateRange: {range: "2024-06-28/2024-07-28"})}
work, but if I use a variable instead
query ($dateRange: DateRangeInput) {sessions(dateRange: $dateRange)}
( with
"dateRange": {"range": "2024-06-28/2024-07-28"}
) it does not?
Copy code
data class DateRange(val range: ClosedRange<LocalDate>)

val graphqlDateInterval: GraphQLScalarType = GraphQLScalarType.newScalar()
    .name("DateInterval")
    .description("A type representing an ISO-8601 formatted date interval")
    .coercing(DateIntervalCoercing)
    .build()

private object DateIntervalCoercing : Coercing<ClosedRange<LocalDate>, String> {
    override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): ClosedRange<LocalDate> =
        runCatching {
            val string = serialize(input, graphQLContext, locale)
            LocalDate.parse(string.substringBefore('/'))..LocalDate.parse(string.substringAfter('/'))
        }.getOrElse {
            throw CoercingParseValueException("Expected valid DateInterval but was $input")
        }

    override fun parseLiteral(
        input: Value<*>,
        variables: CoercedVariables,
        graphQLContext: GraphQLContext,
        locale: Locale,
    ): ClosedRange<LocalDate> {
        val dateIntervalString = (input as? StringValue)?.value
        return runCatching {
            LocalDate.parse(dateIntervalString!!.substringBefore('/'))..
                    LocalDate.parse(dateIntervalString.substringAfter('/'))
        }.getOrElse {
            throw CoercingParseValueException("Expected valid DateInterval literal but was $dateIntervalString")
        }
    }

    override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String =
        runCatching {
            dataFetcherResult as ClosedRange<LocalDate>
            dataFetcherResult.start.toString() + "/" + dataFetcherResult.endInclusive.toString()
        }.getOrElse { throw CoercingParseValueException("Data fetcher result $dataFetcherResult cannot be serialized to a String") }
}
this is the custom scalar and it fails with
Variable 'dateRange' has an invalid value: Expected valid DateInterval but was 2024-06-28/2024-07-28
for the latter query
m
I would suggest doing it.printStackTrace() inside getOrElse so you can see what the actual error is
d
I believe this is related to https://github.com/ExpediaGroup/graphql-kotlin/issues/1445 Looks like variables are parsed differently than args -> you could try unwrapping the values in custom data fetcher (https://github.com/ExpediaGroup/graphql-kotlin/blob/master/generator/graphql-kotli[…]expediagroup/graphql/generator/execution/FunctionDataFetcher.kt ) but unsure whether that is really feasible or not