Apollo Server Testing Utilities (Legacy)
raw JSON →This `apollo-server-testing` package provides utilities specifically designed for testing applications built with older versions of Apollo Server (v2 and v3). Its primary export, `createTestClient`, wraps the `ApolloServer.executeOperation` method to allow direct GraphQL operation execution against an `ApolloServer` instance without spinning up a full HTTP server. The package `apollo-server-testing` is currently at version `2.25.3` and was last published over four years ago. It is considered abandoned for new development, as the current major versions of Apollo Server (v4 and v5, under the `@apollo/server` package) integrate testing utilities directly via `server.executeOperation` or provide `@apollo/server/testing` where applicable. This package is solely relevant for maintaining legacy projects still on `apollo-server` v2 or v3, which are both officially end-of-life.
Common errors
error TypeError: Cannot destructure property 'query' of 'undefined' as it is undefined. ↓
ApolloServer instance (from apollo-server v2/v3) is correctly constructed before passing it to createTestClient. If using @apollo/server v4+, you should directly use server.executeOperation() instead of createTestClient(). error Error: Apollo Server requires 'graphql' to be installed. Please install 'graphql@^14.0.0 || ^15.0.0' or newer. ↓
graphql version: npm install graphql@^14.0.0 || ^15.0.0 or yarn add graphql@^14.0.0 || ^15.0.0. Refer to your apollo-server version's documentation for exact compatible graphql versions. Warnings
breaking The `apollo-server-testing` package is designed for Apollo Server v2 and v3. Both v2 (end-of-life Oct 2023) and v3 (end-of-life Oct 2024) are no longer officially supported by Apollo. New projects should use `@apollo/server` (v4+) and its integrated testing approaches. ↓
gotcha The `createTestClient` function from `apollo-server-testing` does not fully support the `context` function argument that Apollo Server's `executeOperation` method (and the server itself) provides. Specifically, it might not pass `req` or `res` objects to your context function as expected, leading to `undefined` values if your context relies on them. ↓
deprecated The `apollo-server-testing` package is explicitly deprecated and will not be published with Apollo Server 3 or later. Its functionality is a thin wrapper around `ApolloServer.executeOperation`, which is the recommended direct approach for testing Apollo Server logic without the HTTP layer. ↓
Install
npm install apollo-server-testing yarn add apollo-server-testing pnpm add apollo-server-testing Imports
- createTestClient wrong
import { createTestClient } from '@apollo/server/testing';correctimport { createTestClient } from 'apollo-server-testing'; - ApolloServer wrong
import { ApolloServer } from '@apollo/server';correctimport { ApolloServer, gql } from 'apollo-server';
Quickstart
import { ApolloServer, gql } from 'apollo-server';
import { createTestClient } from 'apollo-server-testing';
// 1. Define your GraphQL schema
const typeDefs = gql`
type Query {
hello(name: String): String!
getUser(id: ID!): User
}
type User {
id: ID!
name: String!
email: String!
}
type Mutation {
addUser(name: String!, email: String!): User!
}
`;
// 2. Define your resolvers
const users = [
{ id: '1', name: 'Alice', email: 'alice@example.com' },
{ id: '2', name: 'Bob', email: 'bob@example.com' }
];
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name || 'World'}!`,
getUser: (_, { id }) => users.find(user => user.id === id)
},
Mutation: {
addUser: (_, { name, email }) => {
const newUser = { id: String(users.length + 1), name, email };
users.push(newUser);
return newUser;
}
}
};
// 3. Create an Apollo Server instance
const server = new ApolloServer({
typeDefs,
resolvers,
// Optional: context function
context: () => ({ /* Add any context relevant for your resolvers */ })
});
// 4. Create a test client
const { query, mutate } = createTestClient(server);
// Example Test (using Jest syntax for illustration)
describe('Apollo Server Integration Tests', () => {
it('should fetch a greeting', async () => {
const GET_HELLO = gql`
query GetHello($name: String) {
hello(name: $name)
}
`;
const response = await query({ query: GET_HELLO, variables: { name: 'Test' } });
expect(response.data.hello).toBe('Hello Test!');
});
it('should add a new user', async () => {
const ADD_USER = gql`
mutation AddUser($name: String!, $email: String!) {
addUser(name: $name, email: $email) {
id
name
email
}
}
`;
const response = await mutate({ query: ADD_USER, variables: { name: 'Charlie', email: 'charlie@example.com' } });
expect(response.data.addUser).toEqual({
id: expect.any(String),
name: 'Charlie',
email: 'charlie@example.com'
});
expect(users).toHaveLength(3);
});
it('should fetch an existing user', async () => {
const GET_USER = gql`
query GetUser($id: ID!) {
getUser(id: $id) {
id
name
email
}
}
`;
const response = await query({ query: GET_USER, variables: { id: '1' } });
expect(response.data.getUser).toEqual(users[0]);
});
});