New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@wayfair/gqmock

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wayfair/gqmock

GQMock - GraphQL Mocking Service

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
144
increased by251.22%
Maintainers
1
Weekly downloads
 
Created
Source

@wayfair/gqmock: GQMock - GraphQL Mocking Service

Release Lint codecov Contributor Covenant Maintainer

Table of Contents

About The Project

The problem

There isn't an easy way to customize mock data coming from Apollo Server without writing logic directly into the mock service implementation. This requires understanding the mock server templating implementation and hand rolling maintenance of that implementation directly.

As these custom mocking implementations grow, logic inside of mock servers becomes complex and counter-productive. Writing automated tests for both Frontends and Backends becomes more difficult and coupled in undesired ways.

The solution

@wayfair/gqlmock offers an easy way to seed the data returned by GraphQL operations. It masks the complexities of managing a mock server implementation and instead exposes a declarative API for expressing the deterministic data you need in your tests. There is no additional overhead for adding more tests to your test suite, and because each test has a unique context, running tests in parallel is 💯 supported!

Getting Started

To get a local copy up and running follow these simple steps.

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev @wayfair/gqmock

or

for installation via yarn

yarn add --dev @wayfair/gqmock

Library API

GraphqlMockingService

Parameter NameRequiredDescriptionTypeDefault
portNoPort used to run the mock servernumber5000
subgraphNoEnable subgraph schema supportbooleanfalse
async GraphqlMockingService.start

Starts the mocking server.

async GraphqlMockingService.stop

Stops the mocking server.

async GraphqlMockingService.registerSchema

Registers a schema with the mock server.

Parameter NameRequiredDescriptionTypeDefault
schemaYesA valid GraphQL schemastring
GraphqlMockingService.createContext

Creates a new GraphqlMockingContext instance with a unique sequenceId

Parameter NameRequiredDescriptionTypeDefault
sequenceIdNoA string to be used as a sequenceIdstringuuid

GraphqlMockingContext

GraphqlMockingContext.sequenceId

A unique string used to match GraphQL requests with registered seeds. sequenceId can be attached to requests using a custom Apollo Link:

import {ApolloLink} from '@apollo/client';

const mockServiceLink = new ApolloLink((operation, forward) => {
  operation.setContext(({headers = {}}) => ({
    headers: {
      ...headers,
      ...(sequenceId ? {'mocking-sequence-id': sequenceId} : {}),
    },
  }));

  return forward(operation);
});
async GraphqlMockingContext.operation

Registers a seed for a GraphQL operation.

Parameter NameRequiredDescriptionTypeDefault
operationNameYesName of the GraphQL operationstring
operationSeedResponseYesSee specific propertiesobject
operationSeedResponse.dataNoData to be merged with the default apollo server mockobject{}
operationSeedResponse.errorsNoErrors to returnobject[]
operationMatchArgumentsNoParams used for matching a seed with GraphQL operations. By default matching is exact.object{}
optionsNoSee specific propertiesobject{}
options.usesLeftNoUses left before discarding the seednumberseed doesn't get discarded
options.partialArgsNoAllow partial matching of query arguments with the seed argumentsbooleanfalse
async GraphqlMockingContext.networkError

Registers a seed for a network error.

Parameter NameRequiredDescriptionTypeDefault
operationNameYesName of the GraphQL operationstring
operationSeedResponseYesSeed to be merged with the default apollo server mockobject
operationSeedResponse.dataNoData to be merged with the default apollo server mockobject{}
operationSeedResponse.errorsNoErrors to returnobject[]
operationMatchArgumentsNoParams used for matching a seed with GraphQL operations. By default matching is exact.object{}
optionsNoSee specific propertiesobject{}
options.usesLeftNoUses left before discarding the seednumberseed doesn't get discarded
options.partialArgsNoAllow partial matching of query arguments with the seed argumentsbooleanfalse

Usage

Unit testing

describe('App', function () {
  let mockingService;
  beforeAll(async () => {
    mockingService = new GraphqlMockingService({port: 5000});
    await mockingService.start();

    const schema = fs.readFileSync(
      path.resolve(__dirname, './schema.graphql'),
      'utf-8'
    );
    await mockingService.registerSchema(schema);
  });

  afterAll(async () => {
    await mockingService.stop();
  });

  it('should work', async () => {
    const seed = {
      data: {
        booksByGenreCursorConnection: {
          edges: [
            {},
            {},
            {
              node: {
                id: 'Qm9vazo1',
                title: 'Harry Potter and the Chamber of Secrets',
                author: {
                  id: 'QXV0aG9yOjE=',
                  fullName: 'J. K. Rowling',
                  __typename: 'Author',
                },
                __typename: 'Book',
              },
              __typename: 'BooksEdge',
            },
          ],
        },
      },
    };

    const mockingContext = mockingService.createContext();
    await mockingContext.operation('GetBooks', seed, {genre: 'ALL'});

    render(<App sequenceId={mockingContext.sequenceId} />);
    const books = await screen.findAllByText(
      'Harry Potter and the Chamber of Secrets'
    );
    expect(books.length).toEqual(3);
  });
});

Chaining multiple seeds

const context = service.createContext();
await context.operation(/* ... */).operation(/* ... */).networkError(/* ... */);

Define operation seed response data

data is one of the allowed properties for operationSeedResponse registration parameter. It is supposed to mimic the query response defined by the registered schema. As such, data will be a composition of objects and arrays all the way to primitive leaf fields. You can define objects in the following way:

const operationSeedResponse = {
  data: {
    productBySku: {
      name: 'Flagship Table with Sku',
    },
  },
};
Defining lists in response data
List long-hand notation
const operationSeedResponse = {
  data: {
    productBySku: {
      name: 'Flagship Table with Sku',
      variants: [
        {},
        {
          name: 'standing',
          tags: [{}, {value: 'adjustable'}],
        },
        {},
        {
          name: 'office',
          tags: [{}, {value: 'adjustable'}],
        },
      ],
    },
  },
};

In this example, variants is a list of 4 elements, and tags is a list of 2 elements. In this notation, the last element is treated as the blueprint for all elements for the array. However, this blueprint can be overridden by defining an element in any index other than the last index.

List short-hand notation

The same lists can be defined using $length to define how many elements a list should have. $<index> is used to override selected items in the list. The prefix for both operations can be defined when the mocking service is initialized.

const operationSeedResponse = {
  data: {
    productBySku: {
      name: 'Flagship Table with Sku',
      variants: {
        name: 'office',
        tags: {value: 'adjustable', $length: 2},
        $length: 4,
        $2: {name: 'standing', tags: {value: 'adjustable', $length: 2}},
      },
    },
  },
};

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. For detailed contributing guidelines, please see CONTRIBUTING.md

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Project Link: https://github.com/wayfair-incubator/gqmock

Acknowledgements

This template was adapted from https://github.com/othneildrew/Best-README-Template.

Keywords

FAQs

Package last updated on 12 Oct 2022

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