npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

apollo-mock-http

v1.4.2

Published

Mocking HTTP responses in Apollo Client

Downloads

142

Readme

apollo-mock-http

An easy and maintainable way of injecting mock data into Apollo GraphQL Client for fast feature development decoupled from API/Backend.

CircleCI Coverage Status NPM Version semantic-release Contributions welcome

Checkout the library apollo-mock-http on NPM, and it's Downloads trend!

Usage

yarn add apollo-mock-http -D

If you want a quick hands-on with the project, you can checkout this example usage in CodeSandbox

import { injectMock } from 'apollo-mock-http';

// Pass in the links that are making up the Apollo client, and other mock data configurations
injectMock({
  links,

  // Switch between mock and real with a flick :)
  enableMock: true,

  // Operations which need to be mocked (queries / mutations)
  targetOperations: ['getCompanyName', 'getCompanyEmployees'],

  // Mock data configurations for various operations
  mockData: {
    'getCompanyName': {data: {name: 'Test', __typename: 'String'}, errors: null},
    'getCompanyEmployees': {data: {employees: ['TestUser'], __typename: 'Employee'}, errors: null},
    'getCompanyId': {data: {id: '123', __typename: 'CompanyID'}, errors: null},
    ...
  },

  /* [Optional]
  By default, the mock link gets injected directly into the chain of links which is provided.

  In case, your links follow some structure like
    {linkName: 'L1', linkObj: new ApolloLink()}
  you can use this function to generate object containing mock link
  */
  createCustomLinkObj: (generatedMockLink) => {
    return {
      linkName: 'mockHttp',
      linkObj: generatedMockLink
    };
  }
})

For simulating error scenarios, use errors attribute in the mock response as below

mockData: {
  'getCompanyName': {

    data: null,

    // Attribute (error or errors) usually depends on how your Error Links consume response. But this is a standard response Apollo Client can handle.
    errors: [{
      message: 'sample error'
    }]
  },
},

[!TIP] You can also utilize data from localstorage as mockdata. The library looks for the localstorage key 'AMH_mockdata' and uses that data in case mockdata isn't provided when instantiated. Ensure the string is a parseable JSON. NOTE: values in localstorage are strings!

Architecture

In order to be able mock responses, we will inject a mock link into the link chain that the Apollo Client uses., and based on the configuration the relevant mocked response is returned.

Watch the video

Contributing

Please check Contributing guide for any collaboration. Thanks.

Inspiration

Apollo GraphQL client is the default client for many enterprises for GraphQL data fetching on the browser side. During feature developments, many teams face the issue of APIs not ready. We tend to mock the data in the components, and while merging to master, we stress to remove the mocks to keep things clean. We run a huge risk here, and a lot of secondary checks get into picture to ensure master is always Production ready. Things could become easily unmanageable and could eventually blow up which could cause feature rollback, and customer getting blocked.

We have come up with a strategy of mocking the data near network layer. That means components interact just like how they interact in Production. There is a single place where mock data injection happens, and it's very easy to enable / disable the mocks, making it very easy for developers working together, and could reliably develop, test different scenarios of UI like success, failure, different responses etc. This brings a lot of confidence in rolling out. The biggest benefit is all teams can get themselves decoupled from APIs under construction, and move faster - Huge productivity. Also, this puts less load on Backend Data Servers, and huge bandwidth for others.