What is @graphql-tools/mock?
@graphql-tools/mock is a package that allows you to create mock data for your GraphQL schema. This is particularly useful for testing and development purposes, as it enables you to simulate various scenarios and responses without needing a fully implemented backend.
What are @graphql-tools/mock's main functionalities?
Mocking a GraphQL Schema
This feature allows you to add mocks to an existing GraphQL schema. The code sample demonstrates how to create a simple schema, add mocks to it, and execute a query against the mocked schema.
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { addMocksToSchema } = require('@graphql-tools/mock');
const { graphql } = require('graphql');
const typeDefs = `
type Query {
hello: String
}
`;
const schema = makeExecutableSchema({ typeDefs });
const schemaWithMocks = addMocksToSchema({ schema });
const query = `
query {
hello
}
`;
graphql(schemaWithMocks, query).then((result) =>
console.log(result)
);
Customizing Mock Resolvers
This feature allows you to customize the mock resolvers for specific fields in your schema. The code sample shows how to override the default mock for the 'hello' field in the Query type.
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { addMocksToSchema } = require('@graphql-tools/mock');
const { graphql } = require('graphql');
const typeDefs = `
type Query {
hello: String
}
`;
const schema = makeExecutableSchema({ typeDefs });
const mocks = {
Query: () => ({
hello: () => 'Hello, custom world!'
})
};
const schemaWithMocks = addMocksToSchema({ schema, mocks });
const query = `
query {
hello
}
`;
graphql(schemaWithMocks, query).then((result) =>
console.log(result)
);
Using Mock List
This feature allows you to use MockList to generate a list of mock items with a specified range. The code sample demonstrates how to mock a list of users with a random length between 2 and 6.
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { addMocksToSchema, MockList } = require('@graphql-tools/mock');
const { graphql } = require('graphql');
const typeDefs = `
type Query {
users: [User]
}
type User {
id: ID
name: String
}
`;
const schema = makeExecutableSchema({ typeDefs });
const mocks = {
Query: () => ({
users: () => new MockList([2, 6])
}),
User: () => ({
id: () => '1',
name: () => 'John Doe'
})
};
const schemaWithMocks = addMocksToSchema({ schema, mocks });
const query = `
query {
users {
id
name
}
}
`;
graphql(schemaWithMocks, query).then((result) =>
console.log(result)
);
Other packages similar to @graphql-tools/mock
graphql-tools
graphql-tools is a comprehensive toolkit for building, mocking, and testing GraphQL schemas. It provides utilities for schema stitching, schema transforms, and more. Compared to @graphql-tools/mock, it offers a broader range of functionalities beyond just mocking.
graphql-faker
graphql-faker is a tool that allows you to generate fake data for your GraphQL schema. It provides a web interface to visualize and interact with the mocked data. While @graphql-tools/mock is more focused on programmatic mocking, graphql-faker offers a more interactive approach.
apollo-server
apollo-server is a fully-featured GraphQL server with built-in support for mocking. It allows you to easily set up a GraphQL server with mock data for development and testing. Compared to @graphql-tools/mock, apollo-server provides a more integrated solution for setting up a GraphQL server with mocks.