What is the difference between scalar types and object types in GraphQL?
Understand GraphQL scalar types vs object types, why objects need selection sets, and when to define custom scalars — with clear, practical examples.
Expected Interview Answer
Scalar types are the leaf values of a GraphQL query that hold a single concrete value (Int, Float, String, Boolean, ID or a custom scalar), while object types are structured types made up of named fields, each of which resolves to another scalar or object.
Every GraphQL query resolves down a tree until it reaches scalars, which cannot be selected into further — they are the terminal nodes. Object types describe entities (like User or Product) and require you to select which sub-fields you want. You can define custom scalars (e.g. DateTime, JSON) with your own serialize, parseValue and parseLiteral logic to validate and coerce values, whereas object types are composed and often carry resolvers per field.
- Scalars enforce a single, validated primitive value
- Object types model structured, nested entities
- Custom scalars centralize format validation (dates, emails)
- Clear leaf-vs-branch distinction makes queries predictable
- Field selection is required on objects, forbidden on scalars
AI Mentor Explanation
A scalar type is like a single stat on the scoreboard — a batter's run count of 47 is one final number you cannot break down further. An object type is like the full batting card for that player, a structured record bundling name, runs, balls faced and strike rate, where each entry inside eventually resolves down to individual scalar numbers you can read off.
Step-by-Step Explanation
Step 1
Identify the leaf values
Any field that returns a single primitive — a number, string, boolean or ID — is a scalar and cannot have a sub-selection.
Step 2
Identify the structured entities
Fields that model a thing with its own named sub-fields (User, Order) are object types and require a selection set.
Step 3
Traverse the type tree
Every valid query is a path from a root object down through nested objects until it reaches scalar leaves.
Step 4
Add custom scalars where needed
Define custom scalars like DateTime or Email with serialize/parseValue/parseLiteral to validate non-standard primitives.
Step 5
Enforce selection rules
The server rejects queries that select sub-fields on a scalar or that leave an object field without a selection set.
What Interviewer Expects
- Knowledge of the five built-in scalars (Int, Float, String, Boolean, ID)
- Understanding that scalars are query leaves and objects are branches
- Awareness that objects require a selection set
- Ability to explain custom scalars and their coercion functions
- Clear real example of an object composed of scalars
Common Mistakes
- Thinking ID is just a String rather than a distinct serialized scalar
- Trying to select sub-fields on a scalar field
- Forgetting that object fields must have a selection set
- Believing you cannot define custom scalars
- Confusing enums with scalars or object types
Best Answer (HR Friendly)
“In GraphQL, scalar types are the plain single values like numbers, text or true/false that sit at the tips of a query. Object types are the containers that group related fields together, and when you ask for an object you have to say which of its inner fields you want, drilling down until you hit those simple scalar values.”
Code Example
scalar DateTime
type User {
id: ID!
name: String!
age: Int
isActive: Boolean!
createdAt: DateTime!
}
type Query {
user(id: ID!): User
}
# A query must select scalar leaves on the User object
query {
user(id: "42") {
id
name
age
isActive
createdAt
}
}Follow-up Questions
- How do you implement a custom scalar like DateTime?
- What is the difference between the ID scalar and String?
- Why must object fields include a selection set?
- How do enums relate to scalar types?
- What happens if a non-null scalar resolves to null?
MCQ Practice
1. Which of these is NOT a built-in GraphQL scalar type?
User is an object type you define; Int, Boolean and ID are built-in scalars.
2. What is required when a query field returns an object type?
Object type fields must include a selection set naming which sub-fields to return; scalars must not.
3. Custom scalars are typically used to?
Custom scalars centralize serialize/parseValue/parseLiteral logic to validate primitives such as DateTime or Email.
Flash Cards
What are the five built-in GraphQL scalars? — Int, Float, String, Boolean and ID.
Can a scalar field have a selection set? — No — scalars are query leaves and cannot be selected into further.
What defines an object type? — A named type composed of fields, each resolving to another scalar or object; it requires a selection set.
Why use a custom scalar? — To validate and coerce a non-standard primitive (DateTime, Email, JSON) via serialize/parseValue/parseLiteral.