[3.6.5] - Custom Schema Hooks / graphql-java-datetime #920
-
Using https://github.com/donbeave/graphql-java-datetime import com.zhokhov.graphql.datetime.GraphQLLocalDate
import com.zhokhov.graphql.datetime.GraphQLLocalDateTime
import graphql.language.StringValue
import graphql.schema.Coercing
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import graphql.schema.GraphQLScalarType
import graphql.schema.GraphQLType
import org.springframework.stereotype.Component
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Period
import javax.money.CurrencyUnit
import javax.money.Monetary
import kotlin.reflect.KClass
import kotlin.reflect.KType
@Component
class CustomSchemaGeneratorHooks : SchemaGeneratorHooks {
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier as? KClass<*>) {
LocalDateTime::class -> GraphQLLocalDateTime()
LocalDate::class -> GraphQLLocalDate()
CurrencyUnit::class -> currencyUnit
Period::class -> periodType
else -> null
}
Leads to the following exception on startup:
Workaround: private val _localDateTimeType = object : GraphQLLocalDateTime() {
override fun hashCode(): Int = Objects.hash(name)
override fun equals(other: Any?): Boolean = Objects.equals(this.hashCode(), other.hashCode())
}
private val _localDateType = object : GraphQLLocalDate() {
override fun hashCode(): Int = Objects.hash(name)
override fun equals(other: Any?): Boolean = Objects.equals(this.hashCode(), other.hashCode())
}
/**
* Workaround to prevent duplicate registration
*/
val localDateTimeType: GraphQLScalarType =
GraphQLScalarType.Builder()
.name("LocalDateTime")
.description(LocalDateTime::class.qualifiedName)
.coercing(_localDateTimeType.coercing)
.build()
/**
* Workaround to prevent duplicate registration
*/
val localDateType: GraphQLScalarType =
GraphQLScalarType.Builder()
.name("LocalDate")
.description(LocalDate::class.qualifiedName)
.coercing(_localDateType.coercing)
.build()
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello 👋 As you noticed, underlying issue is due to the fact that Since in order to generate the the valid schema you need to explicitly provide those mappings in the hooks, I believe that if you disable the |
Beta Was this translation helpful? Give feedback.
Hello 👋
As you noticed, underlying issue is due to the fact that
graphql-java
types do not implement hashcode/equals so if you try to add the same type twice it will blow up. I haven't looked too deep into the linkedgraphql-java-datetime
library but it looks like it has autoconfiguration that will attempt to automatically register those extended scalars which in turn conflicts with your schema hook declaration.Since in order to generate the the valid schema you need to explicitly provide those mappings in the hooks, I believe that if you disable the
graphql-java-datetime
autoconfiguration class it should work as well.