0
0
GraphQLquery~10 mins

Mocking resolvers in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking resolvers
Define schema with types
Create mock resolvers for fields
Query execution triggers resolver
Mock resolver returns fake data
Query returns mocked data to client
This flow shows how GraphQL queries use mock resolvers to return fake data based on the schema.
Execution Sample
GraphQL
type Query {
  book: Book
}

type Book {
  title: String
  author: String
}

// Mock resolver
const mocks = {
  Query: { book: () => ({ title: "Mock Title", author: "Mock Author" }) }
};
Defines a simple schema and a mock resolver that returns fake book data when queried.
Execution Table
StepActionResolver CalledReturned ValueQuery Result
1Start query for 'book'NoN/AN/A
2Resolver for Query.book calledQuery.book{ title: "Mock Title", author: "Mock Author" }N/A
3Resolver for Book.title calledBook.title"Mock Title""title": "Mock Title"
4Resolver for Book.author calledBook.author"Mock Author""author": "Mock Author"
5Assemble final responseNoN/A{ book: { title: "Mock Title", author: "Mock Author" } }
💡 Query completes after all fields resolved with mocked data.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
bookundefined{ title: "Mock Title", author: "Mock Author" }{ title: "Mock Title", author: "Mock Author" }{ title: "Mock Title", author: "Mock Author" }{ title: "Mock Title", author: "Mock Author" }
titleundefinedundefined"Mock Title""Mock Title""Mock Title"
authorundefinedundefinedundefined"Mock Author""Mock Author"
Key Moments - 2 Insights
Why does the Query.book resolver return an object instead of a simple string?
Because the 'book' field returns a Book type object, the resolver must return an object with fields 'title' and 'author' as shown in execution_table step 2.
How does the mock resolver know what fields to provide for the Book type?
The mock resolver returns an object matching the Book type shape. The GraphQL engine then calls resolvers for each field like 'title' and 'author' as in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does the Query.book resolver return at step 2?
A"Mock Title"
B{ title: "Mock Title", author: "Mock Author" }
C"Mock Author"
Dundefined
💡 Hint
Check the 'Returned Value' column at step 2 in the execution_table.
At which step does the resolver for the 'author' field return its value?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Resolver Called' column to find when 'Book.author' is resolved.
If the mock resolver for Query.book returned null, what would the final query result be?
A{ book: { title: "Mock Title" } }
BAn error would occur
C{ book: null }
D{ book: {} }
💡 Hint
Consider what happens when a resolver returns null for an object field in GraphQL.
Concept Snapshot
Mocking resolvers in GraphQL:
- Define schema types
- Create mock resolvers returning fake data
- Query triggers resolvers
- Resolvers return mocked values
- Query returns mocked data to client
Full Transcript
This visual execution shows how mocking resolvers work in GraphQL. First, a schema defines types like Query and Book. Then, mock resolvers are created to return fake data for these types. When a query requests the 'book' field, the Query.book resolver is called and returns a mock object with title and author. Next, the GraphQL engine calls resolvers for each field of Book, returning mocked strings. Finally, the query response assembles all mocked data and returns it to the client. This process helps developers test and develop without real data sources.