<?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 Wes Cole on Medium]]></title>
        <description><![CDATA[Stories by Wes Cole on Medium]]></description>
        <link>https://medium.com/@wesleymcole?source=rss-bf7fbb29aa92------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*rDKYepxJ3bKg6zgJlSVYvQ.jpeg</url>
            <title>Stories by Wes Cole on Medium</title>
            <link>https://medium.com/@wesleymcole?source=rss-bf7fbb29aa92------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 21:30:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wesleymcole/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[Sorting data from a GraphQL+GraphCool Endpoint]]></title>
            <link>https://medium.com/@wesleymcole/sorting-data-from-a-graphql-endpoint-13bc863ae46?source=rss-bf7fbb29aa92------2</link>
            <guid isPermaLink="false">https://medium.com/p/13bc863ae46</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Wes Cole]]></dc:creator>
            <pubDate>Sat, 26 Aug 2017 13:54:52 GMT</pubDate>
            <atom:updated>2017-08-29T12:24:53.717Z</atom:updated>
            <content:encoded><![CDATA[<p>The other day, I was working on a pretty simple update to the app that I’m building on the side, <a href="https://trackdatbaby.herokuapp.com/">Track Dat Baby</a>, and I ran into a problem when I was trying to sort a list of data from GraphQL by date. In hindsight this was a really simple thing to do, I was just approaching it from the wrong direction.</p><p>If you aren’t familiar with what GraphQL is or what queries look like, I would recommend taking a read through the <a href="http://graphql.org/">GraphQL Documentation</a>. I’m also using <a href="https://www.graph.cool/">GraphCool</a> which adds some much helpful filters and query arguments that makes working with data a lot easier. I’m going to jump right into my query and my initial way of trying to handle sorting.</p><p>First, let me give you a little bit of background on the app. Track Dat Baby is a simple app for new parents to track the number of feedings and dirty diapers that their new baby has. Its a very important set of data for new parents to know because it lets them know if their baby is progressing properly in the early stages. Track Data Baby’s GraphQL schema has three different types: User, Baby, and Entry. What I was trying to do was to get a User, which has a Baby, which has Entries added to it and then sort those entries by the date in which the user added them.</p><p>Entry Type Schema:</p><pre>type Entry implements Node {<br>  baby: Baby <a href="http://twitter.com/relation">@relation</a>(name: &quot;BabyOnEntry&quot;)<br>  childType: String!<br>  createdAt: DateTime!<br>  id: ID! <a href="http://twitter.com/isUnique">@isUnique</a><br>  parentType: String!<br>  time: DateTime! <a href="http://twitter.com/defaultValue">@defaultValue</a>(value: &quot;2017-07-30T22:51:45.384&quot;)<br>  updatedAt: DateTime!<br>}</pre><p>Ok, let’s work through my initial approach. Initially, I was querying for all entries associated to a baby with the following GraphQL Query.</p><pre>query {<br>  user {<br>    id<br>    baby {<br>      id<br>      entires {<br>        id<br>        parentType<br>        childType<br>        time<br>      }<br>    }<br>  }<br>}</pre><p>If you aren’t sure what that query is doing, first we request the current user, the baby associated to the user, and then the entries. We only want to get the ID, parentType, childType, and time fields in the Entry type. We are only going to be focused on the time field.</p><p>Ok, let’s get into listing out the entries and sorting them. I’m using React to build the app, so I was easily able to get the list of entries and display them using React Apollo.</p><pre>import React from &quot;react&quot;;<br>import<em> { </em>gql<em>, </em>graphql<em> } </em>from<em> &quot;react-apollo&quot;;</em></pre><pre>const EntriesList = props =&gt; {</pre><pre>  if (props.data.loading) {<br>    return (<br>      &lt;p&gt;{props.data.error.message}&lt;/p&gt;<br>    );<br>  }<br>  <br>  const entries = props.data.user.baby.entries;</pre><pre>  return (<br>    &lt;ul&gt;<br>      {entries.map((entry, key) =&gt;<br>        &lt;li&gt;{entry.time}&lt;/li&gt;<br>      }<br>    &lt;/ul&gt;<br>  );<br>}</pre><pre>export const entriesListQuery = gql`<br>  query entries {<br>    user {<br>      id<br>      baby {<br>        id<br>        entries {<br>          id<br>          parentType<br>          childType<br>          time<br>        }<br>      }<br>    }<br>  }<br>`;</pre><pre>const EntriesListWithData = graphql(entriesListQuery)(EntriesList);</pre><pre>export default EntriesListWithData;</pre><p>Now we have an EntriesListWithData component that will output an ul list with all of the entries listed out in the default order that they were created and with the newest being added to the bottom of the list. I would like to see the entries listed by their time field that the user sets and the newest added to the top of the list.</p><p>My first thought was “hey, Javascript has .sort maybe I should try that.” I’m sure you may already know what the problem is, but let me keep going because it stumped me and I’m sure it might stump some other people new to GraphQL. With Javascript .sort I thought that I could take the entries array and sort them before I passed them into .map like so.</p><pre>{entries<br>  .sort((a,b) =&gt; a.time - b.time )<br>  .map(entry =&gt; &lt;li&gt;{time}&lt;/li&gt; );<br>}</pre><p>Trying to run that would would always return a specific error that I had a hard time tracking down. The code would just fail and not give me any more information about it.</p><pre>TypeError: Cannot assign to read only property &#39;1&#39; of object &#39;[object Array]&#39;</pre><p>I kept running through and google options trying to figure out what was going on. Finally, something clicked and I realized that GraphQL has its own sorting capabilities and that the error that I was hitting was because GraphQL’s array was read only which was causing the error to appear on the .sort.</p><p>The fix for this is very trivial. It’s a matter of making a very small change to the GraphQL query.</p><pre>query entries {<br>  user {<br>    id <br>    baby {<br>      id<br>      entries(<br>        orderBy: time_ASC<br>      ) {<br>        id<br>        parentType<br>        childType<br>        time<br>      }<br>    }<br>  }<br>}</pre><p>If you look, you will notice that the only thing that I added was orderBy: time_ASC . What this did was order all entries by the time field and sorted them in ascending order. It was that simple. I would recommend reading through the <a href="https://www.graph.cool/docs/reference/simple-api/ordering-by-field-vequoog7hu/">documentation on GraphCool</a> as this is specific to their APIs.</p><p>While I spent more time than I should have trying to figure this out, I was thrilled to find GraphCool made it so easy. Just one more reason why I am loving using GraphCool + GraphQL.</p><p>I hope this helps someone else that might be trying to learn GraphQL and needing help finding some of the small features like this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13bc863ae46" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating an SVG Library with Twig Macros]]></title>
            <link>https://medium.com/@wesleymcole/creating-an-svg-library-with-twig-macros-a0b6b26037da?source=rss-bf7fbb29aa92------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0b6b26037da</guid>
            <category><![CDATA[timber]]></category>
            <category><![CDATA[twig]]></category>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[wordpress-web-development]]></category>
            <dc:creator><![CDATA[Wes Cole]]></dc:creator>
            <pubDate>Wed, 23 Aug 2017 13:43:00 GMT</pubDate>
            <atom:updated>2017-08-23T13:43:00.344Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UWkL4QRmHnvLiIoXPoU2qA.jpeg" /></figure><p>I have been using <a href="http://upstatement.com/timber">Timber</a> for building out WordPress projects and have loved how helpful it is to build sites with components and utilize features like macros to make building sites easier.</p><p>Before we get into the awesomeness of using macros, we need to set up our SVG icons so that we can easily access them with symbols.</p><p>The first thing that you will want to do is create an SVG sprite file.</p><pre>const rename = require(&quot;gulp-rename&quot;);<br>const svgmin = require(&quot;gulp-svgmin&quot;);<br>const svgstore = require(&quot;gulp-svgstore&quot;);<br>const cheerio = require(&quot;gulp-cheerio&quot;);</pre><pre>const paths = {<br>    icons: &quot;assets/icons/svg/*.svg&quot;<br>};</pre><pre>gulp.task(&quot;svg&quot;, () =&gt; {<br>    gulp<br>        .src(paths.icons)<br>        .pipe(svgmin())<br>        .pipe(rename({ prefix: &quot;icon-&quot; })<br>        .pipe(svgstore({ inlineSvg: true })<br>        .pipe(<br>            cheerio({<br>                run: function($, file) {<br>                    $(&quot;svg&quot;).attr(&quot;style&quot;, &quot;display: none&quot;);<br>                    $(&quot;[fill]&quot;).removeAttr(&quot;fill&quot;);<br>                    $(&quot;path&quot;).removeAttr(&quot;class&quot;);<br>                },<br>                parseOptions: { xmlMode: true }<br>            })<br>        )<br>        .pipe(gulp.dest(&quot;assets/icons&quot;)<br>});</pre><pre>gulp.task(&quot;default&quot;, [&quot;svg&quot;]);</pre><p>The above Gulp task will look into the svg directory in and take all SVG files and run through the following tasks:</p><ol><li>Minify each SVG file.</li><li>Rename adds a prefix in the ID for the specific SVG icon symbol so that we have a consistent naming scheme for all icons.</li><li>SVGStore takes all of the separate SVG files and combines them into one using the symbol element.</li><li>Cheerio will search the file and remove fills and classes so that all SVG styles are consistent.</li><li>Finally, the single SVG Sprite file containing all separate SVG icons will be outputted in the icons directory.</li></ol><p>Next, we need to add this SVG sprite file into the footer of the site. I’m typically using WordPress, so I use a function like this to add the file to the footer for all pages. You can add this function to your functions.php file and make sure you update the PARENT_THEME_DIR constant and SVG icons path.</p><pre>add_action( &#39;wp_footer&#39;, &#39;add_svg_sprite_to_footer&#39;, 9999 );</pre><pre>function add_svg_sprite_to_footer() {<br>    $svg_icons = PARENT_THEME_DIR . &#39;assets/icons/svg-icons.svg&#39;;<br>    <br>    if (file_exists( $svg_icons )) {<br>        require_once( $svg_icons );        <br>    }<br>}</pre><p>Ok, we are done setting up, so we can now use Twig macros to easily display SVG Icons wherever we want in our site. If you aren’t familiar with macros, I would recommend reading about them in the <a href="https://twig.symfony.com/doc/2.x/tags/macro.html">Twig Documentation</a>. In essence a macro is similar to a function in that you can output HTML in a reusable element.</p><p>Let’s start creating the macro.</p><pre>{% macro svg(options={}) %}<br>    <br>    {% set optons = {<br>        icon: &quot;&quot;,<br>        title: &quot;&quot;,<br>        desc: &quot;&quot;<br>    } | merge(options) %}</pre><pre>    {% set title = options.title ? options.title : options.icon %}<br>    {% set ariaHidden = &#39; aria-hidden=&quot;true&quot;&#39; %}</pre><pre>    &lt;svg class=&quot;icon icon-{{options.icon}}&quot; {{ariaHidden}} role=&quot;img&quot;&gt;<br>        &lt;title&gt;{{options.title}}&lt;/title&gt;<br>        {% if options.desc %}<br>            &lt;desc&gt;{{options.desc}}&lt;/desc&gt;<br>        {% endif %}<br>        &lt;use xlink:href=&quot;#icon-{{options.icon}}&quot;&gt;&lt;/use&gt;<br>    &lt;/svg&gt;</pre><pre>{% endmacro %}</pre><p>Let’s walk through what we are doing with the macro:</p><ol><li>The macro takes an options object that we will use later to specify which icon we want to display.</li><li>If you want default options, you can assign them in the options object that we create within the macro. Twig will merge the object that you pass into the macro with the defaults that you set.</li><li>If a title exists in the object, we are going to use that for the SVG title, otherwise, we use the icon property.</li><li>We will output the SVG HTML and pass in all of the appropriate variables from the options object.</li><li>You will notice that the &lt;use&gt; tag contains the icon- prefix that we set in the Gulp file. This allows you to pass in the name of the file that you want to access without having to use the prefix. You will see this in action in just a bit.</li></ol><p>You will probably want to create a macros directory within your Twig files directory and then I typically create a new file for each macro. Now that we have the macro built, we need to actually use it in our Twig files.</p><pre>{% <strong>import</strong> &#39;_macros/_svg.twig&#39; <strong>as</strong> m_svg %}<br>{{m_svg.svg({icon: &#39;facebook-square&#39;, title: &#39;Facebook&#39;, desc: &#39;Facebook Icon&#39;})}}<br>{{m_svg.svg({icon: &#39;twitter-square&#39;, title: &#39;Twitter&#39;, desc: &#39;Twitter Icon&#39;})}}<br>{{m_svg.svg({icon: &#39;instagram-square&#39;, title: &#39;Instagram&#39;, desc: &#39;Instagram Icon&#39;})}}</pre><p>I think this code sample will be the easiest to walk through yet. :)</p><ol><li>Import the macro from wherever you added it into your Twig directory. The import will assign all macros within that file to the name that you give it in the import. So, we can access the SVG macro by using m_svg.svg.</li><li>You will notice that each line under the import is invoking the svg macro and we are passing in an object with all of the appropriate parameters for the icon. One thing to note, the icon property in the object that you pass in should be the same as the name of the single SVG file that you are wanting to display.</li></ol><p>That’s it, You know have a macro that you can use throughout all of your Twig files to easily display SVG icons. We’ve run into a few use cases where you might need to still use separate inline SVG files, but for use cases like social media icons, this approach should make working with SVG files a lot easier.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0b6b26037da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Tachyons without cluttering up your HTML]]></title>
            <link>https://medium.com/@wesleymcole/using-tachyons-without-cluttering-up-your-html-7e108cd637b0?source=rss-bf7fbb29aa92------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e108cd637b0</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[tachyons]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[twig]]></category>
            <category><![CDATA[functional-css]]></category>
            <dc:creator><![CDATA[Wes Cole]]></dc:creator>
            <pubDate>Sat, 19 Aug 2017 22:03:31 GMT</pubDate>
            <atom:updated>2017-08-19T22:03:31.200Z</atom:updated>
            <content:encoded><![CDATA[<p>Tachyons is a wonderful functional CSS framework that I’m starting to use in my projects. If you don’t know what Tachyons is or haven’t read about functional CSS, go ahead and jump over to <a href="https://adamwathan.me/css-utility-classes-and-separation-of-concerns/">Adam Wathan’s pos</a>t about it.</p><p>While I have grown to appreciate and adopt the principles of functional CSS, I’m still not in love with all of the classes that you have to add to the HTML to style your site. I think that it makes your HTML less readable and clutters it up a bit too much.</p><p>My solution is to abstract your Tachyons classes into their own object for each file. This allows you to use a more semantic naming structure, keep all of your classes in one place, and make your HTML more readable and enjoy all of the benefits of functional CSS. Take a look at the example below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b66c21954b0bc904557c7b676b7b5f0/href">https://medium.com/media/3b66c21954b0bc904557c7b676b7b5f0/href</a></iframe><p>This is a pretty simple example, but it quickly demonstrates my approach. I store all of the classes in a styles object at the top of each file. This example is using <a href="https://twig.symfony.com/">Twig</a>, but you can use this same approach with any templating language that allows you to create objects directly in the file. Each HTML element that you are styling gets its own property within the styles object that you can use within your HTML. Even though this is a short example, you can quickly see the benefit of having your classes accessible via a single object. You know exactly where your classes are defined and don’t have to hunt through your HTML to make a change. Your HTML is also much more readable.</p><p>For the fun of it, I’m going to re-create the following component on the Tachyons’ site to show you the same approach with a more involved component.</p><p><a href="http://tachyons.io/components/headers/startup-hero/index.html">View the Start Up Hero Component</a></p><p>Below is the straight HTML component from the Tachyons site. It’s not horrible, but there are a ton of classes within the HTML that make it a lot more difficult to read and figure out where you are. Also, who wants to have to copy and paste all of those classes over and over again for a repeated item like the menu? I know you would most likely use a loop for that, but there will be use cases where you would be repeating the same classes over again.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67c2a139392a81fdac9fa9278470308b/href">https://medium.com/media/67c2a139392a81fdac9fa9278470308b/href</a></iframe><p>Below is the same file using the approach that I outlined above. First off, the styles object is kind of big and clunky, but I think the benefits of using it outweighs that issue. The HTML is much easier to read and you only have one location to edit all of the style classes. No copying and pasting for repeated elements because you have them stored in the object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f31af0f8dc7de01a03d9257f8c7310f/href">https://medium.com/media/1f31af0f8dc7de01a03d9257f8c7310f/href</a></iframe><p>I would appreciate your thoughts on this. So far, I haven’t run into any issues with it, but don’t presume that there aren’t any.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e108cd637b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>