Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@graphql-tools/mock
Advanced 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.
Check API Reference for more information about this package; https://www.graphql-tools.com/docs/api/modules/mock_src
You can also learn more about Mocking in this chapter; https://www.graphql-tools.com/docs/mocking
FAQs
A set of utils for faster development of GraphQL tools
The npm package @graphql-tools/mock receives a total of 710,085 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 4 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.