Scalars
To view this content, buy the book! 😃🙏
Or if you’ve already purchased.
Scalars
Scalars are primitive values. There are five included scalar types:
Int
: Signed 32-bit non-fractional number. Maximum value around 2 billion (2,147,483,647).Float
: Signed double-precision (64-bit) fractional value.String
: Sequence of UTF-8 (8-bit Unicode) characters.Boolean
:true
orfalse
.ID
: Unique identifier, serialized as a string.
We can also define our own scalars, like Url
and DateTime
. In the description of our custom scalars, we write how they’re serialized so the frontend developer knows what value to provide for arguments. For instance, DateTime
could be serialized as an integer (milliseconds since Epoch) or as an ISO string:
scalar DateTime
type Mutation {
dayOfTheWeek(when: DateTime): String
}
Given the above schema, the client would send one of the below operations, depending on the definition of DateTime
:
# if DateTime is serialized as an integer
mutation {
dayOfTheWeek(when: 1591028749941)
}
# if DateTime is serialized as an ISO string
mutation {
dayOfTheWeek(when: "2020-06-01T16:25:49.941Z")
}
The benefits to using custom scalars are clarity (when: DateTime
is clearer than when: Int
) and consistent validation (whatever value we pass is checked to make sure it’s a valid DateTime
).
We define our own custom scalar in Chapter 11.