Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Mocking Resolvers in GraphQL
📖 Scenario: You are building a simple GraphQL API for a bookstore. Before connecting to a real database, you want to create mock resolvers to simulate data fetching. This helps frontend developers start working without waiting for the backend to be fully ready.
🎯 Goal: Create a GraphQL schema for books and authors, then write mock resolvers that return fixed sample data for queries.
📋 What You'll Learn
Define a GraphQL schema with types Book and Author
Create a query type with books and authors fields
Write mock resolver functions that return fixed arrays of books and authors
Connect the schema and resolvers to create a working mock GraphQL server
💡 Why This Matters
🌍 Real World
Mocking resolvers lets frontend developers work with a GraphQL API before the real database and backend logic are ready.
💼 Career
Understanding how to mock GraphQL resolvers is useful for rapid prototyping, testing, and collaboration between frontend and backend teams.
Progress0 / 4 steps
1
Define the GraphQL schema
Create a GraphQL schema string called typeDefs with these exact types: Book with fields id: ID!, title: String!, authorId: ID!; Author with fields id: ID!, name: String!; and a Query type with fields books: [Book!]! and authors: [Author!]!.
GraphQL
Hint
Use the gql template literal to define your schema string.
2
Create mock data arrays
Create two constant arrays called books and authors. books should contain exactly two objects with keys id, title, and authorId with values '1', 'The Great Gatsby', '1' and '2', '1984', '2'. authors should contain exactly two objects with keys id and name with values '1', 'F. Scott Fitzgerald' and '2', 'George Orwell'.
GraphQL
Hint
Use arrays of objects with exact keys and values as specified.
3
Write mock resolver functions
Create a constant object called resolvers with a Query field. Inside Query, add two functions: books and authors. Each function should return the corresponding array books or authors.
GraphQL
Hint
Use arrow functions inside the Query object to return the arrays.
4
Create the Apollo Server with mocks
Create a constant called server by instantiating ApolloServer with an object containing typeDefs and resolvers. Use const { ApolloServer } = require('apollo-server'); at the top. This completes the mock GraphQL server setup.
GraphQL
Hint
Import ApolloServer and create a new instance with your schema and resolvers.
Practice
(1/5)
1. What is the main purpose of mocking resolvers in GraphQL?
easy
A. To simulate API responses without needing real data
B. To optimize database queries for faster performance
C. To secure the API by hiding sensitive data
D. To automatically generate GraphQL schemas
Solution
Step 1: Understand mocking resolvers
Mocking resolvers are used to create fake data responses for GraphQL fields without connecting to a real database.
Step 2: Identify the main purpose
This helps frontend developers test and build UI without waiting for backend data.
Final Answer:
To simulate API responses without needing real data -> Option A
Quick Check:
Mocking = Simulate data [OK]
Hint: Mocks simulate data, not optimize or secure APIs [OK]
Common Mistakes:
Confusing mocking with database optimization
Thinking mocks secure the API
Assuming mocks generate schemas automatically
2. Which of the following is the correct way to define a mock resolver for a GraphQL field user that returns a fixed name?
Mock resolvers are objects where the type (e.g., Query) maps to functions returning objects matching the schema.
Step 2: Check the correct syntax
The user field should be a function returning an object with a name property, so const mocks = { Query: { user: () => ({ name: 'Alice' }) } }; is correct.
B. Missing return statement inside the user resolver function
C. Resolver should return a string, not an object
D. Incorrect key name; should be 'User' instead of 'user'
Solution
Step 1: Check function body syntax
The user resolver uses curly braces but does not return an object explicitly.
Step 2: Understand JavaScript function return rules
Without a return statement, the function returns undefined, causing the mock to fail.
Final Answer:
Missing return statement inside the user resolver function -> Option B
Quick Check:
Functions with braces need explicit return [OK]
Hint: Use return or parentheses for object in arrow functions [OK]
Common Mistakes:
Assuming implicit return with braces
Confusing type names case sensitivity
Expecting string return instead of object
5. You want to mock a GraphQL resolver for a product field that returns a list of products with id and price. Which mock resolver correctly returns two products with ids 1 and 2 and prices 10.5 and 20.0 respectively?