TExcelTAD
Home › GraphQL › Scalar Types

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:

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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:

  • ID says "this identifies an object". Client caches (Apollo, Relay) use id: ID fields to normalize data. It is not meant to be read by humans.
  • String is for text that has meaning: names, descriptions, URLs.

Dates, money and other values

There is no built-in Date, DateTime or Decimal. Common options:

  1. Send an ISO-8601 String ("2026-03-14T09:30:00Z") and document the format.
  2. Send money as an Int in minor units (cents) to avoid floating-point errors.
  3. Define a custom scalar (scalar DateTime) that validates and converts values. That's a later lesson.

Warning: Never use Float for money. 0.1 + 0.2 is 0.30000000000000004 in every language that uses IEEE-754 doubles.

Test yourself

How is a field of type ID serialized in the response?

  1. As a number
  2. As a string
  3. As whatever the resolver returned
  4. As base64

What happens when an Int resolver returns 3.5?

  1. It is rounded to 4
  2. It is truncated to 3
  3. The field is null and an error is added
  4. The query fails validation

What range does GraphQL's Int cover?

  1. 8-bit
  2. 32-bit signed
  3. 64-bit signed
  4. Unlimited