@giraphql/plugin-mocks
TypeScript icon, indicating that this package has built-in type declarations

2.10.0 • Public • Published

Mocks Plugin for GiraphQL

A simple plugin for adding resolver mocks to a graphQL schema.

Usage

Install

yarn add @giraphql/plugin-mocks

Setup

import MocksPlugin from '@giraphql/plugin-mocks';
const builder = new SchemaBuilder({
  plugins: [MocksPlugin],
});

Adding mocks

You can mock any field by adding a mock in the options passed to builder.builSchema under mocks.{typeName}.{fieldName}.

builder.queryType({
  fields: (t) => ({
    someField: t.string({
      resolve: () => {
        throw new Error('Not implemented');
      },
    }),
  }),
});

builder.toSchema({
  mocks: {
    Query: {
      someField: (parent, args, context, info) => 'Mock result!',
    },
  },
});

Mocks will replace the resolve functions any time a mocked field is executed. A schema can be build multiple times with different mocks.

Adding mocks for subscribe functions

To add a mock for a subscriber you can nest the mocks for subscribe and resolve in an object:

builder.subscriptionType({
  fields: (t) => ({
    someField: t.string({
      resolve: () => {
        throw new Error('Not implemented');
      },
      subscribe: () => {
        throw new Error('Not implemented');
      },
    }),
  }),
});

builder.toSchema({
  mocks: {
    Subscription: {
      someField: {
        resolve: (parent, args, context, info) => 'Mock result!',
        subscribe: (parent, args, context, info) => {
          /* return a mock async iterator */
        },
      },
    },
  },
});

Package Sidebar

Install

npm i @giraphql/plugin-mocks

Weekly Downloads

1

Version

2.10.0

License

ISC

Unpacked Size

87.8 kB

Total Files

37

Last publish

Collaborators

  • hayes