Scalar & Data types
A scalar in an indivisible value (which means it doesn't contain fields).
It can be seen as a primitive type such as in JVM.
Create a scalar
It is possible to define your own scalars based on your needs.
To do you, add it in your schema like this:
scalar Date
type Mutation {
addMovie(title: String!, directorName: String!, releaseDate: Date!): Movie!
}
To write your custom scalar, you need to specify its serialization like this:
import kotlinx.datetime.Instant
arianeServer {
scalar<Instant, String>("Date") {
description = "A custom scalar that handles dates"
serialize { result -> (result as? Instant)?.toString() }
parseValue { input -> (input as? String)?.let(Instant::parse) }
parseLiteral { input -> (input as? StringValue)?.value?.let(Instant::parse) }
}
}.launch()
You can then use this scalar in your resolver.
import kotlinx.datetime.Instant
fun main() = arianeServer {
resolvers {
Mutation {
resolve("addMovie") {
val releaseDate: Instant = arguments["releaseDate"]
val title = arguments["title"]
val directorName = arguments["director"]
val movie = Movie(title, Director(directorName), releaseDate)
movies.add(movie)
movie
}
}
}
}.launch()
Data types
GraphQL already specifies 5 official scalars which are:
GraphQL type | Kotlin type |
---|---|
String | String |
Boolean | Boolean |
Int | Int |
Float | Float |
ID | String |
Ariane comes with a bunch of custom scalars:
GraphQL type | Kotlin type |
---|---|
Long | Long |
Short | Short |
Byte | Byte |
BigDecimal | java.math.BigDecimal |
BigInteger | java.math.BigInteger |
Note you still have to declare them in your schema if you use them:
Example:
scalar Long
scalar Byte