Socket
Socket
Sign inDemoInstall

@graphql-tools/mock

Package Overview
Dependencies
4
Maintainers
3
Versions
1133
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-tools/mock

A set of utils for faster development of GraphQL tools


Version published
Weekly downloads
1M
increased by12.17%
Maintainers
3
Created
Weekly downloads
 

Package description

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

Readme

Source

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

Last updated on 24 Nov 2021

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc