Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
The Schema  ›  Lesson

Scalars and Enums

Scalars 12 minScalars are the single-value leaf types (Int, Float, String, Boolean, ID and any custom scalars), and enums are leaf types whose value must come from a fixed list, written bare in queries and as strings in JSON.

Scalars and Enums

A review's rating should be a whole number, a book's genre should be one of four known shelves, and its publication date should be a date. If the schema just said "string" for all three, every client would have to guess the format and every server would have to re-check it. Scalars and enums make those rules part of the contract.

The rule

Scalars are single values; enums are single values from a fixed list.

GraphQL has five built-in scalars:

Scalar Holds Bookshop example
Int A signed 32-bit whole number rating: Int!
Float A double-precision decimal price: Float!
String UTF-8 text title: String!
Boolean true or false inStock: Boolean!
ID A unique identifier id: ID!

ID is always sent as a string in responses, even if the database uses numbers. As an input it accepts a string or an integer, so book(id: 42) works and the resolver receives "42". Treat IDs as opaque labels, never as numbers to do maths with.

Scalars are checked on the way in. Passing 3.5 to an Int argument, or "five" to a Float, is an error before any resolver runs.

You can add custom scalars for values with a known format:

scalar Date @specifiedBy(url: "https://tools.ietf.org/html/rfc3339")

type Book {
  published: Date
}

The schema only declares the name; the server code decides how a Date is turned into JSON and how incoming values are checked. @specifiedBy points readers to the format's definition.

An enum is a scalar-like type whose value must be one of a listed set:

enum Genre {
  FANTASY
  MYSTERY
  SCIENCE
  HISTORY
}

Enum values are written as bare words in queries (books(genre: MYSTERY)) and appear as strings in JSON, in both responses and variables ("genre": "MYSTERY"). By convention they are UPPER_CASE. Anything outside the list, such as ROMANCE, is rejected, and tools can autocomplete the valid values.

The shortcut

For every leaf field, ask "what are the allowed values?" Any text → String. A whole number → Int. A known short list → enum. A special format → custom scalar. An identity → ID.

Typing the leaves of Review and BookStep 1 of 4
  1. rating

    Whole numbers 1 to 5: Int!. The range itself is checked in the resolver.

Drills

Answer fast — trust the rule.

quick drill

Which type fits a review's star rating from 1 to 5?

quick drill

A client calls book(id: 42). What value does the resolver receive for id?

quick drill

How does the genre appear in a JSON response?

quick drill

A request sends the variables { "genre": "ROMANCE" } for a Genre argument. What happens?

quick drill

What does scalar Date in a schema define on its own?

Remember

  • Scalars are single values; enums are single values from a fixed list.
  • Ask what values are allowed: text, whole number, short list, special format or identity.
  • Trap: enum values are bare in queries but strings in JSON, and IDs are strings, not numbers.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainType a new field

The shop adds a format field to Book, which can be paperback, hardback or ebook, and a pageCount field. Choose a type for each, write the SDL, and explain how a client would filter by format in a query and in a variables JSON.