Scalar types are the basic building blocks for data in GraphQL. They represent simple values like text, numbers, or true/false.
When you want to store a person's name as text.
When you need to save an age as a whole number.
When you want to record a price with decimals.
When you need to store a true or false value, like if a user is active.
When you want to uniquely identify an object with an ID.
Syntax
GraphQL
type Example {
name: String
age: Int
price: Float
isActive: Boolean
id: ID
}
String is for text like names or descriptions.
ID is a unique identifier, often used for objects.
Examples
This defines a person with two text fields for names.
GraphQL
type Person {
firstName: String
lastName: String
}
This defines a product with a decimal price and whole number quantity.
GraphQL
type Product {
price: Float
quantity: Int
}
This defines a user with a true/false status and a unique ID.
GraphQL
type User {
isActive: Boolean
id: ID
}
Sample Program
This example shows a Book type with different scalar fields for common data.
GraphQL
type Book {
title: String
pages: Int
rating: Float
isPublished: Boolean
bookId: ID
}
OutputSuccess
Important Notes
Scalar types cannot contain other fields or objects.
Use ID when you want a unique identifier, but it is treated like a string.
Boolean values are only true or false, no other options.
Summary
Scalar types represent simple data values in GraphQL.
Common scalar types are String, Int, Float, Boolean, and ID.
They help define the shape of data you can query or send.
Practice
(1/5)
1. Which of the following is NOT a standard GraphQL scalar type?
easy
A. Boolean
B. String
C. Date
D. ID
Solution
Step 1: Recall standard GraphQL scalar types
The standard scalar types are String, Int, Float, Boolean, and ID.
Step 2: Identify the non-standard type
Date is not a built-in scalar type in GraphQL; it requires a custom scalar.
Final Answer:
Date -> Option C
Quick Check:
Standard scalars exclude Date [OK]
Hint: Remember only 5 built-in scalars: String, Int, Float, Boolean, ID [OK]
Common Mistakes:
Thinking Date is built-in
Confusing ID with Int
Assuming Boolean is custom
2. Which of the following is the correct way to declare a field of type ID in a GraphQL schema?
easy
A. userId: id
B. userId: Int
C. userId: String!
D. userId: ID!
Solution
Step 1: Understand GraphQL type syntax
Types are case-sensitive; ID must be uppercase and non-null indicated by !.
Step 2: Check each option
userId: ID! uses correct type 'ID!' with uppercase and non-null. Others are either lowercase or wrong type.
Final Answer:
userId: ID! -> Option D
Quick Check:
ID type is uppercase and non-null with ! [OK]
Hint: ID type is uppercase and ! means non-null [OK]
Common Mistakes:
Using lowercase 'id'
Confusing ID with String
Omitting ! for required fields
3. Given this GraphQL query result for a field declared as Float: { price: 19 } What is the actual type of the returned value?
medium
A. Float
B. String
C. Integer
D. Boolean
Solution
Step 1: Understand GraphQL Float type
Float represents decimal numbers but can also return whole numbers as floats.
Step 2: Analyze the value 19
Even though 19 looks like an integer, GraphQL returns it as a Float type because the field is Float.
Final Answer:
Float -> Option A
Quick Check:
Float fields return numbers as floats, even whole numbers [OK]
Hint: Float fields always return float type, even if whole number [OK]
Common Mistakes:
Assuming 19 is Integer
Thinking Float must have decimal point
Confusing with String
4. Identify the error in this GraphQL schema snippet:
type Product {
id: ID
name: String!
price: float
inStock: Boolean
}
medium
A. 'Boolean' should be lowercase 'boolean'
B. 'float' should be 'Float' with uppercase F
C. 'ID' should be 'Id' with lowercase d
D. 'String!' should be 'String' without exclamation mark
Solution
Step 1: Check scalar type capitalization
GraphQL scalar types are case-sensitive; 'Float' must be capitalized.
Step 2: Verify other types
'Boolean' and 'ID' are correctly capitalized; 'String!' is valid for non-null.
Final Answer:
'float' should be 'Float' with uppercase F -> Option B
Quick Check:
Scalar types must be capitalized exactly [OK]
Hint: Scalar types always start with uppercase letter [OK]
Common Mistakes:
Using lowercase scalar types
Changing Boolean case
Removing ! from non-null
5. You want to store a unique identifier for users that is a string but should never be null. Which scalar type and modifier combination is best in GraphQL schema?
hard
A. ID!
B. String
C. Int!
D. Boolean!
Solution
Step 1: Understand the purpose of ID scalar
ID is designed for unique identifiers and is serialized as string.
Step 2: Ensure non-null constraint
Adding ! means the field cannot be null, which fits the requirement.
Final Answer:
ID! -> Option A
Quick Check:
Use ID! for unique, non-null string identifiers [OK]
Hint: Use ID! for unique, required string IDs [OK]
Common Mistakes:
Using String without !
Using Int for string IDs
Using Boolean for IDs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.