Levels of GraphQL Testing
Testing a GraphQL API spans three levels: unit tests for individual resolver functions in isolation, integration tests that execute real queries against the full schema with a test server, and end-to-end tests that hit a running server over HTTP. Unit tests mock data sources and check resolver logic directly; integration tests catch schema wiring issues like missing resolvers or incorrect type relationships that unit tests alone would miss.
Cricket analogy: Like practicing individual batting shots in the nets (unit tests) versus playing a full intra-squad simulation match (integration tests) versus an actual international fixture (end-to-end), each testing level catches different classes of issues.
Unit Testing Resolvers
Because resolvers are typically plain functions with the signature (parent, args, context, info), you can unit test them directly by calling the function with mocked arguments and a mocked context (such as a fake database client or data loader), then asserting on the return value. This isolates business logic from the GraphQL execution engine entirely, making tests fast and independent of schema wiring.
Cricket analogy: Like testing a bowler's yorker delivery in a solo bowling machine session isolated from match conditions, unit testing a resolver isolates its logic from the rest of the schema.
// resolver.test.js
const { resolvers } = require('./resolvers');
test('Query.book resolves a book by id', async () => {
const mockDb = {
findBookById: jest.fn().mockResolvedValue({ id: '1', title: 'Dune' }),
};
const result = await resolvers.Query.book(
null,
{ id: '1' },
{ dataSources: { db: mockDb } }
);
expect(result).toEqual({ id: '1', title: 'Dune' });
expect(mockDb.findBookById).toHaveBeenCalledWith('1');
});Integration Testing With executeOperation and MockedProvider
Apollo Server provides executeOperation to run a real GraphQL operation against your assembled schema without starting an HTTP server, catching issues like missing resolvers, type mismatches, or broken field relationships. On the frontend, Apollo Client's MockedProvider lets you render a component with predefined mock responses for specific queries, verifying that the component renders loading, success, and error states correctly without a real network call.
Cricket analogy: Like running a full simulated match with the actual playing XI to test team combinations before the real tournament, executeOperation runs real queries against the assembled schema to catch wiring issues.
MockedProvider matches mocks strictly by query, variables, and order by default — a component that fires the same query with slightly different variables than your mock will fail with a 'No more mocked responses' error, which can make tests brittle if not written carefully.
- GraphQL testing spans unit tests (resolvers), integration tests (schema execution), and end-to-end tests (live server).
- Resolvers are plain functions and can be unit tested directly with mocked context and arguments.
- Apollo Server's executeOperation runs real queries against the assembled schema without an HTTP server.
- Apollo Client's MockedProvider renders components with predefined mock query responses for frontend tests.
- Integration tests catch schema wiring issues that isolated unit tests can miss.
- MockedProvider matches mocks by exact query and variables, so mismatches cause test failures.
- End-to-end tests against a running server validate the full stack including network and auth.
Practice what you learned
1. What is the main advantage of unit testing a GraphQL resolver directly?
2. What does Apollo Server's executeOperation allow you to do?
3. What is MockedProvider used for?
4. Why might a test using MockedProvider fail with 'No more mocked responses'?
5. What class of issues do integration tests catch that isolated resolver unit tests typically miss?
Was this page helpful?
You May Also Like
Apollo Client Basics
Learn how Apollo Client manages GraphQL queries, mutations, and the normalized cache in front-end applications.
GraphQL Code Generation
Understand how GraphQL Code Generator produces type-safe TypeScript types, hooks, and resolvers from your schema and operations.
GraphQL Interview Questions
A curated set of common GraphQL interview questions and model answers covering core concepts, performance, and real-world tradeoffs.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics