Mocking resolvers helps you test and develop your GraphQL API without needing a real database or backend.
Mocking resolvers in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
const mocks = {
Query: {
fieldName: () => mockedValue,
},
TypeName: {
fieldName: () => mockedValue,
},
};Resolvers are functions that return data for each field in your GraphQL schema.
Mocking resolvers means writing simple functions that return fake data instead of real data.
hello field in the Query type to always return 'Hello, world!'.const mocks = { Query: { hello: () => 'Hello, world!' } };
User type fields name and age with fixed values.const mocks = { User: { name: () => 'Alice', age: () => 30 } };
randomNumber field to return a random number between 0 and 99 each time.const mocks = { Query: { randomNumber: () => Math.floor(Math.random() * 100) } };
This example creates a simple GraphQL server with a greeting field mocked to always return 'Hi there!'.
const { ApolloServer, gql } = require('apollo-server'); const typeDefs = gql` type Query { greeting: String } `; const mocks = { Query: { greeting: () => 'Hi there!' } }; const server = new ApolloServer({ typeDefs, mocks }); server.listen().then(({ url }) => { console.log(`Server ready at ${url}`); });
Mocking is great for early development but replace mocks with real resolvers when your backend is ready.
You can use libraries like graphql-tools to help create mocks easily.
Mocks can return static values or dynamic values like random numbers or dates.
Mocking resolvers lets you simulate API responses without real data.
Use mocks to speed up frontend development and testing.
Mocks are simple functions returning fake data for GraphQL fields.
Practice
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 AQuick Check:
Mocking = Simulate data [OK]
- Confusing mocking with database optimization
- Thinking mocks secure the API
- Assuming mocks generate schemas automatically
user that returns a fixed name?Solution
Step 1: Understand mock resolver structure
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.Final Answer:
const mocks = { Query: { user: () => ({ name: 'Alice' }) } }; -> Option DQuick Check:
Mock resolver returns object with fields [OK]
- Returning a string instead of an object
- Swapping Query and user keys
- Not using a function for the resolver
{ book { title author } }?
const mocks = {
Query: {
book: () => ({ title: '1984', author: 'George Orwell' })
}
};Solution
Step 1: Analyze the mock resolver return value
The book resolver returns an object with title and author fields as strings.Step 2: Match query fields with returned object
The query requests title and author, both present in the returned object, so the output includes both.Final Answer:
{ "data": { "book": { "title": "1984", "author": "George Orwell" } } } -> Option AQuick Check:
Returned object matches query fields [OK]
- Expecting only one field returned
- Thinking resolver must return string
- Ignoring requested fields in query
const mocks = {
Query: {
user: () => {
name: 'Bob'
}
}
};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 BQuick Check:
Functions with braces need explicit return [OK]
- Assuming implicit return with braces
- Confusing type names case sensitivity
- Expecting string return instead of object
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?Solution
Step 1: Understand the expected return type
The product field should return a list (array) of objects, each with id and price fields.Step 2: Check each option's return value
const mocks = { Query: { product: () => [{ id: 1, price: 10.5 }, { id: 2, price: 20.0 }] } }; returns an array of two objects with correct fields and values. Others either return wrong types or syntax errors.Final Answer:
const mocks = { Query: { product: () => [{ id: 1, price: 10.5 }, { id: 2, price: 20.0 }] } }; -> Option CQuick Check:
Return array of objects for list fields [OK]
- Returning object with arrays instead of array of objects
- Assigning array directly without function
- Missing return or using wrong syntax in function
