Bird
Raised Fist0
GraphQLquery~10 mins

Type definitions in GraphQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Type definitions
Start schema
↓
Define types
↓
Add fields with types
↓
Use scalar or custom types
↓
Schema ready for queries/mutations
GraphQL type definitions start by defining types, then adding fields with scalar or custom types, preparing the schema for queries and mutations.
Execution Sample
GraphQL
type Book {
  id: ID!
  title: String!
  author: Author
}

type Author {
  id: ID!
  name: String!
}
Defines two types, Book and Author, with fields and their types, including required fields using !.
Execution Table
StepActionType/FieldType AssignedNotes
1Define typeBookObject TypeStart defining Book type
2Add fieldidID!Unique identifier, required
3Add fieldtitleString!Book title, required
4Add fieldauthorAuthorReference to Author type, optional
5Define typeAuthorObject TypeStart defining Author type
6Add fieldidID!Unique identifier, required
7Add fieldnameString!Author name, required
8Schema complete--Types ready for queries and mutations
💡 All types and fields defined with their types; schema is ready for use.
Variable Tracker
Type/FieldStartAfter Step 1After Step 4After Step 7Final
BookundefinedObject Type definedFields: id(ID!), title(String!), author(Author)Fields unchangedComplete
AuthorundefinedundefinedundefinedObject Type defined with fields id(ID!), name(String!)Complete
Key Moments - 2 Insights
Why does the exclamation mark (!) appear after some types?
The exclamation mark means the field is required (non-null). See execution_table rows 2, 3, 6, and 7 where fields have ! indicating they must have a value.
Can a field reference another type?
Yes, fields can reference other types as in row 4 where 'author' field in Book references the Author type.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type is assigned to the 'title' field in the Book type?
AID!
BString!
CAuthor
DString
💡 Hint
Check execution_table row 3 under 'Type Assigned' column.
At which step is the Author type fully defined with its fields?
AStep 7
BStep 6
CStep 4
DStep 8
💡 Hint
Look at execution_table rows 5 to 7 for Author type field additions.
If the 'author' field in Book was marked as 'Author!' instead of 'Author', what would that mean?
AThe author field is a scalar type
BThe author field is optional
CThe author field is required (non-null)
DThe author field is a list
💡 Hint
Recall the meaning of ! from key_moments and execution_table rows 2 and 3.
Concept Snapshot
GraphQL type definitions:
- Use 'type TypeName { fields }' to define types
- Fields have 'name: Type' format
- '!' means field is required (non-null)
- Fields can reference scalar or custom types
- Types form schema for queries and mutations
Full Transcript
This visual execution traces how GraphQL type definitions are built step-by-step. We start by defining a type named Book, then add fields like id with type ID! meaning required unique identifier, title as required String, and author referencing another type Author. Next, we define Author type with id and name fields, both required. The exclamation mark means the field cannot be null. Fields can reference other types, allowing nested data structures. After all fields are added, the schema is ready for queries and mutations. The execution table shows each step, the variable tracker shows how types and fields build up. Key moments clarify common confusions about required fields and type references. The quiz tests understanding of field types, definition steps, and meaning of required fields. This helps beginners see how GraphQL schemas are constructed visually and clearly.

Practice

(1/5)
1. What is the main purpose of type definitions in GraphQL?
easy
A. To describe the shape and types of data clients can request
B. To execute database queries directly
C. To style the user interface of a GraphQL client
D. To store data permanently in the server

Solution

  1. Step 1: Understand the role of type definitions

    Type definitions specify what data types exist and how data is structured in GraphQL.
  2. Step 2: Identify what clients use type definitions for

    Clients use type definitions to know what data they can ask for and how it will look.
  3. Final Answer:

    To describe the shape and types of data clients can request -> Option A
  4. Quick Check:

    Type definitions = Describe data shape [OK]
Hint: Type definitions describe data structure, not execution or styling [OK]
Common Mistakes:
  • Confusing type definitions with query execution
  • Thinking type definitions style UI
  • Assuming type definitions store data
2. Which of the following is the correct way to define a simple GraphQL object type named Book with a title field of type String?
easy
A. define Book { title: String }
B. object Book { title: String }
C. Book type { title: String }
D. type Book { title: String }

Solution

  1. Step 1: Recall GraphQL syntax for object types

    GraphQL uses the keyword type followed by the type name and fields in braces.
  2. Step 2: Match the correct syntax

    type Book { title: String } matches the correct syntax: type Book { title: String }.
  3. Final Answer:

    type Book { title: String } -> Option D
  4. Quick Check:

    Object type syntax = type Name { fields } [OK]
Hint: Remember: 'type' keyword defines object types in GraphQL [OK]
Common Mistakes:
  • Using 'object' instead of 'type'
  • Placing 'type' after the name
  • Using 'define' keyword which is invalid
3. Given the following GraphQL type definition:
type User {
  id: ID!
  name: String
  age: Int
}

Which of these queries will return the name and age fields of a user?
medium
A. { user { id name age } }
B. { user { name age } }
C. { user { id } }
D. { user { age id } }

Solution

  1. Step 1: Identify requested fields in each query

    We want only name and age fields returned.
  2. Step 2: Check which query requests exactly those fields

    { user { name age } } requests name and age only, matching the requirement.
  3. Final Answer:

    { user { name age } } -> Option B
  4. Quick Check:

    Query fields = name and age only [OK]
Hint: Select only needed fields inside braces after type name [OK]
Common Mistakes:
  • Including extra fields like 'id' when not asked
  • Requesting fields in wrong order (order doesn't matter but distracts)
  • Missing required fields if needed
4. Identify the error in this GraphQL type definition:
type Product {
  id: ID
  price: Float!
  name: String!
  description: String
  price: Int
}
medium
A. Missing exclamation mark on 'id' field
B. Incorrect type 'Float!' for price
C. Duplicate field name 'price' with different types
D. Description field cannot be optional

Solution

  1. Step 1: Review all fields for correctness

    Notice that 'price' is declared twice with different types: Float! and Int.
  2. Step 2: Understand GraphQL rules on field names

    Field names must be unique within a type; duplicates cause errors.
  3. Final Answer:

    Duplicate field name 'price' with different types -> Option C
  4. Quick Check:

    Duplicate fields cause errors [OK]
Hint: Check for repeated field names in type definitions [OK]
Common Mistakes:
  • Ignoring duplicate field names
  • Thinking missing '!' is always an error
  • Assuming optional fields are invalid
5. You want to define a GraphQL type Order that includes an id (non-null ID), a list of items (each an Item type), and a total price (non-null Float). Which is the correct type definition?
hard
A. type Order { id: ID! items: [Item!]! total: Float! }
B. type Order { id: ID items: Item total: Float }
C. type Order { id: ID! items: [Item]! total: Float! }
D. type Order { id: ID! items: [Item!] total: Float }

Solution

  1. Step 1: Analyze each field requirement

    'id' must be non-null ID, 'items' is a non-null list of non-null Item, 'total' is non-null Float.
  2. Step 2: Match the correct syntax

    type Order { id: ID! items: [Item!]! total: Float! } correctly uses ID!, [Item!]! (list non-null and items non-null), and Float!.
  3. Final Answer:

    type Order { id: ID! items: [Item!]! total: Float! } -> Option A
  4. Quick Check:

    Non-null fields and list syntax correct [OK]
Hint: Use [Type!]! for non-null list of non-null items [OK]
Common Mistakes:
  • Missing non-null markers (!)
  • Using list without non-null item type
  • Confusing list syntax with single type
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