Bird
Raised Fist0
GraphQLquery~10 mins

Scalar types (String, Int, Float, Boolean, ID) in GraphQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Scalar types (String, Int, Float, Boolean, ID)
Start: Define Scalar Type
↓
Choose one of: String, Int, Float, Boolean, ID
↓
Assign value matching type
↓
Use value in query or mutation
↓
GraphQL validates type
↓
Return or store value
↓
End
This flow shows how a scalar type is chosen, assigned a value, validated by GraphQL, and then used in queries or mutations.
Execution Sample
GraphQL
type Query {
  getUserName(id: ID!): String
}

query {
  getUserName(id: "123")
}
This GraphQL query requests a user name by providing an ID of type ID, expecting a String result.
Execution Table
StepActionInput/ValueType CheckedResult/Output
1Receive argument 'id'"123"IDValid ID string
2Validate argument type"123"IDPasses validation
3Execute resolver getUserNameid = "123"IDReturns user name as String
4Return result to client"Alice"StringQuery result: "Alice"
5End of query execution---
💡 Query completes after returning a String result matching the schema.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
idundefined"123""123""123"
resultundefinedundefined"Alice""Alice"
Key Moments - 2 Insights
Why must the ID type value be a string and not a number?
The ID scalar in GraphQL is serialized as a string to allow flexibility (like UUIDs). In the execution_table step 1 and 2, the value "123" is treated as a string, not a number.
What happens if the returned value does not match the declared scalar type?
GraphQL will raise a validation error before sending the response. In step 4, the returned value must be a String as declared, or the query fails.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what type is the input value "123" checked against?
AString
BInt
CID
DBoolean
💡 Hint
Check the 'Type Checked' column at step 2 in the execution_table.
At which step does the resolver return the user name as a String?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Result/Output' columns in the execution_table.
If the id argument was given as 123 (number) instead of "123" (string), what would happen?
AIt would pass validation as ID accepts numbers
BIt would fail validation because ID expects a string
CIt would convert automatically to string
DIt would return a Boolean false
💡 Hint
Refer to key_moments about ID type and execution_table step 2 validation.
Concept Snapshot
GraphQL Scalar Types:
- String: text data
- Int: whole numbers
- Float: decimal numbers
- Boolean: true or false
- ID: unique identifier (string)
Use scalar types to define simple data fields and validate inputs/outputs.
Full Transcript
This visual execution trace shows how GraphQL scalar types work, focusing on the ID and String types. The flow starts by defining a scalar type, then assigning a value that matches the type. The example query requests a user name by ID. The execution table traces each step: receiving the ID argument as a string, validating it as ID, executing the resolver to get a String user name, and returning the result. Variables like 'id' and 'result' are tracked through the steps. Key moments clarify why ID is a string and the importance of matching return types. The quiz tests understanding of type checking and validation. The snapshot summarizes the five main scalar types and their use in GraphQL schemas.

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

  1. Step 1: Recall standard GraphQL scalar types

    The standard scalar types are String, Int, Float, Boolean, and ID.
  2. Step 2: Identify the non-standard type

    Date is not a built-in scalar type in GraphQL; it requires a custom scalar.
  3. Final Answer:

    Date -> Option C
  4. 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

  1. Step 1: Understand GraphQL type syntax

    Types are case-sensitive; ID must be uppercase and non-null indicated by !.
  2. Step 2: Check each option

    userId: ID! uses correct type 'ID!' with uppercase and non-null. Others are either lowercase or wrong type.
  3. Final Answer:

    userId: ID! -> Option D
  4. 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

  1. Step 1: Understand GraphQL Float type

    Float represents decimal numbers but can also return whole numbers as floats.
  2. 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.
  3. Final Answer:

    Float -> Option A
  4. 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

  1. Step 1: Check scalar type capitalization

    GraphQL scalar types are case-sensitive; 'Float' must be capitalized.
  2. Step 2: Verify other types

    'Boolean' and 'ID' are correctly capitalized; 'String!' is valid for non-null.
  3. Final Answer:

    'float' should be 'Float' with uppercase F -> Option B
  4. 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

  1. Step 1: Understand the purpose of ID scalar

    ID is designed for unique identifiers and is serialized as string.
  2. Step 2: Ensure non-null constraint

    Adding ! means the field cannot be null, which fits the requirement.
  3. Final Answer:

    ID! -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard