S.
11/12/2023, 12:00 AMAll types within a GraphQL schema must have unique names. No two provided types may have the same name.
No provided type may have a name which conflicts with any built in types (including Scalar and Introspection types).
You have redefined the type 'LocalDateTime' from being a 'GraphQLScalarType' to a 'GraphQLScalarType'
S.
11/12/2023, 12:01 AMclass CustomSchemaGeneratorHooks : SchemaGeneratorHooks {
override fun willGenerateGraphQLType(type: KType): graphql.schema.GraphQLType? =
when (type.classifier as? KClass<*>) {
UUID::class -> CustomGraphQLScalar.graphqlUUID
LocalDateTime::class -> CustomGraphQLScalar.graphqlLocalDateTime
else -> null
}
}
val CustomGraphQLScalar.graphqlLocalDateTime: GraphQLScalarType
get() = GraphQLScalarType.newScalar()
.name("LocalDateTime")
.description("A type representing a formatted kotlinx.datetime.LocalDateTime")
.coercing(LocalDateTimeCoercing)
.build()
private object LocalDateTimeCoercing : Coercing<LocalDateTime, String> {
override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): LocalDateTime = runCatching {
LocalDateTime.parse(serialize(input, graphQLContext, locale))
}.getOrElse {
throw CoercingParseValueException("Expected valid LocalDateTime but was $input")
}
override fun parseLiteral(
input: Value<*>,
variables: CoercedVariables,
graphQLContext: GraphQLContext,
locale: Locale,
): LocalDateTime {
val localDateTimeString = (input as? StringValue)?.value
return runCatching {
LocalDateTime.parse(localDateTimeString!!)
}.getOrElse {
throw CoercingParseValueException("Expected valid LocalDateTime literal but was $localDateTimeString")
}
}
override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String =
runCatching {
dataFetcherResult.toString()
}.getOrElse {
throw CoercingParseValueException("Data fetcher result $dataFetcherResult cannot be serialized to a String")
}
}
S.
11/12/2023, 12:03 AMYou have redefined the type 'LocalDateTime' from being a 'GraphQLScalarType' to a 'GraphQLScalarType'
error?Dariusz Kuc
11/12/2023, 2:40 AMDariusz Kuc
11/12/2023, 2:43 AMgraphqlLocalDateTime
twice (guessing it is referenced from two different places) and as a result graphql-java
see two different objects (as they dont implement hashcode/equals)
Workaround/fix is to drop get()
and just declare the scalar as val (as otherwise you are constructing new scalar whenever referencing it)S.
11/12/2023, 11:24 AM