Struggling to Mock graphQL Data? Open Source apollo-mock-http to the Rescue.
Published: 2023-02-08 18:30:00

This blog post is co-authored by [Arun Singh](https://medium.com/@singh_arun) (Development Manager), [Anavatya C.V.](http://www.linkedin.com/in/anavatya-cv-40a626107) (Senior Software Engineer) at Intuit.


![logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgc84760w62m7ofcux41.png)


Are you struggling with mocking GraphQL data for your product development when an API is not available? Is mocking data directly in your components leading to unclean coding practices and indeterministic outputs that require cleanup?

Struggle no more.

Our newly-introduced open source project [apollo-mock-http](https://github.com/intuit/apollo-mock-http) is an easy and maintainable way to inject mock data into [Apollo GraphQL Client](https://www.apollographql.com/docs/react) for fast front-end feature development decoupled from the back-end API.

TL;DR — You can check out this [codesandbox link](https://codesandbox.io/s/apolloclient-mocking-7nwheq) to quickly get started.

As a [global financial technology platform](https://investors.intuit.com/news/news-details/2022/Intuits-Global-Financial-Technology-Platform-Architecture-Drives-Technology-Innovation-for-Customers-With-Speed-at-Scale/default.aspx) company serving more than 100M consumer and small business customers around the world with [TurboTax](https://cts.businesswire.com/ct/CT?id=smartlink&url=https%3A%2F%2Fturbotax.intuit.com%2F&esheet=52904538&newsitemid=20221110005451&amp;newsitemid=20220915005356&lan=en-US&anchor=TurboTax&index=11&md5=274e61c37b231273fffffecccdb2fb43), [Credit Karma](https://cts.businesswire.com/ct/CT?id=smartlink&url=https%3A%2F%2Fwww.creditkarma.com%2F&esheet=52904538&newsitemid=20221110005451&amp;newsitemid=20220915005356&lan=en-US&anchor=Credit+Karma&index=12&md5=97973fa1bbbee9694701221dd1acb076), [QuickBooks](https://cts.businesswire.com/ct/CT?id=smartlink&url=https%3A%2F%2Fquickbooks.intuit.com%2F&esheet=52904538&newsitemid=20221110005451&amp;newsitemid=20220915005356&lan=en-US&anchor=QuickBooks&index=13&md5=f35c26e128cda2554b80e932126a37fd), and [Mailchimp](https://cts.businesswire.com/ct/CT?id=smartlink&url=https%3A%2F%2Fmailchimp.com%2F&esheet=52904538&newsitemid=20221110005451&amp;newsitemid=20220915005356&lan=en-US&anchor=Mailchimp&index=14&md5=daafcc85820d43cb727462ca33197a61), accelerating development velocity is a top priority for Intuit. Like many large enterprise companies, our platform architecture is built on a modern SaaS development environment, and we take full advantage of technologies like [React](https://reactjs.org/), [GraphQL](https://graphql.org/), [micro-frontends](https://martinfowler.com/articles/micro-frontends.html), to name a few — and Apollo Client when working with GraphQL.

While the Apollo Client framework provides a clean API to fetch data from remote endpoints, it can be a “magical box” for developers when it comes to how the framework gets the data, and returns it to the application. Until now, there hasn’t been a practical methodology for injecting mock data into Apollo Client to work along with actual data. To solve this, our team has developed an apollo-mock-http library, and contributed it to the open source community, to enable developers to simulate various back-end scenarios (e.g., success, error, missing data) for selective queries or mutations.

Before going into the details of how apollo-mock-http works, let’s take a look at how Apollo Client is typically configured in applications. For data fetching, Apollo Client uses a set of links, which perform different operations on HTTP requests. Of these links, the last one is [HTTP link](https://www.apollographql.com/docs/react/api/link/apollo-link-http/), which actually calls the back-end. Once the response is received, it will be sent through the set of links, finally reaching the UI components.

This is where the fun began for our team.

We wondered what would happen if we placed a dummy HTTP link before, which could conditionally block some requests and allow others to reach the last HTTP link? By providing an alternate path, we hypothesized that we’d be able to simulate different scenarios and return mock data for requests we were most interested in, and let others reach the back-end to get real data entirely from the front-end.

It worked!

To see how, check out the [apollo-mock-http](https://www.npmjs.com/package/apollo-mock-http) library animation below.


![architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmgl3okxmkmtmsychovz.gif)

## Mocking made easy with apollo-mock-http
Benefits at-a-glance:

1. GraphQL data mocking
2. Mixed mocking — selective queries/mutations
3. Near network layer mocking — components are clean
4. Scalable, and one-point maintenance for mocking, with enable/disable flag — ready for production/integration testing, etc.
5. 100% code coverage, automated builds, semantic releases with documentation examples and a [code sandbox playground](https://codesandbox.io/s/apolloclient-mocking-7nwheq)

To use this for your next project, install the library using this command:

```bash
yarn add apollo-mock-http -D
```

Once added to your project, add a configuration (see example below) containing a set of links that make up your Apollo Client instance, mock data for the queries involved, and a few other helper attributes. Once provided, the library will inject a dummy link just before the last HTTP link. This way we can have control over the requests, and tweak responses for different scenarios.

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

injectMock({
  links,
  enableMock: true,
  targetOperations: ['getCompanyName'],
  mockData: {                                            
    'getCompanyName': {
        data: { name: 'Test', __typename: 'String' }, 
        error: null
    },
  },

  // If your link follows a specific structure, you can use this to customize
  createCustomLinkObj: (generatedMockLink) => {
    return {
      linkName: 'mockHttp',
      linkObj: generatedMockLink
    };
  }
})

```

To learn more, go to [readme](https://github.com/intuit/apollo-mock-http/blob/main/README.md) for the apollo-mock-http repo in GitHub.

As an industry leader in open source infrastructure tools built on cloud-native technologies such as Kubernetes and Argo, we’re proud to contribute one more open source project that can help a lot of developers out there. We look forward to collaborating with you, so please share any suggestions you may have by [creating issues](https://github.com/intuit/apollo-mock-http/issues/new) in the GitHub repo and follow the required setup [contributing guide](https://github.com/intuit/apollo-mock-http/blob/main/CONTRIBUTING.md).

Many thanks to my awesome friends and colleagues here at Intuit for bringing this project to light. Especially [Anavatya C.V](http://www.linkedin.com/in/anavatya-cv-40a626107) (Senior Software Engineer), [Tharun Mathew Paul](https://medium.com/@tharunpaul) (Senior Software Engineer), [Ramit Garg](https://www.linkedin.com/in/ramit-garg-295b3a22) (Senior Software Engineer), [Arun Singh](https://medium.com/@singh_arun) (Development Manager), [Punam Goswami](http://linkedin.com/in/punamgoswami) (Principal Software Engineer), [Guarav Misra](https://www.linkedin.com/in/gaurav-misra-87286314) (Group Development Manager).

If this blog has inspired your interest to learn more about what it’s like to work on a high performing engineering team at Intuit, visit [here](https://www.intuit.com/careers/teams/software-engineering/) to join our talent community!