<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vamshi Suram</title>
    <description>The latest articles on DEV Community by Vamshi Suram (@vamshisuram).</description>
    <link>https://dev.to/vamshisuram</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F145795%2F5bb31de2-de18-47af-8318-0a00b69b872a.png</url>
      <title>DEV Community: Vamshi Suram</title>
      <link>https://dev.to/vamshisuram</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vamshisuram"/>
    <language>en</language>
    <item>
      <title>Struggling to Mock graphQL Data? Open Source apollo-mock-http to the Rescue.</title>
      <dc:creator>Vamshi Suram</dc:creator>
      <pubDate>Wed, 08 Feb 2023 18:30:00 +0000</pubDate>
      <link>https://dev.to/vamshisuram/struggling-to-mock-graphql-data-open-source-apollo-mock-http-to-the-rescue-347o</link>
      <guid>https://dev.to/vamshisuram/struggling-to-mock-graphql-data-open-source-apollo-mock-http-to-the-rescue-347o</guid>
      <description>&lt;p&gt;This blog post is co-authored by &lt;a href="https://medium.com/@singh_arun" rel="noopener noreferrer"&gt;Arun Singh&lt;/a&gt; (Development Manager), &lt;a href="http://www.linkedin.com/in/anavatya-cv-40a626107" rel="noopener noreferrer"&gt;Anavatya C.V.&lt;/a&gt; (Senior Software Engineer) at Intuit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxgc84760w62m7ofcux41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxgc84760w62m7ofcux41.png" alt="logo" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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?&lt;/p&gt;

&lt;p&gt;Struggle no more.&lt;/p&gt;

&lt;p&gt;Our newly-introduced open source project &lt;a href="https://github.com/intuit/apollo-mock-http" rel="noopener noreferrer"&gt;apollo-mock-http&lt;/a&gt; is an easy and maintainable way to inject mock data into &lt;a href="https://www.apollographql.com/docs/react" rel="noopener noreferrer"&gt;Apollo GraphQL Client&lt;/a&gt; for fast front-end feature development decoupled from the back-end API.&lt;/p&gt;

&lt;p&gt;TL;DR — You can check out this &lt;a href="https://codesandbox.io/s/apolloclient-mocking-7nwheq" rel="noopener noreferrer"&gt;codesandbox link&lt;/a&gt; to quickly get started.&lt;/p&gt;

&lt;p&gt;As a &lt;a href="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" rel="noopener noreferrer"&gt;global financial technology platform&lt;/a&gt; company serving more than 100M consumer and small business customers around the world with &lt;a href="https://cts.businesswire.com/ct/CT?id=smartlink&amp;amp;url=https%3A%2F%2Fturbotax.intuit.com%2F&amp;amp;esheet=52904538&amp;amp;newsitemid=20221110005451&amp;amp;newsitemid=20220915005356&amp;amp;lan=en-US&amp;amp;anchor=TurboTax&amp;amp;index=11&amp;amp;md5=274e61c37b231273fffffecccdb2fb43" rel="noopener noreferrer"&gt;TurboTax&lt;/a&gt;, &lt;a href="https://cts.businesswire.com/ct/CT?id=smartlink&amp;amp;url=https%3A%2F%2Fwww.creditkarma.com%2F&amp;amp;esheet=52904538&amp;amp;newsitemid=20221110005451&amp;amp;newsitemid=20220915005356&amp;amp;lan=en-US&amp;amp;anchor=Credit+Karma&amp;amp;index=12&amp;amp;md5=97973fa1bbbee9694701221dd1acb076" rel="noopener noreferrer"&gt;Credit Karma&lt;/a&gt;, &lt;a href="https://cts.businesswire.com/ct/CT?id=smartlink&amp;amp;url=https%3A%2F%2Fquickbooks.intuit.com%2F&amp;amp;esheet=52904538&amp;amp;newsitemid=20221110005451&amp;amp;newsitemid=20220915005356&amp;amp;lan=en-US&amp;amp;anchor=QuickBooks&amp;amp;index=13&amp;amp;md5=f35c26e128cda2554b80e932126a37fd" rel="noopener noreferrer"&gt;QuickBooks&lt;/a&gt;, and &lt;a href="https://cts.businesswire.com/ct/CT?id=smartlink&amp;amp;url=https%3A%2F%2Fmailchimp.com%2F&amp;amp;esheet=52904538&amp;amp;newsitemid=20221110005451&amp;amp;newsitemid=20220915005356&amp;amp;lan=en-US&amp;amp;anchor=Mailchimp&amp;amp;index=14&amp;amp;md5=daafcc85820d43cb727462ca33197a61" rel="noopener noreferrer"&gt;Mailchimp&lt;/a&gt;, 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 &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt;, &lt;a href="https://graphql.org/" rel="noopener noreferrer"&gt;GraphQL&lt;/a&gt;, &lt;a href="https://martinfowler.com/articles/micro-frontends.html" rel="noopener noreferrer"&gt;micro-frontends&lt;/a&gt;, to name a few — and Apollo Client when working with GraphQL.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://www.apollographql.com/docs/react/api/link/apollo-link-http/" rel="noopener noreferrer"&gt;HTTP link&lt;/a&gt;, 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.&lt;/p&gt;

&lt;p&gt;This is where the fun began for our team.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;It worked!&lt;/p&gt;

&lt;p&gt;To see how, check out the &lt;a href="https://www.npmjs.com/package/apollo-mock-http" rel="noopener noreferrer"&gt;apollo-mock-http&lt;/a&gt; library animation below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxmgl3okxmkmtmsychovz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxmgl3okxmkmtmsychovz.gif" alt="architecture" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mocking made easy with apollo-mock-http
&lt;/h2&gt;

&lt;p&gt;Benefits at-a-glance:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;GraphQL data mocking&lt;/li&gt;
&lt;li&gt;Mixed mocking — selective queries/mutations&lt;/li&gt;
&lt;li&gt;Near network layer mocking — components are clean&lt;/li&gt;
&lt;li&gt;Scalable, and one-point maintenance for mocking, with enable/disable flag — ready for production/integration testing, etc.&lt;/li&gt;
&lt;li&gt;100% code coverage, automated builds, semantic releases with documentation examples and a &lt;a href="https://codesandbox.io/s/apolloclient-mocking-7nwheq" rel="noopener noreferrer"&gt;code sandbox playground&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To use this for your next project, install the library using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add apollo-mock-http &lt;span class="nt"&gt;-D&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;injectMock&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apollo-mock-http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;injectMock&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;links&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;enableMock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;targetOperations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getCompanyName&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;mockData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                                            
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getCompanyName&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;__typename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;String&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; 
        &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="c1"&gt;// If your link follows a specific structure, you can use this to customize&lt;/span&gt;
  &lt;span class="na"&gt;createCustomLinkObj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;generatedMockLink&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;linkName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mockHttp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;linkObj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;generatedMockLink&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To learn more, go to &lt;a href="https://github.com/intuit/apollo-mock-http/blob/main/README.md" rel="noopener noreferrer"&gt;readme&lt;/a&gt; for the apollo-mock-http repo in GitHub.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://github.com/intuit/apollo-mock-http/issues/new" rel="noopener noreferrer"&gt;creating issues&lt;/a&gt; in the GitHub repo and follow the required setup &lt;a href="https://github.com/intuit/apollo-mock-http/blob/main/CONTRIBUTING.md" rel="noopener noreferrer"&gt;contributing guide&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;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 &lt;a href="https://www.intuit.com/careers/teams/software-engineering/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to join our talent community!&lt;/p&gt;

</description>
      <category>vibecoding</category>
    </item>
  </channel>
</rss>
