Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@graphql-tools/mock

Package Overview
Dependencies
Maintainers
3
Versions
1148
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-tools/mock

A set of utils for faster development of GraphQL tools

  • 9.0.3-alpha-20240527111853-49b97e4daa4a7d40c7e0fac217d615f1e19165fe
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created

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

FAQs

Package last updated on 27 May 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc