Scalars are equivalent to primitive data types in a programming language. In GraphQL, there are five built-in scalar types:
- Boolean, true or false
- Int, a signed 32‐bit numeric non‐fractional value
- Float, a signed double‐precision fractional values
- String, a sequence of UTF‐8 characters
- ID, a unique identifier
A scalar simply represents a single value and are the basic building blocks of your schema. This is in comparison to object types, which represent a collection of values. An object type has fields, and each field has a type which may be a scalar or an object type itself. If the field's type is an object, that object will have fields that are also scalars or other objects, and so on. In this way we end up with a tree-like structure in both our schema and the queries made against it.
query { # <- The "root" of the tree
movies {
actors {
name # <- A "leaf" on the tree
}
crew {
name # <- Another "leaf"
}
}
}
Enums are similar to scalars, in that an enum represents a single, concrete value. However, each enum type is defined explicitly in the schema (there are no "built-in" enums) and its definition must include a set of values that the enum type can be. For example, we might create an enum like:
enum Animal {
Cat
Dog
Bird
}
A field that returns an Animal
will still have a single value, like a scalar, but that value will be either "Cat", "Dog" or "Bird" -- no other values are allowed.
A leaf type is a term that encompasses both scalars and enums. Leaf types represent the leaves or terminating points in our tree-like queries and their responses.
When a field returns an object type (like movies
, actors
, or crew
in the above example), we have to tell GraphQL which of the object type's fields we want to query. This selection of fields is called a selection set and is wrapped by curly brackets. In our example, name
is the selection set for the crew
field, crew
and actors
are the selection set for the movies
field, and even the movies
field is part of the selection set for the query root type.
The important thing to notice here is that leaf types do not have fields, so any field that returns a leaf type will not have a selection set.
For more information, you can check out the official spec.