<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rocky Neurock on Medium]]></title>
        <description><![CDATA[Stories by Rocky Neurock on Medium]]></description>
        <link>https://medium.com/@rockyneurock?source=rss-2eae88297db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3hZmSS0BJ3Gryd5U.</url>
            <title>Stories by Rocky Neurock on Medium</title>
            <link>https://medium.com/@rockyneurock?source=rss-2eae88297db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 19:45:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rockyneurock/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[TIL: How to Setup Global Mocks with Vue Test Utils and Jest]]></title>
            <link>https://medium.com/kloeckner-i/til-how-to-setup-global-mocks-with-vue-test-utils-and-jest-ec0c0eb884b9?source=rss-2eae88297db------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec0c0eb884b9</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[til]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Rocky Neurock]]></dc:creator>
            <pubDate>Wed, 08 Jan 2020 21:47:52 GMT</pubDate>
            <atom:updated>2020-01-08T21:47:52.838Z</atom:updated>
            <content:encoded><![CDATA[<p>Though we have been using <a href="https://vuejs.org/">Vue.js</a> for a while on a project at kloeckner.i, we hadn’t encountered a need to <a href="https://vue-test-utils.vuejs.org/api/options.html#mocks">mock</a> any <a href="https://vuejs.org/v2/guide/plugins.html">plugins</a> for <em>all</em> the components in our tests… until today.</p><p>We’re working on translating a particular app’s interface into multiple languages and decided to use a popular plugin: <a href="https://kazupon.github.io/vue-i18n/">Vue I18n</a>. This plugin is used in virtually every component. Rather than use or mock the plugin in every test, I wanted a way to mock the plugin once. Fortunately, <a href="https://vue-test-utils.vuejs.org/">Vue Test Utils</a> and <a href="https://jestjs.io/">Jest</a> afforded me a simple way to do this.</p><p>Setting this up can be described in a 2-step process:</p><h4>Step 1</h4><p>Setup global mocks with <a href="https://vue-test-utils.vuejs.org/api/#config">Vue Test Utils’ config</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb658dc79e26cf7b32296c38b3726e9f/href">https://medium.com/media/bb658dc79e26cf7b32296c38b3726e9f/href</a></iframe><h4>Step 2</h4><p>Configure Jest to include the Vue Test Utils config before each test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3f52070a727e877e483e3ff869740a9/href">https://medium.com/media/a3f52070a727e877e483e3ff869740a9/href</a></iframe><p>Given the above example, any code under test that calls $myPlugin.foo() will receive &#39;bar&#39;.</p><p>Note: Local mocks override global mocks, as you would expect.</p><p>Thanks for reading. Of course, you can do much more than set up mocks with Vue Test Utils’ config options. Hopefully, this approach can help you if you find yourself in situation like ours.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec0c0eb884b9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kloeckner-i/til-how-to-setup-global-mocks-with-vue-test-utils-and-jest-ec0c0eb884b9">TIL: How to Setup Global Mocks with Vue Test Utils and Jest</a> was originally published in <a href="https://medium.com/kloeckner-i">kloeckner.i</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Use Case for an Async Generator]]></title>
            <link>https://medium.com/kloeckner-i/a-use-case-for-an-async-generator-b378359ca7a7?source=rss-2eae88297db------2</link>
            <guid isPermaLink="false">https://medium.com/p/b378359ca7a7</guid>
            <category><![CDATA[async]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Rocky Neurock]]></dc:creator>
            <pubDate>Fri, 25 Oct 2019 13:59:01 GMT</pubDate>
            <atom:updated>2019-10-25T13:59:01.547Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HZWk8OI_PvErQqOK55WZrQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@jeisblack?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Jason Blackeye</a> on <a href="https://unsplash.com/s/photos/generator?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I <em>try</em> to keep up with the latest JavaScript features but there are two common reasons why I have a hard time doing it:</p><ol><li>I’m busy. Really busy. <em>Sigh</em>.</li><li>It’s easy to read about new things, <em>sometimes</em> even understand them well, but unless you apply those learnings right away they tend to leave your brain in a hurry.</li></ol><p>I remember having this experience years ago when I first read about generators: <a href="https://babeljs.io/docs/en/learn#generators">https://babeljs.io/docs/en/learn#generators</a>. It took me a few reads to understand what was going on. Then I asked myself the same question I always have when I read about new features: “How can I use this to write less code?” 🤔</p><p>I went searching for generator use cases and was particularly interested in the async ones. As it turns out, a lot of the solutions to problems I read about were already implemented in my favorite frameworks and libraries. I was glad smarter people were already creating things to help me write less code but that left me in a state of: ¯\_(ツ)_/¯ “Maybe some day I’ll need this.”</p><h4>An Embarrassing Problem Presents a Learning Opportunity</h4><p>Fast forward a few years to 2018. A teammate and I gave a talk about <a href="https://emberjs.com/">Ember.js</a> and <a href="https://graphql.org/">GraphQL</a> at EmberFest. In the next year we were asked to give the same talk at a frontend web development meetup. Since the audience wasn’t Ember-specific, I decided to record a short video demoing the contents of the talk, including a lot of coding.</p><p>I thought it would be nice to share the video with the Ember community but it needed some work beforehand:</p><ol><li>It had no audio.</li><li>It could have used more examples.</li><li>The embarrassing part… my typing. My shameful, frustratingly poor typing.</li></ol><h4>About #3</h4><p>I’m not great at typing, at least, not for a developer. I often get frustrated with myself over my inability to type consistently and it gets even worse when I have an audience. But I decided to put my best foot forward and record myself typing as well as I cou… no, wait, stop, abort mission, we have to go back.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F18e01OgZF2jjJWOMOu%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FnailedIt-nailed-it-netflix-nicole-byers-18e01OgZF2jjJWOMOu&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F18e01OgZF2jjJWOMOu%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/22fab8091094b419c832bd1df5328627/href">https://medium.com/media/22fab8091094b419c832bd1df5328627/href</a></iframe><h4>Time for a New Plan 🤔</h4><p>My inability to type well enough for TV led me to a somewhat ridiculous idea: “What if I wrote a plugin for my code editor that could type for me?”</p><p>This could spare me the embarrassment of fixing so, so many typos which would probably also keep the video much shorter 😅 But would it be worth the time to create a plugin like this? Yes. My plugin can keep typing for me in any of my future videos.</p><h4>I Have a Plan. Now What?</h4><p>Time to implement. First, I needed to learn how to author a plugin for my code editor, <a href="https://atom.io/">Atom</a>. Lucky for me the plugins are written in JavaScript. My plan was to use this plugin in a series of steps:</p><ol><li>Open a dialog.</li><li>Accept a file path from the user.</li><li>Read the file.</li><li>Automatically type the file’s contents.</li></ol><p>Steps 1–3 were easy enough to implement. Step 4 required some thinking…</p><h4>There and Back and There Again</h4><p>My plan was actually to use async generators, at first. I would split the file contents into a list of lines. Then, for each line, I would iterate over each character and type it at some realistic interval.</p><p>I then thought (too briefly) that I should just try this with promises since I had never used generator functions. I ended up writing something that hilariously didn’t work. Like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0a6d87b6cd94baaf711943790d7230a/href">https://medium.com/media/d0a6d87b6cd94baaf711943790d7230a/href</a></iframe><p>The result was pretty entertaining. It did type all of the characters from my file but they were all over the place.</p><p>The problem with this code is that the callback I’ve given to the forEach loop, though asynchronous, is always called synchronously. Imagine the implementation of forEach as it’s achieved via this polyfill:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2652e551a4482c36bb4047562269985e/href">https://medium.com/media/2652e551a4482c36bb4047562269985e/href</a></iframe><p>I immediately understood that I hadn’t put enough time into thinking about it. Could I make it work with promises? Yes, sure, but I would have to think about it more and I still wanted to try a generator-based solution.</p><p>I decided to go back to my original idea and try out async generators. The resulting generator doesn’t end up looking that much different than the previous code I tried but it works and I like it a lot more.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe475d742254c26aee2173a688a8c486/href">https://medium.com/media/fe475d742254c26aee2173a688a8c486/href</a></iframe><p>Now I have a solution to my typing-on-video problem and some first-hand experience with async generators. Feel free to share any experiences you’ve had with generator functions, or sympathize with my lousy typing, in the comments.</p><p>Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b378359ca7a7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kloeckner-i/a-use-case-for-an-async-generator-b378359ca7a7">A Use Case for an Async Generator</a> was originally published in <a href="https://medium.com/kloeckner-i">kloeckner.i</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TIL: Higher Order Vue Components]]></title>
            <link>https://medium.com/kloeckner-i/til-higher-order-vue-components-8cea3237b003?source=rss-2eae88297db------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cea3237b003</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[til]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Rocky Neurock]]></dc:creator>
            <pubDate>Thu, 17 Oct 2019 15:23:28 GMT</pubDate>
            <atom:updated>2019-10-17T15:23:28.023Z</atom:updated>
            <content:encoded><![CDATA[<p>At kloeckner.i, we decided to try out <a href="https://vuejs.org/">Vue.js</a> on a new application. Over the last few months there have been several times I wondered if <a href="https://reactjs.org/docs/higher-order-components.html">Higher Order Components</a> would help me out when writing a feature. Today I really needed to use this pattern and learned one way to do it.</p><p>I wanted to DRY up some code that generated tables we had all over our app. I decided to render the table content from a list. Sometimes that content consisted of scalar values but other times we were passing the content into components.</p><p>I knew I could dynamically render the components but I needed a way to dynamically supply props, too. In my case the components had a common trait: They each had one prop; however, their props had different names.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/337a012a3af3b1410ae8834d53e6e7f3/href">https://medium.com/media/337a012a3af3b1410ae8834d53e6e7f3/href</a></iframe><p>I ended up writing the following Higher Order Component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc26f95c9bc58a04b71bb19b52ff1335/href">https://medium.com/media/bc26f95c9bc58a04b71bb19b52ff1335/href</a></iframe><p>The function has two arguments: The component to render and an object whose key/value pairs map prop names. It returns a component that passes its prop values to the desired component having different prop names.</p><p>This gives me what I want: The ability to render the existing components from a list in a dynamic way <em>without</em> altering their original interfaces.</p><p>A full example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d63a41370f76fcd1d882556a2e570eb0/href">https://medium.com/media/d63a41370f76fcd1d882556a2e570eb0/href</a></iframe><p>When I render the components in the template they each have a generic data prop that receives a value from current item in the loop.</p><pre>&lt;component :is=”field.component” :data=”item[field.dataKey]” /&gt;</pre><p>You can see everything in action here: <a href="https://codesandbox.io/s/vue-template-8o5lm">https://codesandbox.io/s/vue-template-8o5lm</a>.</p><p>By using my Higher Order Component I can map that data prop to the actual prop of the underlying component; in the example above, date and name.</p><p>Another nice thing about the mapProps function is that the propsMap allows me to map as many props as I want so this isn’t limited to my simple use case.</p><p>Now I know at least one way I can create Higher Order Components in Vue.js. Feel free to share the ways you’ve solved this or similar problems in the comments.</p><p>Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cea3237b003" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kloeckner-i/til-higher-order-vue-components-8cea3237b003">TIL: Higher Order Vue Components</a> was originally published in <a href="https://medium.com/kloeckner-i">kloeckner.i</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[No Graph Theory Required]]></title>
            <link>https://medium.com/kloeckner-i/ember-and-graphql-8aa15f7a2554?source=rss-2eae88297db------2</link>
            <guid isPermaLink="false">https://medium.com/p/8aa15f7a2554</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[ember]]></category>
            <dc:creator><![CDATA[Rocky Neurock]]></dc:creator>
            <pubDate>Fri, 12 Oct 2018 11:26:29 GMT</pubDate>
            <atom:updated>2019-01-14T09:58:59.240Z</atom:updated>
            <content:encoded><![CDATA[<h3>No Graph Theory Required: Ember and GraphQL in Practice</h3><p><strong>Update November 12th, 2018:</strong> Video of the talk has been uploaded to YouTube by the EmberFest team.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fvideoseries%3Flist%3DPLN4SpDLOSVkSB9034lDNdP1JoNBGssax9&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DkgfBexYbM68&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FkgfBexYbM68%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/caa33ded8cd5c2253133ea1fc795b0d2/href">https://medium.com/media/caa33ded8cd5c2253133ea1fc795b0d2/href</a></iframe><p><strong>Update October 30th, 2018:</strong> You can find the slides from our talk here: <a href="https://www.slideshare.net/RockyNeurock/no-graph-theory-required-ember-and-graphql-in-practice">https://www.slideshare.net/RockyNeurock/no-graph-theory-required-ember-and-graphql-in-practice</a></p><p>We’ve been working with GraphQL in our Ember apps for some time now. I wanted to share our journey so others thinking about GraphQL, or struggling with JSON API, could gain a solid understanding of how to get started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Owz9jy5LkHDqywu_xJ5XfQ.png" /></figure><p>The post was adapted from a talk given at <a href="https://emberfest.eu/">EmberFest 2018</a> by myself and my colleague <a href="https://twitter.com/chadian">Chad Carbert</a>. Chad created an <a href="https://github.com/chadian/ember-graphql-examples">example repo</a> that includes an Ember app demonstrating GraphQL integration with 4 different clients.</p><h3>Why GraphQL?</h3><p>At kloeckner.i, we’re fortunate to have the freedom to experiment and try out new technology. The first time I saw GraphQL, I immediately wanted to try it. To me, it seemed like a better way to wrangle data from an API.</p><p>The real catalyst for our venture into GraphQL was the need to build new apps and services. We encountered some issues working with JSON API and it seemed like a great time to try something different.</p><h3>Issues with JSON API</h3><p>If you use Ember Data, you’re likely familiar with the JSON API spec for building APIs. It offers the path of least resistance with Ember Data and that’s entirely by design.</p><p>In my experience, it really does save tons of time with API design and provides a huge boost to your team’s productivity. That being said, it’s not without its pain points.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zpah16Ts7B-nTMg7DJODpQ.png" /></figure><p>I’ll skip the details of CRUD in JSON API, assuming you’re familiar. What I do want to highlight is that CRUD doesn’t cover everything.</p><p>A real-world example of this from our own services is a shopping cart. To perform a checkout, the frontend updates a cart record (i.e., it sends a PATCH request) and we introduce some side effects on the backend to put the cart into a different state.</p><p>This all works fine but it’s completely un-obvious that this behavior even exists. Basically, communication with an API doesn’t always want to work with the CRUD pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3REn5krEX55Q2OaNFXmOEA.png" /><figcaption>Photo: Ufuk Ucta</figcaption></figure><p>Ember developers are really fortunate to have Ember Data. For backend developers; though, things aren’t so easy.</p><p>Libraries can be difficult to work with or simply don’t exist for some platforms. Some of our fellow developers have said they suspect this might be due to the design of the spec itself.</p><p>In any case, dealing with the complexities of the spec that aren’t handled by a library can have major drawbacks. At worst, we’ve resorted to offloading some data handling to our Ember apps, which is obviously not ideal.</p><p>At other times, this issue has caused a lot of frustration writing custom code to handle those complicated parts of the spec. This is especially true when the actual task at hand is just a simple concept but the implementation causes a big headache.</p><h4>JSON API Lags Behind GraphQL in Some Areas</h4><p>I’ll borrow some details from a tracking issue Yehuda opened on GitHub: <a href="https://github.com/json-api/json-api/issues/1272">Tracking Issue for deficiencies vs. GraphQL #1272</a>. Here, he perfectly summarizes some additional issues we’ve experienced with JSON API.</p><p>I’ve seen Ember developers gravitate the most toward his last two points:</p><ul><li>A built in way to describe types or having schema.</li><li>Ready made tools for API exploration.</li></ul><p>These things have a huge impact on productivity. But his first two points have a significant impact on the actual code that I write:</p><ul><li>Deep querying.</li><li>Really flexible mutations.</li></ul><p>I’ll show some examples of this later on.</p><p><strong>To quote one of our developers:</strong></p><blockquote>When using GraphQL, I don’t feel like I need to bend it to my needs like JSON API.<br> — Thiago Massa</blockquote><p>I agree with him and I think this is a really nice summation of our overall experience, having used both.</p><h3>What is GraphQL?</h3><p>In short GraphQL is a query language that can be used to retrieve data from your API. To retrieve this data from your API you can use a query that reads quite naturally.</p><p>For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cef1c41b599fc9485a885e4a501de395/href">https://medium.com/media/cef1c41b599fc9485a885e4a501de395/href</a></iframe><p>In this example let’s imagine we’re online shopping. For our online cart example we want to pull the total amount, and for each item we would like the description and price. One of the benefits to querying with GraphQL is that you only request the data you need.</p><p>One of the interesting things about GraphQL is that your data’s relationship is represented by fields and types. This makes it easy to trace your data’s relationships and its connections all the way to the root nodes that describe what base queries and mutations you have available. This discovery extends to the tools, making it easier for tooling to infer the graph of data through an introspection query.</p><p>GraphQL has many language features and it is quite well documented on the <a href="https://graphql.org/learn/">GraphQL website</a>, so it’s definitely worth taking a look.</p><h3>Integrating GraphQL with Ember</h3><p>I’ll cover some clients and then provide some examples of common patterns from our codebases.</p><h4>Clients</h4><p>After trying a few clients, we ultimately settled on Apollo. Apollo is a large scale GraphQL platform that spans the client side, server side and in-between.</p><p>While Apollo includes quite a lot of features and tooling, the <a href="https://github.com/bgentry/ember-apollo-client">ember-apollo-client</a> addon makes working with them really easy. We recommend this addon and the examples provided demonstrate its usage.</p><p><strong>In addition to Apollo, the first client we tried worked with Ember Data.</strong></p><p>We didn’t choose this one.</p><p>At first, it was appealing because we were trying this in an existing Ember app and it allowed for the simple inclusion of a new adapter and serializer to get up and running.</p><p>As our GraphQL services developed, we found it too inflexible for our needs. It also limited some of the flexibility GraphQL provides and we didn’t think it would work at full scale.</p><p><strong>These lightweight clients are worth mentioning.</strong></p><ul><li><a href="https://github.com/kadirahq/lokka">Lokka</a></li><li><a href="https://github.com/prisma/graphql-request">graphql-request</a></li></ul><p>They’re easy to include with <a href="https://github.com/ef4/ember-auto-import">ember-auto-import</a> and Chad demonstrates their usage in the example repo he created.</p><h4>Common Patterns in Our Codebases with GraphQL</h4><p>I’m going to use the example of a shopping cart that’s very similar to the ones we build in our real-world Ember apps. This shopping cart can contain 10s of items so we paginate them when displaying the cart to the user.</p><h4>Deep Querying</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9902c009637312a814c380cd34e3616a/href">https://medium.com/media/9902c009637312a814c380cd34e3616a/href</a></iframe><p>This works fine but you don’t have control over the items you get. It would be nice to sort and paginate the items. There’s no way to do that with this code so we ended up with a lot of code that looks something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e7d1b49f856d9678535d87d85f763a5/href">https://medium.com/media/2e7d1b49f856d9678535d87d85f763a5/href</a></iframe><p>This also works but it’s starting to get messy. There are also some other minor drawbacks:</p><ul><li>In this example, we have to make two synchronous network requests.</li><li>The data structure of the model becomes a little unexpected.</li></ul><p>We can get the same data with a GraphQL query.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb490d0f9c4cfd6764867f365e0c8fe7/href">https://medium.com/media/fb490d0f9c4cfd6764867f365e0c8fe7/href</a></iframe><p>Some of the things I really like about the GraphQL version, in no particular order:</p><ul><li>We have much more control over the data we get.</li><li>The model code is reduced and easier for me to parse.</li><li>We only have to make one network request.</li><li>The order data remains nested.</li></ul><h4>Mutating Data</h4><p>Let’s expand on our example and say the user is allowed to update the shipping address for their cart items.</p><p>They can update the address for an individual item or they can apply one address to all the cart items. It’s the latter case we want to dive into.</p><p>First, let’s talk about how that might be difficult with Ember Data and JSON API. As far as I’m aware, both Ember Data and JSON API require data changes on a per record basis. I.E., you can’t arbitrarily update many records at once.</p><p>How could we perform this task with Ember Data? We need to update the cart items individually.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a503a9d8d90aed0b829b6171ec795c6/href">https://medium.com/media/4a503a9d8d90aed0b829b6171ec795c6/href</a></iframe><p>This isn’t bad but there are two things that stand out to me:</p><ol><li>If our items are paginated, we would need to load all of them in order to update them.</li><li>We update the records in Ember Data’s store <em>before</em> we save them.</li></ol><p>If the data is bound to a view, we end up with an optimistic user experience. This isn’t necessarily a bad thing but if anything goes wrong in the process, the records become out-of-sync with the server.</p><p>I don’t like dealing with attribute rollbacks or trying to communicate the problem effectively to the user.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/515d11d0c3379ae3479b6740a47ffc36/href">https://medium.com/media/515d11d0c3379ae3479b6740a47ffc36/href</a></iframe><p>Here you can see we don’t need to mutate any objects in our store. This means we didn’t have to load all the records to make the changes.</p><p>Since we didn’t update the records in our store, we avoid any out-of-sync UI in case something went wrong.</p><p>There are a few things I really like about mutating data with GraphQL:</p><ul><li>Mutations are extremely flexible. You can update any number, and types, of records at once.</li><li>The data flow is more unidirectional.</li></ul><p>It’s possible to be very irresponsible with such flexibility but in my experience, that just doesn’t happen. Because you can create exactly what you need, you do, and everyone benefits.</p><h3>Testing</h3><p>In a typical REST application you need to send a separate request, for separate resources on separate endpoints. This idea paired extremely well with mocking during acceptance tests as we’ve seen with `ember-cli-mirage`. With mirage you are able to stub a single endpoint and pass the the request over to a handler which has access to an in-memory database and factories.</p><p>When looking at an application using a GraphQL API the setup is slightly different. A query is sent via `POST`, with all queries sharing the same endpoint. This makes it more difficult to stub the graph endpoint with the correct data as the context depends specifically on the query itself, not the URL.</p><p>With an existing codebase that is migrating to use GraphQL it is not just the API requests that needs to migrated, acceptance tests also need to be considered. At kloeckner we had an existing codebase with `ember-cli-mirage` and it made sense to try to bridge the gap with GraphQL. This lead to the creation of `<a href="https://github.com/kloeckner-i/ember-cli-mirage-graphql">ember-cli-mirage-graphql</a>` which provides a handler that maps a GraphQL schema to a Mirage schema.</p><p>Another testing tool that is relatively new that helps with API testing is <a href="https://netflix.github.io/pollyjs">Polly.js</a>. Developed at Netflix, Polly is similar in spirit to the <a href="https://github.com/vcr/vcr">VCR gem</a> used by the Ruby community. Polly requires that you have a “Real API” endpoint that your tests can use. This “Real API” is only used the first time while tests are running so that it can create a recording json of the request and the response. On subsequent test runs these recordings are replayed against matching requests, without the need of the “Real API” running. This works great with GraphQL since matching the request is done against the entire request, including the query.</p><p>Lastly if there is a specific use-case that needs to be handled in a custom way there is `<a href="https://github.com/apollographql/graphql-tools">graphql-tools</a>`, by Apollo. This package allows unpacking a query, when a schema is provided, to handle the data with mock resolvers. With mock resolver functions there is the chance to handle on a per-type basis any part of the schema. This is extremely flexible but leaves much of the wiring up to you.</p><p>The testing method that is chosen ultimately depends on your use-case and your current environment, but hopefully one of these techniques will help provide some ideas on how to test your app.</p><h3>Our Experience So Far</h3><p>Our experience working with GraphQL and Ember has been really nice.</p><p>With an easy-to-explore schema in our hands, we know everything we can and can’t do. This greatly increases our productivity, eliminating tedious API discovery processes.</p><p>GraphQL’s flexibility allows us to design APIs exactly as we need them. We don’t have to shoehorn anything into a constrained API model.</p><p>The data flow changes have improved our user experience. This part is subjective, and depends on your desired UX, but it’s been really nice for us.</p><p>Thank you for reading. Hopefully you have a better sense of how to get started with GraphQL in Ember and what problems it might solve for you.</p><p>This blog post and kloeckner.i are unaffiliated with the Ember, GraphQL and Apollo GraphQL projects. Ember is a trademark of Tilde Inc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8aa15f7a2554" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kloeckner-i/ember-and-graphql-8aa15f7a2554">No Graph Theory Required</a> was originally published in <a href="https://medium.com/kloeckner-i">kloeckner.i</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>