Skip to content
This repository has been archived by the owner on May 16, 2019. It is now read-only.

Scalars

Paweł Gutkowski edited this page Jul 28, 2017 · 1 revision

As defined by specification, scalar represents a primitive value in GraphQL. In KGraphQL, besides built-in scalar types, client code can declare custom scalar type, which can coerce to String, Boolean, Int, Long or Float (Kotlin Double).

KGraphQL provides group of DSL methods: stringScalar { }, booleanScalar { }, intScalar{ }, longScalar{ }, floatScalar{ }. They differ only by Kotlin primitive type they coerce to.

Scalar has to define its coercion functions deserialize and serialize or coercion object which implements correct subtype of com.github.pgutkowski.kgraphql.schema.scalar.ScalarCoercion.

Example of direct coercion functions declaration

stringScalar<UUID> {
    deserialize = { uuid : String -> UUID.fromString(uuid) }
    serialize = UUID::toString
}

Example of coercion object declaration

stringScalar<UUID> {
     coercion = object : StringScalarCoercion<UUID> {
             override fun serialize(instance: UUID): String = instance.toString()
             override fun deserialize(raw: String): UUID = UUID.fromString(raw)
         }
     }