Scalar types (String, Int, Float, Boolean, ID) in GraphQL - Time & Space Complexity
When working with scalar types in GraphQL, it's important to understand how the time to process these values changes as the amount of data grows.
We want to know how the cost of handling these simple data types scales when many values are involved.
Analyze the time complexity of the following GraphQL query fetching scalar fields.
query GetUsers {
users {
id
name
age
isActive
}
}
This query requests scalar fields (ID, String, Int, Boolean) for each user in the list.
Look for repeated actions in the query processing.
- Primary operation: Reading scalar fields for each user.
- How many times: Once per user in the list.
As the number of users grows, the total work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reading 10 users' scalar fields |
| 100 | Reading 100 users' scalar fields |
| 1000 | Reading 1000 users' scalar fields |
Pattern observation: The work increases evenly as the number of users increases.
Time Complexity: O(n)
This means the time to fetch scalar fields grows directly with the number of items requested.
[X] Wrong: "Fetching scalar fields is instant and does not depend on the number of items."
[OK] Correct: Even simple fields must be read for each item, so more items mean more work.
Understanding how simple data types scale helps you explain performance clearly and shows you grasp the basics of data fetching costs.
"What if we added a nested list of posts for each user? How would the time complexity change?"
Practice
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 CQuick Check:
Standard scalars exclude Date [OK]
- Thinking Date is built-in
- Confusing ID with Int
- Assuming Boolean is custom
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 DQuick Check:
ID type is uppercase and non-null with ! [OK]
- Using lowercase 'id'
- Confusing ID with String
- Omitting ! for required fields
{ price: 19 }What is the actual type of the returned value?
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 AQuick Check:
Float fields return numbers as floats, even whole numbers [OK]
- Assuming 19 is Integer
- Thinking Float must have decimal point
- Confusing with String
type Product {
id: ID
name: String!
price: float
inStock: Boolean
}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 BQuick Check:
Scalar types must be capitalized exactly [OK]
- Using lowercase scalar types
- Changing Boolean case
- Removing ! from non-null
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 AQuick Check:
Use ID! for unique, non-null string identifiers [OK]
- Using String without !
- Using Int for string IDs
- Using Boolean for IDs
Ready to prove it?
Pass the test and this pattern moves unknown
