<?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 Steve Aragon on Medium]]></title>
        <description><![CDATA[Stories by Steve Aragon on Medium]]></description>
        <link>https://medium.com/@stearagon?source=rss-b6212886f7cd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-sl6WW7n9DElk-5r4iL38A.png</url>
            <title>Stories by Steve Aragon on Medium</title>
            <link>https://medium.com/@stearagon?source=rss-b6212886f7cd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 08:22:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@stearagon/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[GraphQL Ruby: Clean Up your Query Type]]></title>
            <link>https://medium.com/alphasights-engineering-blog/graphql-ruby-clean-up-your-query-type-d7ab05a47084?source=rss-b6212886f7cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7ab05a47084</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[querying]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Steve Aragon]]></dc:creator>
            <pubDate>Mon, 14 Mar 2016 13:14:28 GMT</pubDate>
            <atom:updated>2016-05-25T13:41:55.362Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6cz3wQf8SfOfW37P5lViXg.png" /></figure><h4>From RESTful to GraphQL</h4><p>At AlphaSights, the team I work on is writing an ambitious web application that redesigns our core workflow. At first, this application used a RESTful API to communicate between the Ember frontend and the Rails backend. Now, we are moving over to a GraphQL API which has only one endpoint from which to query and modify the database.</p><p>To implement this, we are using are own homegrown <a href="https://github.com/alphasights/ember-graphql-adapter">Ember Data GraphQL Adapter</a> as well as the <a href="https://github.com/rmosolgo/graphql-ruby">GraphQL Ruby</a> gem. If you’re unfamiliar with either, watch <a href="https://m.alphasights.com/no-rest-for-the-wicked-a-gentle-introduction-to-graphql-55008b46e3d#.pz2nm48rm">this video</a> for an intro to GraphQL given by my AlphaSights colleague, <a href="https://medium.com/@arkh4m">Ju Liu</a>, and/or check out this GraphQL Ruby <a href="https://medium.com/@gauravtiwari/graphql-and-relay-on-rails-getting-started-955a49d251de#.3lzrzstrb">tutorial</a> from the gem’s readme.</p><h4>Some Query Type of Way</h4><p>I am going to assume that you already know a little bit about GraphQL and the GraphQL Ruby gem, but here’s some additional info on what goes into your GraphQL Ruby schema (from the gem’s docs):</p><pre><em>- Types</em> which expose objects in your application</pre><pre><em>- Fields</em> which expose properties of those objects (and may connect types to other types)</pre><pre><em>- Query root</em>, which is a special type used for executing read queries</pre><pre><em>- Mutation root</em>, which is a special type used for executing mutations</pre><p>The main file we will look at is our query root, named <strong>query_type.rb</strong> in our schema, which can become quite complex as you build up the functionality of your GraphQL endpoint.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5324dbadde1096938f6ee82f40645d75/href">https://medium.com/media/5324dbadde1096938f6ee82f40645d75/href</a></iframe><p>For this example, we are working on an app that keeps track of players on a team.</p><p>Looking at the <strong>query_type.rb</strong>,<strong> </strong>we notice that we have a simple field, <strong>player</strong>, which grabs the player data using its id.</p><p>Next thing we notice is the more complex <strong>players</strong> field which contains an if/else statement within its resolve block.</p><p>We’ll want to abstract this out, which should make the code easier to follow and modify. Knowing you need to move the code is easy, but where/how do we move it?</p><h4>Resolvers?!</h4><p>We started using what we call resolvers to house our more complex resolve blocks. We isolate our if/else statement in our <strong>players</strong> resolver, below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80b069262111889d7355f5d4e7f02b63/href">https://medium.com/media/80b069262111889d7355f5d4e7f02b63/href</a></iframe><p>Since we know that these lines of code from the <strong>query_type.rb</strong>…</p><pre>-&gt; (obj, args, ctx) do<br> ...code...<br> end</pre><p>…are creating a lambda, which will eventually have <strong>call </strong>called upon it, we can write a <strong>call</strong> method and pass the arguments from <strong>(obj, args, ctx)</strong>. Also you’ll notice, we are only using <strong>arguments</strong> in the <strong>call</strong> method, so we use underscores for <strong>obj </strong>and <strong>ctx</strong> to note their unimportance or lack of use<strong>.</strong></p><p>We house this <strong>players</strong> resolver in a resolvers folder which leaves our current folder structure like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*BIMrW4mnJI9vw9l6zhg9Zw.png" /><figcaption>Our GraphQL folder structure</figcaption></figure><p>Now our <strong>query_type.rb</strong> file looks a little leaner as we place in a new instance of our <strong>players </strong>resolver on line 17 below…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb2332895529124fb5c60a63f082cce8/href">https://medium.com/media/cb2332895529124fb5c60a63f082cce8/href</a></iframe><p>…but we still have that darn, unpleasant if/else statement in our <strong>players</strong> resolver.</p><h4>To if/else, So Long Old Friend</h4><p>Here is an example of a pattern we use to get rid of some of the if/else statements that you may see in some of your resolve lambdas or even your Rails controllers. ::shudder::</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b3b9f8151cf87a75ef092f5543097a6/href">https://medium.com/media/8b3b9f8151cf87a75ef092f5543097a6/href</a></iframe><p>We create a method (line 9 above) that will iterate over a list of possible arguments (:<strong>ids, :team_id, :position_type) </strong>and direct it to the right query-handling method if said argument is present. In this example, if it does not contain an argument we expect, we end up grabbing all of the instances of the model (i.e. <strong>Player.all</strong>).</p><p>Some advantages of this pattern:</p><ul><li>clarity on what will happen when you receive a certain argument</li><li>single responsibility for each argument method</li><li>ease of adding additional arguments</li><li>ease of unit testing the resolver</li></ul><p>An example of a resolver test would be (this resolver could be tested like this before we broke up the if/else statement as well):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd101bd9b9c94dcc9c06f302de52306d/href">https://medium.com/media/dd101bd9b9c94dcc9c06f302de52306d/href</a></iframe><h4>One Last Change, Please</h4><p>Ok, one other thing we do here to keep our <strong>query_type.rb</strong> organized. We will look at the result first and then look at how we got there.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8cc0f242efa7e87a1bdfb66f9ad02b8/href">https://medium.com/media/e8cc0f242efa7e87a1bdfb66f9ad02b8/href</a></iframe><p>With only one &lt;model&gt;QueryType, it doesn’t make much sense to do this, but on a more sizable app, with tens, hundreds, dare I say thousands of these &lt;model&gt;QueryTypes, this structure would help.</p><p>To implement this change, we’ll store our <strong>player</strong> and <strong>players</strong> fields into one &lt;model&gt;QueryType file…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b6076a44f37966375aebbd08203de19/href">https://medium.com/media/5b6076a44f37966375aebbd08203de19/href</a></iframe><p>…and create something we call a<strong> FieldCombiner</strong> class, whose <strong>combine </strong>method will iterate through the list of QueryTypes, which are <strong>GraphQL::ObjectType</strong>s., and call <strong>fields </strong>on each one and merge the results into one hash<strong>.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2641d96164f2ac7548eb211fe24a1c5a/href">https://medium.com/media/2641d96164f2ac7548eb211fe24a1c5a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*TU7f_D8t0mEvPPJIj_MHAA.png" /><figcaption>Our final GraphQL folder structure</figcaption></figure><h4>And That’s It!</h4><p>This structure helps us easily navigate and build upon our GraphQL codebase as we leave the RESTful world behind. We are working on making these resolvers even easier to use and even more Rails-y, so expect more on the topic. Please comment below if you have any questions, suggestions or trolling impulses. I would love to hear what y’all think, and I hope this helps you as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*yoiPueas7WLWlpD6waEurg.gif" /><figcaption>That was easy!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7ab05a47084" width="1" height="1" alt=""><hr><p><a href="https://medium.com/alphasights-engineering-blog/graphql-ruby-clean-up-your-query-type-d7ab05a47084">GraphQL Ruby: Clean Up your Query Type</a> was originally published in <a href="https://medium.com/alphasights-engineering-blog">AlphaSights Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>