<?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 Armand Aquino on Medium]]></title>
        <description><![CDATA[Stories by Armand Aquino on Medium]]></description>
        <link>https://medium.com/@apaquino?source=rss-c8c8aa8c710b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3MlVEthZSz9FHAyr.jpeg</url>
            <title>Stories by Armand Aquino on Medium</title>
            <link>https://medium.com/@apaquino?source=rss-c8c8aa8c710b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 04:22:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@apaquino/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: Learn by Doing (Part 3 of 3)]]></title>
            <link>https://medium.com/edge-coders/graphql-learn-by-doing-part-3-of-3-9b8ab33e56ad?source=rss-c8c8aa8c710b------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b8ab33e56ad</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Armand Aquino]]></dc:creator>
            <pubDate>Fri, 29 Jan 2016 19:53:31 GMT</pubDate>
            <atom:updated>2016-06-30T19:26:33.865Z</atom:updated>
            <content:encoded><![CDATA[<p>This is the last story for the 3 part series <strong>GraphQL</strong> “<em>Learn by Doing</em>” series.</p><p>Hope you enjoyed <a href="https://medium.com/@apaquino/graphql-learn-by-doing-part-1-of-3-9b04cadeacfa?source=latest---published_user------3">Part 1</a> and <a href="https://medium.com/@apaquino/graphql-learn-by-doing-part-2-of-3-4f02ae01d47d?source=latest---published_user------2">2</a> and have a working GraphQL server and can query the GitHub API. Let’s take it to the next level.</p><p>We last left off by taking the results from a GitHub API call and mapped each field that is returned. We also modified some of the fields so the developer and app have to do less work. Now we‘ll add <strong>new</strong> fields so the developer and app have to do <em>even less</em> work. Great, right?</p><p>Let’s just say that based on the data returned from the GitHub API call that we don’t just want to display the URL to get the information on the users a particular user is following but we want to get the actual information on those users as well. We want to display all the information on the current view and not have to pass that data to another component to make another API call or the user has to perform some action to display the data so we can lazy load the data. We want all the data for the initial render. Ok? Got it.</p><p>Since we have access to the data that is returned from the original data source and can add new fields, let’s add a new field to the UserInfoType that resolves the information we want by making another RESTful API call.</p><p>We need to create a new type with the fields that are available when calling the URL from “following_url” and create a new field with that type and resolve it by calling another API call.</p><p>I will not go through making that other type because you should be able to to do that by now. Run the API query again and make a new type based on the output. As usual, the link to the repo will be provided at the end of this article. There will be room to make the code DRY but that can be done another time in a future sprint.</p><p>The one thing you will notice when you see the results from that API call is that it is an array of objects with fields of that new type we just created. We will use a new type that we may not have seen before: “new GraphQLList()”. That is simply an object that expect a list of objects, or an object that is iterable like an array, of the certain GraphQL object type. Here is the sample code with the new field we will add to the UserInfoType.</p><pre>...<br>“users_following”: {<br>   type: new GraphQLList(RepoInfoType),<br>   resolve: (obj) =&gt; {<br>     const brackIndex = obj.following_url.indexOf(“{“),<br>           url = obj.following_url.slice(0, brackIndex);<br>     return axios.get(url)<br>                 .then(function(response) {<br>                   return response.data;<br>                 });<br>   }<br> },<br>...</pre><ul><li>Add new “users_following” field to the UserInfoType</li><li>Declare the type to be a list of the new type “RepoInfoType” (need to create)</li><li>Re-implement the logic to trim the “following_url” because we only get the the object returned by the data source - not how GraphQL resolve/returns it or new fields that we added. We can refactor this to keep it DRY if we want to in the future.</li><li>Call axios again with the URL string.</li></ul><p>Here is a screenshot of the udpated query in Graph<em>i</em>QL</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TOOr19six4x2XhpDctxHqQ.png" /></figure><p>Now we don’t have to make another API call because we under-fetched. I would like to point out that it still needs to be done somewhere but why should the client do it? Let’s make the client have a smaller footprint, get only the data it needs, and do less work. The GraphQL server will probably be on better hardware equipped to handle more data call and execute functions faster and more efficiently.</p><p>The other interesting part is you can design this to nest deeper into the “users_following” because you can call it again to see information on the users of the users the original user you are querying for is following. Hope I didn’t lose you there so below is a screenshot. Watch out though because you can get rate-limited by GitHub on this if you go crazy deep, I did. If that was your requirement for some reason, you can get it on one round-trip!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R3uXL23mueUWTMjCu3xSoQ.png" /></figure><p>As you add more types you will notice that you repeat a lot of fields. We can DRY up this code a few ways. Since this is JavaScript and the fields just expect objects, you can write a function that returns an object with the configuration you need. Here is some of the code you can write to re-use common fields in the types. Your code will look much cleaner.</p><pre>// Common fields<br>function usersFollowing() {<br>  return {<br>    type: new GraphQLList(RepoInfoType),<br>    description: “Fields about the people you this person follows”,<br>    resolve: (obj) =&gt; {<br>      const brackIndex = obj.following_url.indexOf(“{“),<br>      url = obj.following_url.slice(0, brackIndex);<br>      return axios.get(url)<br>                  .then(function(response) {<br>                    return response.data;<br>                  });<br>      }<br>  };<br>}</pre><pre>// Added field in the UserInfoType</pre><pre>...<br>“users_following”: usersFollowing(),</pre><p>This was just an easy implementation of GitHub on GraphQL to serve as an intro to get your feet wet. There are other things that can be done to make it more scalable, re-usable, and have more functionality. For example, using some features to prepare it for relayjs, an awesome framework by Facebook that uses GraphQL, will allow you to have functionality for pagination and handle nesting better.</p><p>Last thing I will show you is how to make queries to a GraphQL server from your React app. There are many ways to do this in a React app: In the componentWillMount lifecycle method or in an API call in the <em>actions</em> in Flux/Redux. You also have different XHR libraries to choose from like axios, fetch, or jquery. I will use axios in this example.</p><pre>axios.post(‘/graphql’, {<br> query: `<br> {<br>   gitHubUser(username:”${login}”) {<br>     login,<br>     avatar_url,<br>     following<br>     following_url,<br>     location,<br>     users_following {<br>       login,<br>       avatar_url<br>     }<br>   }<br>}<br> `<br>})<br>.then( response =&gt; {<br>// response from axios comes with a data object and so does graphql // that is why there is data.data.<br>// you can call another function here or if this is in a component you can call setState.</pre><pre>  return response.data.data;<br>});</pre><p>In this example, we create a query object that is passed to axios and it’s set to a string template that builds the GraphQL query. It expects the variable “login” to be set somewhere in the code, usually a variable in props or state. The one thing I want to point out here is that I use the “post” method here instead of “get” which is usually used for fetching data with RESTful API calls. That is because the “query” object I call is not just a url and can get very long. Using “get” has a limit of ~2000 characters because of the URL maximum length limit. You can unknowingly go over that limit using “get” query parameters and you don’t want to always check the length before you make the call. So I just use “post” all the time.</p><p>Here is a link to the <a href="https://github.com/apaquino/graphql-github-wrapper/tree/part3">repo</a>. It is not a complete implementation and not much work will be done on it in the near future so I can work on other GraphQL and Relay blog posts. However, feel free to fork, update, and make a PR, pull request if you want build on top of this and share it everyone.</p><p>Hope you found this series informative and learned more about GraphQL by doing. It was a joy to write because I think GraphQL is awesome sauce.</p><p><em>If you thought this article was helpful, then consider recommending ❤ the story so more people will find it.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b8ab33e56ad" width="1" height="1" alt=""><hr><p><a href="https://medium.com/edge-coders/graphql-learn-by-doing-part-3-of-3-9b8ab33e56ad">GraphQL: Learn by Doing (Part 3 of 3)</a> was originally published in <a href="https://medium.com/edge-coders">AZ.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL: Learn by Doing (Part 2 of 3)]]></title>
            <link>https://medium.com/edge-coders/graphql-learn-by-doing-part-2-of-3-4f02ae01d47d?source=rss-c8c8aa8c710b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f02ae01d47d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Armand Aquino]]></dc:creator>
            <pubDate>Fri, 29 Jan 2016 19:53:18 GMT</pubDate>
            <atom:updated>2016-06-30T19:29:50.041Z</atom:updated>
            <content:encoded><![CDATA[<p>This is Part 2 out of 3 of this <strong>GraphQL</strong> “<em>Learn by Doing</em>” series.</p><p>Hope you enjoyed <a href="https://medium.com/@apaquino/graphql-learn-by-doing-part-1-of-3-9b04cadeacfa?source=latest---------3">Part 1</a> and have a working GraphQL server. Let’s make it do some cooler things.</p><p>In this series we’ll be querying data from an external source. GraphQL is a server that is part of the data service layer in your application architecture. It doesn’t store the data but only queries the data or sends data to the data source so it can make the mutations. We‘ll only focus on the GraphQL query portion in *<em>this*</em> series but I’ll do another series on just mutations later.</p><p>Note that GraphQL can query data from multiple types of data sources. It can be from a relational database like PostgreSQL or a NoSQL datbase like MongoDB or a text file or a local private variable or from an RESTful API call. We’ll be making calls to the GitHub API to get our data. I chose the GitHub API because it is an external data source that doesn’t require any additional setup, doesn’t require you to sign up for an API access key, and many people can relate to it.</p><p>This is the URL you call to the API on either a browser or a XHR method:</p><pre><a href="https://api.github.com/users/apaquino">https://api.github.com/users/apaquino</a></pre><p>The last portion is like the <em>where</em> clause and should change to the github user login of the user you want to get data for. The return data will look something like this.</p><pre>{<br><strong>login</strong>: “apaquino”,<br><strong>id</strong>: 8031866,<br><strong>avatar_url</strong>: “<a href="https://avatars.githubusercontent.com/u/8031866?v=3">https://avatars.githubusercontent.com/u/8031866?v=3</a>&quot;, <strong><br>site_admin</strong>: false,<br><strong>... <br>following</strong>: 12,<br>...<br>}</pre><p>There are 30 fields in total but I only included a few in this example.</p><p>Most apps don’t need all 30 fields and only use a few to display on the UI, but when they make a RESTful API call, they get all 30 fields. If this was a “user” table in a relational database and you only need 5 specific fields, then would you use the SQL statement below? I hope not!</p><pre>SELECT * from USERS where LOGIN = ‘apaquino’</pre><p>Now lets make the GraphQL objects we need to setup the schema so our GraphQL server knows how to get the data.</p><p>First, let’s make a GraphQL object type that will have the fields we need so it can be the root query field. The data from the API call helps you determine the name and type to declare each field. For this example, I did not add a “description” field. If there is no “resolve” function, then it will match the name of the field from the data source to the field in the query “<em>fields”</em> object and return the value as-is. For example, “login” from the GitHub API will be returned when you query for login in the GraphQL server.</p><pre>const UserInfoType = new GraphQLObjectType({<br> name: “UserInfo”,<br> description: “Basic information on a GitHub user”,<br> fields: () =&gt; ({<br>   “login”: { type: GraphQLString },<br>   “id”: { type: GraphQLInt },<br>   “avatar_url”: { type: GraphQLString },<br>   ...    <br>   &quot;site_admin&quot;: { type: GraphQLBoolean },<br>   ...<br>})</pre><p>Now let’s update the query object to use this UserInfoType for the fields.</p><pre>const query = new GraphQLObjectType({<br>  name: “Query”,<br>  description: “First GraphQL Server Config — Yay!”,<br>  fields: () =&gt; ({<br>    gitHubUser: {<br>      type: UserInfoType,<br>      description: “GitHub user API data with enhanced and additional data”,<br>      args: {<br>        username: {<br>          type: new GraphQLNonNull(GraphQLString),<br>          description: “The GitHub user login you want information on”,<br>        },<br>      },<br>      resolve: (_,{username}) =&gt; {<br>        const url = `<a href="https://api.github.com/users/$">https://api.github.com/users/$</a>{username}`;<br>        return axios.get(url)<br>                    .then(function(response) {<br>                      return response.data;<br>                    });<br>      }<br>    },<br>  })<br>});</pre><ul><li>Add a root query field called gitHubUser</li><li>Declare it is has the fields defined in the UserInfoType we just created</li><li>Declare it will accept 1 argument named “username” that cannot be null</li><li>Describe how it will resolve getting the data by creating a URL string based on the argument passed in, use the get method in the axios module to make the RESTful API call, and return the data.</li></ul><p>There are a few fields that return a value that I think will require most front-end developers to perform extra work. Here is an example of one of them.</p><pre>following_url: “<a href="https://api.github.com/users/apaquino/following">https://api.github.com/users/apaquino/following</a>{/other_user}&quot;,</pre><p>That is the URL that can be used to make another RESTful API call. If you use that as-is, then you will get an error because of the {/other_user} portion. So you have to remove it.</p><p>With GraphQL, you can modify that value before it is returned to the app. In the “following_url” field in the UserInfoType fields object, let’s add a “resolve” function so we can perform some javascript on it before it is returned. Now it is more useful for the developer and developer and app has to do less work. This saves time for everyone so unless you are mean a person, you should love this.</p><pre>...<br>&quot;following_url”: {<br>   type: GraphQLString,<br>   resolve: (obj) =&gt; {<br>     const brackIndex = obj.following_url.indexOf(“{“);<br>     return obj.following_url.slice(0, brackIndex);<br>   }<br> },<br>...</pre><p>This implementation is just JavaScript so you can use it’s full power to do a lot of neat things. Since I have the entire return object at my disposal, we can access the data and any field, find out the position of the “{“ , <em>slice</em> the “following_url” field to return to the app a deep copy of the “following_url” without the portion we don’t want. (Try not to use <em>splice</em> to mutate the original object and make in-place updates, just don’t.)</p><p>Here is a screenshot of Graph<em>i</em>QL with a query for just 5 fields we want and the output that would be returned. Also, on the right is the UI explorer to see all the fields that is available and it’s type … just in case you don’t want to just depend on the autocomplete feature when typing your query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tVmlnnG2klh7MJUXT8Q2Xg.png" /></figure><p>Notice the “following_url” is nice and neat, and can be passed in to another a RESTful API call. Now suppose you want to make that call in just one trip? Note that you have access to the entire object that is returned so you can make <strong>new</strong> fields for the developer to query and give it a “resolve” function that can not only manipulate the data before it is returned but also make other JavaScript calls, like axios, to get the data. That is what will do in <a href="https://medium.com/@apaquino/graphql-learn-by-doing-part-3-of-3-9b8ab33e56ad?source=latest---published_user------1">Part 3</a> :)</p><p>Code from this blog can be found <a href="https://github.com/apaquino/graphql-github-wrapper/tree/part2">here</a>. I will have different branches for each part of the series.</p><p>Hope you enjoyed the second installment of <strong>GraphQL</strong>: <em>Learn by Doing</em>.</p><p><em>If you thought this article was helpful, then consider recommending ❤ the story so more people will find it.</em></p><p>Thanks for reading and stay tuned for more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f02ae01d47d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/edge-coders/graphql-learn-by-doing-part-2-of-3-4f02ae01d47d">GraphQL: Learn by Doing (Part 2 of 3)</a> was originally published in <a href="https://medium.com/edge-coders">AZ.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL: Learn by Doing (Part 1 of 3)]]></title>
            <link>https://medium.com/edge-coders/graphql-learn-by-doing-part-1-of-3-9b04cadeacfa?source=rss-c8c8aa8c710b------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b04cadeacfa</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Armand Aquino]]></dc:creator>
            <pubDate>Fri, 29 Jan 2016 19:53:01 GMT</pubDate>
            <atom:updated>2016-06-30T19:30:08.041Z</atom:updated>
            <content:encoded><![CDATA[<p>This will be Part 1 out of 3 of the <strong>GraphQL</strong> “<em>Learn by Doing</em>” series. I’m putting this together because I think “learning by doing” it is a great way to learn something. Remember the quote from my last post. Well here it is to refresh your memory:</p><blockquote>I hear and I forget, I see and I remember, I do and I understand.</blockquote><p>My last <a href="https://edgecoders.com/graphql-a-legit-reason-to-use-it-7858ce31638a#.l9zxz8z7f">GraphQL post</a> gave some legit reasons to use GraphQL. You may have read about it, hopefully got excited, and saw the code to declare what data you want, but you may have left without knowing how to implement a GraphQL server on your own. Here is how this series will be broken down:</p><ul><li>Part 1: Install and configure a simple GraphQL server.</li><li><a href="https://medium.com/@apaquino/graphql-learn-by-doing-part-2-of-3-4f02ae01d47d?source=latest---------2">Part 2: Configure the server to be the data layer to get GitHub user data via their API</a>.</li><li><a href="https://medium.com/@apaquino/graphql-learn-by-doing-part-3-of-3-9b8ab33e56ad?source=latest---published_user------1">Part 3: Add new fields beyond the GitHub API to extend what kind of data can be retrieved</a>.</li></ul><p>Sound good? All aboard? Ready to see how cool beans this is? Let’s go.</p><p>I won’t make this a super step-by-step but close. I will assume you have knowledge of javascript, express, node, npm, and you have node and npm installed on your system. FYI, there will be a link to the repo at the end of this article but I suggest you follow along as much as you can.</p><p>Let’s start by making a directory, setting up npm, and downloading the modules you will need to install and configure a GraphQL server.</p><pre>mkdir github-graphql-server<br>cd github-graphql-server<br>npm init</pre><p>When running npm init, type “server.js” for the “entry point” and accept the default values for the rest of the prompted questions.</p><p>We’ll be creating a GraphQL server using express. We will also be writing all the code in ES6/2015 because you should get used to it and start making it a habit because it is the future, IMHO.</p><pre>npm install --save graphql express express-graphql<br>npm install --save babel-cli babel-core babel-loader babel-preset-es2015</pre><p>Create a directory named “data” and file named “schema.js” inside it.</p><p>This will be the schema that describes the data so that your GraphQL server will understand how to retrieve data and transfer information to the Graph<em>i</em>QL tool. Oh yeah, we’re making that too.</p><p>In the data/schema.js file, start by importing some objects from the graphql module. It‘s more than what we need for now but we’ll definitely use it later. Not including all of it now will be the cause of a lot of your errors in the future.</p><pre>import {<br> GraphQLSchema,<br> GraphQLObjectType,<br> GraphQLInt,<br> GraphQLString,<br> GraphQLList,<br> GraphQLNonNull,<br> GraphQLID,<br> GraphQLBoolean,<br> GraphQLFloat<br>} from ‘graphql’;</pre><p>We then make a <em>query</em> object so GraphQL can use it to make a schema that understands how to query/fetch and return the data. We’ll be making two fields that we can query for. A “hello” field that takes one argument “name” that cannot be blank/null and returns a hello message, and “luckyNumber” field that returns a fixed number. Here is the code and I’ll explain after.</p><pre>const query = new GraphQLObjectType({<br>  name: “Query”,<br>  description: “First GraphQL Server Config — Yay!”,<br>  fields: () =&gt; ({<br>    hello: {<br>      type: GraphQLString, <br>      description: &quot;Accepts a name so you can be nice and say hi&quot;,<br>      args: {<br>        name: {<br>          type: new GraphQLNonNull(GraphQLString),<br>          description: “Name you want to say hi to :)”,<br>        }<br>      },<br>      resolve: (_,args) =&gt; {<br>        return `Hello, ${args.name}!!!`;<br>      }<br>    },<br>    luckyNumber: {<br>      type: GraphQLInt, <br>      description: &quot;A lucky number&quot;,<br>      resolve: () =&gt; {<br>        return 888;<br>      }<br>    }<br>  })<br>});</pre><p>Each Query object requires a “name” property but you can also include an optional “description” property. I recommend including a “description” because it will automatically show up in the Graph<em>i</em>QL tool and can help your developers or you can create and maintain documentation because you like doing it =/</p><p>Next is the<em> “</em>fields” property which is required. It is a function that returns an object of the different field objects a developer can query for. Each field object requires a “type”<em> </em>which can be one of the GraphQL types that we imported or a custom GraphQL type object we create that returns multiple fields (We’ll see that in part 2.). GraphQL fields are strongly typed and you must declare what type it expects to receive. The next property each fields object requires is the “resolve” property which is a function. That will be used to return the data for the field or fields. In this simple example case, each field is resolved with static data. Usually this is a query from another data source.</p><p>If you need to pass arguments in order to resolve your fields, then you can include the “args” property. This is usually the case when you are making a call to the data source that requires arguments or user supplied information. The “args” property is an object that can return one or more objects in case you need to pass multiple arguments to your resolve function. You can also include a “description” property for each field. The “description”<em> </em>is very useful for Graph<em>i</em>QL and creates an excellent introspection system.</p><p>If you saw the code above and wondered what the first argument in the “resolve” function was or thought it was a typo, it is not. It is an underscore that denotes the first argument is not provided but the args object is. The order of the parameters in the resolve function matter. You can pass the object returned from the datasource to each resolve function. This way you can modify the data before it is returned or use mutliple data values from the object to determine the return value. If you need to do that, then it must be the first argument in the resolve function. Hard to explain — easy to demonstrate; we’ll see that in Part 2.</p><pre>resolve: (_,args) ...</pre><p>This schema has some really boring stuff, I know. We just want to test the server to make sure it works. We’ll be calling cooler stuff later, promise. Fun fact, the resolve function can handle promises.</p><p>Lastly for this file, create a GraphQL schema object, include the query object we just made in it, and then export the schema.</p><pre>const schema = new GraphQLSchema({<br> query<br>});</pre><pre>export default schema;</pre><p>Now let’s make a really simple express server that uses the express-graphql middleware. In the root directory, create a file called “server.js” and enter this code. I assume you are familiar with express and I’ll just explain the GraphQL specific parts.</p><pre>import express from ‘express’;<br>import GraphQLHTTP from ‘express-graphql’;<br>import schema from ‘./data/schema’;</pre><pre>const app = express();<br>const PORT = 8888;</pre><pre>app.use(“/GraphQL”, GraphQLHTTP({<br>    schema,<br>    graphiql: true<br>  })<br>);</pre><pre>app.listen(PORT, () =&gt; {<br> console.log(“Node/Express server for Flux/GraphQL app. listening on port”, PORT);<br>});</pre><p>We import and create variables for the schema and the express-graphql middleware module. Then use the middleware and make an endpoint called “/GraphQL”. You can call the endpoint whatever you want but this is the convention. The middleware is an object that expects a GraphQL schema object. Let’s also include the optional, but highly recommended, “graphiql” property set to true “graphiql:true” . That is how you setup the Graph<em>i</em>QL server; yes, that easy!</p><p>I added a following start command in the scripts section of the package.json file. This will allow you to use ES6/2015 code to start a node server that understands ES6/2015.</p><pre>“start”: “./node_modules/.bin/babel-node --presets es2015 ./server.js”</pre><p>From your terminal, go to the root directory of this app and type the following command:</p><pre>npm start</pre><p>Now, on your browser, go to <a href="http://localhost:8888/graphql">http://localhost:8888/graphql</a></p><p>You should see this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gMdc7U86vaaX0bzFDSFZkA.png" /></figure><p>As you start typing, it will autocomplete your available valid options with a descriptions if you provided them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*dYsAlMrh3btMMFzDwzYGqQ.png" /></figure><p>Here is a view of Graph<em>i</em>QL with the documentation explorer on the right where you can navigate and see what queries the GraphQL server supports, information on the fields, and more. Go play around and see what information is available on your own. You can get all these cool features just by adding “graphiql: true”, boo-yah. In the screenshot below the complete query appears on the left and results in middle … mission accomplished.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aIS1k1eronb30Zjq3d6Uhg.png" /></figure><p>Congrats on setting up your GraphQL server.</p><p>Code from this blog can be found <a href="https://github.com/apaquino/graphql-github-wrapper/tree/part1">here</a>. I will have different branches for each part of the series.</p><p>Hope you enjoyed the first installment of <strong>GraphQL</strong>: <em>Learn by Doing</em>.</p><p>Next up in <a href="https://medium.com/@apaquino/graphql-learn-by-doing-part-2-of-3-4f02ae01d47d?source=latest---------2">part 2</a> is configuring the GraphQL server to make calls to the GitHub API and return only the data you need and sometimes in a format more useful for the developer.</p><p><em>If you thought this article was helpful, then consider recommending ❤ the story so more people will find it.</em></p><p>Thanks for reading and stay tuned for more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b04cadeacfa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/edge-coders/graphql-learn-by-doing-part-1-of-3-9b04cadeacfa">GraphQL: Learn by Doing (Part 1 of 3)</a> was originally published in <a href="https://medium.com/edge-coders">AZ.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL: A legit reason to use it.]]></title>
            <link>https://medium.com/edge-coders/graphql-a-legit-reason-to-use-it-7858ce31638a?source=rss-c8c8aa8c710b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7858ce31638a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Armand Aquino]]></dc:creator>
            <pubDate>Sun, 17 Jan 2016 19:46:47 GMT</pubDate>
            <atom:updated>2016-06-30T19:30:23.315Z</atom:updated>
            <content:encoded><![CDATA[<p>I absolutely love ReactJS. Everytime there is new technology that integrates nicely with the ReactJS ecosystem, I write a quick prototype app or create a new branch on an existing app to see what the hype is all about … and if I like it.</p><blockquote>I hear and I forget, I see and I remember, I do and I understand.</blockquote><p>Continue reading only if you care about the following:</p><ul><li>You care about your users data plan</li><li>You want users to get the data quicker</li><li>You want your developers to do less work and have a better a “Developer Experience” #DX</li></ul><p>Did I get your attention?</p><p>GraphQL is another middleware layer between your client and your data. It allows your developers to get only the data they need and not over-fetch. Also, it can prevent developers from making extra API calls because they under-fetched. For my last point, it allows developers to do less work by not having to do data manipulation; declare the data you want and then use it. There are many more reasons why GraphQL is awesome but I’ll stick to these in this post.</p><p>When developers work with API data, they possibly do the following:</p><ul><li>Go the website, read the API documentation, and see if they have a page where you can make sample queries and get sample results</li><li>Or make a call from your application and either console.log the results or them write to the page to view.</li></ul><p>I had to do both of the above and make a GraphQL server to output most of the possible results from their API. Here is a sample query of all of the fields for the query I setup on the GraphQL server.</p><pre>query {<br> weatherForecast(city:”San Francisco”) {<br>   city {<br>     id,<br>     population,<br>     name,<br>     country,<br>     coord{<br>       lon,<br>       lat<br>     } <br>   },<br>   cnt,<br>   message,<br>   list{<br>     dt,<br>     dt_txt<br>     main {<br>       temp,<br>       temp_min,<br>       temp_max,<br>       pressure,<br>       sea_level,<br>       grnd_level,<br>       humidity,<br>       temp_kf,<br>       temp_f,<br>       temp_c<br>     },    <br>     temp_f_avg,<br>     pressure_avg,<br>     humidity_avg,<br>     weather {<br>       id,<br>       main,<br>       description,<br>       icon<br>     },<br>     wind{<br>       speed,<br>       deg<br>     },<br>     clouds{<br>       all<br>     }<br>    }<br>  }<br>}</pre><p>If you noticed, that looks like JSON without the values and most of us are familiar with this already. That query is only <em>most</em> of the fields available from the API, there is even more but the fields seems to be internal fields not revelevent to weather data. So imagine you would receive even more fields if you make RESTful API calls. However, not all those fields are from the API, I added extra fields and that is a good thing … more on that later.</p><p>Now here is the code to get only the data I need for an app I wrote earlier.</p><pre>query:<br>{<br> weatherForecast(city:”${term}”) {    <br>   temp_f_avg,<br>   pressure_avg,<br>   humidity_avg,<br>   city {<br>     id,<br>     name,<br>     coord{<br>       lat,<br>       lng: lon<br>     }<br>   },<br>   list {<br>     main {<br>       temp_f,<br>       pressure,<br>       humidity<br>     }<br>   }<br> }<br>}</pre><p>Several things I want to point out.</p><ul><li>My app only receives those fields. It is much less than had I called the data using RESTful API calls.</li><li>I aliased some data fields so the fields map nicely with my prop variable names. That is a bonus so I can use ES6/2015 enhanced object literals :)</li><li>That temp_f is something I added to in my query fields because the data only comes in kelvin, “temp”, and I want to work with fahrenheit.</li><li>The *_avg query fields I added that are average values for historic temp_f, pressure, and humidity data that had to be calculated on the front-end.</li></ul><p>This works out great for the users of the app working on mobile and have a limited data plan. Also works out for users with lower internet speeds because the app gets less data so users should get the data faster.</p><p>The extra fields I added made the developer do less work by not having to write extra code or helper functions to convert the data. No more need to write code or functions to convert the temperature from kelvin to fahrenheit or averages for temp, humidity, and pressure. The components are now thinner and more data comes in and used as-is as values for props so it can be displayed on the page. That may even be a micro-performance boost since the front-end no longer has to perform those calculations at render time and/or you do not need to import any other libraries like lodash to perform the calculations.</p><p>Developers still need to go to a site to see what data they have access to and get sample results. GraphQL has an app for that: GraphiQL. There is no need to update the documentation or website with details on the API call. GraphQL is strongly typed so you have to let the server know what type of data it is getting. You can also configure a lot of things about your query. For example, description of the query, fields, and arguments. Because of this, the server is self-documenting. How many developers love to create and maintain documentation? Here is a screenshot of GraphiQL</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*An3JiEqf_eTz5999LI-AYw.png" /></figure><p>Developers can go to the right and see what data is available with an easy to navigate UI. On the left, they can make a query they want and there is type-ahead so you cannot type anything that is not available. This prevents a lot of fustrating errors from typos and fat fingers. Then you can get a sample of the results. If you like what you see in the results, then you can copy and paste that query and use in your app with minimal changes. I feel this is much better as a front end developer to work with when compared to using RESTful APIs. The best part, all this came for free! I didn’t have to create or maintain this GraphiQL site. Any change I make on the GraphQL server show up here. How awesome is that!?</p><p>Here are some of my pros and cons based on my experience.</p><p>Pros:</p><ul><li>DX is great. Working with GraphiQL first blew me away. I wrote the app both ways and wondered where was this all my life.</li><li>I know what the data will look like when I get it.</li></ul><p>Cons:</p><ul><li>Setting up GraphQL and configuring it is a lot of work and typing.</li><li>*The code and functions still need to be created and they are just move from the front-end to the back-end. That really isn’t a con if you want DX to improve and keep your front-end code thinner.</li><li>Would possibly require another person on the team.</li></ul><p>For the con, it is worth it to me in my opinion. Overall, it is a huge net positive.</p><p>There is so much more I can write about GraphQL but I’ll save that for a part II in the future. Next post may be about making mutations on data. Stay tuned.</p><p>Conclusion: After writing serveral apps using GraphQL, I can say …. I like it, a lot.</p><p>Here is a link to the GitHub repo with the weather app if you want to view the code. It is a weather app written in ReactJS and uses Redux. <a href="https://github.com/apaquino/graphql-openweathermap-server">GraphQL-Weather-App</a>. To run it you will need an api key from <a href="http://openweathermap.org/api">openweathermap.org</a></p><p>If you want to learn more about all that is React and you live in the San Francisco Bay Area, then I suggest you go to this Meetup: <a href="http://www.meetup.com/ReactJS-San-Francisco/">ReactJS San Francisco</a>. After I go here, I end up writing or enhancing a prototype app (and I’m so stuffed from all the free food). I got so inspired that I am a co-orgainizer of this meetup : <a href="http://www.meetup.com/Declarative-Programmers/">Declarative Programmers</a> and we have a event on Saturday, January 23 with a FREE workshop on <a href="http://www.meetup.com/Declarative-Programmers/events/227482405/">ReactJS and GraphQL</a>.</p><p>Hope you found my post informative and agree that GraphQL is legit.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7858ce31638a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/edge-coders/graphql-a-legit-reason-to-use-it-7858ce31638a">GraphQL: A legit reason to use it.</a> was originally published in <a href="https://medium.com/edge-coders">AZ.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>