Integration tests check if different parts of your GraphQL server work well together. Using a test server helps you test safely without affecting real data.
0
0
Integration tests with test server in GraphQL
Introduction
When you want to check if your GraphQL queries and mutations work correctly end-to-end.
When you add new features and want to make sure they don't break existing ones.
When you want to test how your server handles real requests and responses.
When you want to catch bugs before your users see them.
When you want to automate testing as part of your development process.
Syntax
GraphQL
import { createTestClient } from 'apollo-server-testing'; import { ApolloServer } from 'apollo-server'; const server = new ApolloServer({ typeDefs, resolvers }); const { query, mutate } = createTestClient(server); // Use query({ query: YOUR_QUERY }) or mutate({ mutation: YOUR_MUTATION }) to test
You create a test server instance with your schema and resolvers.
Use query to test queries and mutate to test mutations.
Examples
This example tests a query to get a user by ID.
GraphQL
const GET_USER = gql` query GetUser($id: ID!) { user(id: $id) { id name } } `; const res = await query({ query: GET_USER, variables: { id: "1" } });
This example tests a mutation to add a new user.
GraphQL
const ADD_USER = gql` mutation AddUser($name: String!) { addUser(name: $name) { id name } } `; const res = await mutate({ mutation: ADD_USER, variables: { name: "Alice" } });
Sample Program
This program creates a simple GraphQL test server with a user list. It tests adding a user and then fetching that user by ID.
GraphQL
import { ApolloServer, gql } from 'apollo-server'; import { createTestClient } from 'apollo-server-testing'; // Define schema const typeDefs = gql` type User { id: ID! name: String! } type Query { user(id: ID!): User } type Mutation { addUser(name: String!): User } `; // In-memory data const users = []; let idCount = 1; // Resolvers const resolvers = { Query: { user: (_, { id }) => users.find(u => u.id === id), }, Mutation: { addUser: (_, { name }) => { const user = { id: String(idCount++), name }; users.push(user); return user; }, }, }; // Create test server const server = new ApolloServer({ typeDefs, resolvers }); const { query, mutate } = createTestClient(server); // Test adding a user (async () => { const ADD_USER = gql` mutation AddUser($name: String!) { addUser(name: $name) { id name } } `; const resAdd = await mutate({ mutation: ADD_USER, variables: { name: "Bob" } }); // Test querying the user const GET_USER = gql` query GetUser($id: ID!) { user(id: $id) { id name } } `; const resGet = await query({ query: GET_USER, variables: { id: resAdd.data.addUser.id } }); console.log(JSON.stringify(resAdd.data)); console.log(JSON.stringify(resGet.data)); })();
OutputSuccess
Important Notes
Integration tests run your real schema and resolvers, so they catch more issues than unit tests.
Use an in-memory database or mock data to keep tests fast and isolated.
Always clean or reset data between tests to avoid interference.
Summary
Integration tests check if your GraphQL server parts work together correctly.
Use a test server to safely run queries and mutations without affecting real data.
Write tests for both queries and mutations to cover your API fully.