Scalar Types

Scalar types represent leaf values in a GraphQL response. GraphQL provides five built-in scalar types:

  • Int — a signed 32-bit integer.
  • Float — a signed double-precision floating-point value.
  • String — a UTF-8 character sequence.
  • Boolean — true or false.
  • ID — an identifier intended to be serialized as a string.

Example:

type Product {
  id: ID!
  name: String!
  quantity: Int!
  price: Float!
  available: Boolean!
}

A scalar is a leaf because the client cannot select fields beneath it.

For example:

product {
  name
  price
}

is valid, but this is not:

product {
  price {
    value
  }
}

because Float has no child fields.

ID versus String

An ID represents an identifier. It is commonly used for database identifiers and resource identifiers. Although GraphQL serializes IDs in a string-compatible way, using ID communicates semantic intent: this value identifies something.

Integer versus Float

Use Int for whole numbers and Float for decimal numbers. Be careful with financial data: a GraphQL Float should not automatically be interpreted as an appropriate representation for exact monetary calculations. Production financial systems often use decimal representations at the application or database layer.

Built-in Scalars
Int: 32-bit signed integer
Suitable for integer values within the GraphQL Int range.
Float: Double-precision floating point
Used for decimal numeric values.
String: UTF-8 text
Represents textual values.
Boolean: true or false
Represents logical state.
ID: Identifier
Communicates that the value identifies an entity.
Key Takeaways
  • Scalars are GraphQL leaf values.
  • GraphQL provides five built-in scalar types.
  • ID communicates identifier semantics.
  • Scalar types cannot have nested selections.
Knowledge Check

Which built-in GraphQL scalar is intended specifically to represent identifiers?