Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@graphql-tools/mock
Advanced tools
A set of utils for faster development of GraphQL tools
@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.
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)
);
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 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 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.
FAQs
A set of utils for faster development of GraphQL tools
The npm package @graphql-tools/mock receives a total of 784,177 weekly downloads. As such, @graphql-tools/mock popularity was classified as popular.
We found that @graphql-tools/mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.