Scalar Types
Scalars are the leaves of every query: the values that have no sub-fields. GraphQL ships with five built-in scalars, and each one has precise rules for what it accepts and what it returns.
| Scalar | Represents | Example values |
|---|---|---|
Int |
signed 32-bit integer | 0, -17, 2147483647 |
Float |
double-precision number | 3.14, -0.5, 2 |
String |
UTF-8 text | "hello", "日本" |
Boolean |
true or false | true, false |
ID |
unique identifier, serialized as a string | "42", "usr_8f3a" |
Output coercion: what the server sends
When a resolver returns a value, the engine serializes it into the field's scalar type. Some conversions are allowed; nonsense is rejected with an error:
import { graphql, buildSchema } from 'graphql';
const schema = buildSchema(`
type Query {
asInt: Int
asFloat: Float
asString: String
asBoolean: Boolean
asId: ID
}
`);
const rootValue = {
asInt: 7.0, // whole-number float -> 7
asFloat: 2, // int -> 2
asString: 123, // number -> "123"
asBoolean: true,
asId: 42, // number -> "42"
};
const result = await graphql({
schema,
source: '{ asInt asFloat asString asBoolean asId }',
rootValue,
});
console.log(JSON.stringify(result.data));{"asInt":7,"asFloat":2,"asString":"123","asBoolean":true,"asId":"42"}
Note that ID always comes out as a string, even when your database uses numbers. Clients should treat ids as opaque strings.
When serialization fails
If a resolver returns something that can't be represented, that field becomes null and an error explains why:
import { graphql, buildSchema } from 'graphql';
const schema = buildSchema(`
type Query { a: Int, b: Int, c: Boolean, d: Int }
`);
const rootValue = {
a: 3.5, // not a whole number
b: 3000000000, // too big for 32 bits
c: 'yes', // strings are not booleans
d: '12', // numeric strings are accepted for Int
};
const result = await graphql({ schema, source: '{ a b c d }', rootValue });
console.log(JSON.stringify(result.data));
for (const error of result.errors) console.log(`${error.path}: ${error.message}`);{"a":null,"b":null,"c":null,"d":12}
a: Int cannot represent non-integer value: 3.5
b: Int cannot represent non 32-bit signed integer value: 3000000000
c: Boolean cannot represent a non boolean value: "yes"
Int is limited to 32 bits because many languages (and JSON parsers) can't safely handle bigger integers. For larger counts, use Float, a String, or a custom scalar such as BigInt.
Input coercion: what clients may send
Inputs (literal arguments and variables) are checked more strictly than outputs: no strings for numbers, no fractional ints. One convenience: an Int is accepted where a Float is expected, and both strings and integers are accepted for ID:
import { graphql, buildSchema } from 'graphql';
const schema = buildSchema(`
type Query { echoFloat(x: Float): Float, echoId(id: ID): String, echoInt(n: Int): Int }
`);
const rootValue = {
echoFloat: ({ x }) => x,
echoId: ({ id }) => `${typeof id}:${id}`,
echoInt: ({ n }) => n,
};
for (const source of [
'{ echoFloat(x: 5) }',
'{ echoId(id: 99) }',
'{ echoId(id: "abc") }',
'{ echoInt(n: 1.5) }',
'{ echoInt(n: "3") }',
]) {
const result = await graphql({ schema, source, rootValue });
console.log(source.padEnd(24), result.errors ? result.errors[0].message : JSON.stringify(result.data));
}{ echoFloat(x: 5) } {"echoFloat":5}
{ echoId(id: 99) } {"echoId":"string:99"}
{ echoId(id: "abc") } {"echoId":"string:abc"}
{ echoInt(n: 1.5) } Int cannot represent non-integer value: 1.5
{ echoInt(n: "3") } Int cannot represent non-integer value: "3"
ID vs String
Both serialize as strings, but they communicate different intent:
IDsays "this identifies an object". Client caches (Apollo, Relay) useid: IDfields to normalize data. It is not meant to be read by humans.Stringis for text that has meaning: names, descriptions, URLs.
Dates, money and other values
There is no built-in Date, DateTime or Decimal. Common options:
- Send an ISO-8601
String("2026-03-14T09:30:00Z") and document the format. - Send money as an
Intin minor units (cents) to avoid floating-point errors. - Define a custom scalar (
scalar DateTime) that validates and converts values. That's a later lesson.
Warning: Never use
Floatfor money.0.1 + 0.2is0.30000000000000004in every language that uses IEEE-754 doubles.
Test yourself
How is a field of type ID serialized in the response?
- As a number
- As a string
- As whatever the resolver returned
- As base64
What happens when an Int resolver returns 3.5?
- It is rounded to 4
- It is truncated to 3
- The field is
nulland an error is added - The query fails validation
What range does GraphQL's Int cover?
- 8-bit
- 32-bit signed
- 64-bit signed
- Unlimited