<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: John Kelly</title>
    <description>The latest articles on DEV Community by John Kelly (@johndashkelly).</description>
    <link>https://dev.to/johndashkelly</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F51641%2F42e1b0a7-63e8-4202-b711-755fa0d2f9e2.jpg</url>
      <title>DEV Community: John Kelly</title>
      <link>https://dev.to/johndashkelly</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johndashkelly"/>
    <language>en</language>
    <item>
      <title>A Remote Data Request API in Elm</title>
      <dc:creator>John Kelly</dc:creator>
      <pubDate>Mon, 22 Jan 2018 08:48:12 +0000</pubDate>
      <link>https://dev.to/johndashkelly/a-remote-data-request-api-in-elm-3e5e</link>
      <guid>https://dev.to/johndashkelly/a-remote-data-request-api-in-elm-3e5e</guid>
      <description>&lt;p&gt;&lt;em&gt;This post is about the core abstractions found in the elm-postgrest package and how those abstractions may be relevant to similar packages.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;In Elm, the design space of &lt;strong&gt;remote data request APIs&lt;/strong&gt; has seen its fair share of work.&lt;/p&gt;

&lt;p&gt;We have APIs like &lt;code&gt;lukewestby/elm-http-builder&lt;/code&gt; which provide a thin convenience layer over &lt;code&gt;elm-lang/http&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;addItem&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Cmd&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
&lt;span class="n"&gt;addItem&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;HttpBuilder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://example.com/api/items"&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;withQueryParams&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;world"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;withHeader&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;X-My-Header"&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Some Header Value"&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;withJsonBody&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;itemEncoder&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;withTimeout&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="kt"&gt;Time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;withExpect&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expectJson&lt;/span&gt; &lt;span class="n"&gt;itemsDecoder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;withCredentials&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="n"&gt;handleRequestComplete&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have APIs like &lt;code&gt;krisajenkins/remotedata&lt;/code&gt; which model the various states remote data can take.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;RemoteData&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;NotAsked&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Loading&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Failure&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Success&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, we have APIs like &lt;code&gt;jamesmacaulay/elm-graphql&lt;/code&gt;, &lt;code&gt;jahewson/elm-graphql&lt;/code&gt;, &lt;code&gt;dillonkearns/graphqelm&lt;/code&gt;, &lt;code&gt;mgold/elm-data&lt;/code&gt;, &lt;code&gt;noahzgordon/elm-jsonapi&lt;/code&gt;, and others which abstract over &lt;code&gt;elm-lang/http&lt;/code&gt; to provide an API which is nice in the domain language of their respective specification. We'll refer to this group of APIs as &lt;em&gt;backend specific request builders&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In addition to community efforts, Evan himself wrote up &lt;a href="https://gist.github.com/evancz/1c5f2cf34939336ecb79b97bb89d9da6"&gt;a vision for data interchange in Elm&lt;/a&gt;. And although the API for this specific vision likely sits on the same level of abstraction as &lt;code&gt;elm-lang/http&lt;/code&gt;, &lt;code&gt;Json.Decode&lt;/code&gt;, and &lt;code&gt;Json.Encode&lt;/code&gt; rather than backend specific request builders, it legitimized the exploration around "how do you send information between clients and servers?"&lt;/p&gt;

&lt;h1&gt;
  
  
  Design Space
&lt;/h1&gt;

&lt;p&gt;What is in the design space of remote data request APIs? More specifically, what is in the design space of backend specific request builders? &lt;/p&gt;

&lt;p&gt;For the sake of this post, we'll define the design space as:&lt;/p&gt;




&lt;p&gt;A means to &lt;strong&gt;describe the capabilities of a data model&lt;/strong&gt; and subsequently &lt;strong&gt;build requests against that data model&lt;/strong&gt; for client-server applications.&lt;/p&gt;




&lt;p&gt;With the following design goals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Domain Language vs HTTP&lt;/strong&gt; - We want to interact with our backends in their own terms rather than their raw transfer protocol. For example, in the context of GraphQL, this means queries, mutations, selection sets, fragments, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Selections vs Decoders&lt;/strong&gt; - We want to speak in terms of what we wish to select rather than how we wish to decode it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resources vs JSON&lt;/strong&gt; - We want to speak in terms of the abstract representation of our data model rather than its specific interchange format and/or storage format.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Typed vs Untyped&lt;/strong&gt; - We want to compose our requests using the values of our application rather than the concatenation of query strings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a second look at these design goals but this time in the form of a diagram:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
   &lt;tr&gt;
      &lt;th&gt;Request Builder&lt;/th&gt;
      &lt;th&gt;Schema Description&lt;/th&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
      &lt;td&gt;CRUD requests &lt;br&gt; selections &lt;br&gt; conditions &lt;br&gt; order &lt;br&gt; limit and offset &lt;br&gt; pagination&lt;/td&gt;
      &lt;td&gt;resource schema&lt;br&gt;attributes&lt;br&gt;relationships&lt;br&gt;cardinality&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
      &lt;td colspan="2"&gt;
         &lt;center&gt;&lt;i&gt;Abstraction Barrier&lt;/i&gt;&lt;/center&gt;
      &lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
      &lt;th&gt;Transfer Protocol&lt;/th&gt;
      &lt;th&gt;Interchange Format&lt;/th&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
      &lt;td&gt;HTTP:&lt;br&gt;headers&lt;br&gt;body&lt;br&gt;methods&lt;br&gt;url: query and fragment&lt;br&gt;status codes&lt;/td&gt;
      &lt;td&gt;json&lt;br&gt;edn&lt;br&gt;xml&lt;br&gt;transit&lt;br&gt;protobuf&lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The dividing horizontal line in the diagram represents an &lt;em&gt;abstraction barrier&lt;/em&gt;. The barrier, in this case, separates backend specific request builders (above) from their implementation (below). Users at one layer should not need to concern themselves with the details below. The remainder of this post will examine an Elm API at the abstraction level of backend specific request builder.&lt;/p&gt;

&lt;h1&gt;
  
  
  elm-postgrest
&lt;/h1&gt;

&lt;p&gt;I'm the author of &lt;code&gt;john-kelly/elm-postgrest&lt;/code&gt;; a package that abstracts over &lt;code&gt;elm-lang/http&lt;/code&gt;, &lt;code&gt;Json.Decode&lt;/code&gt;, and &lt;code&gt;Json.Encode&lt;/code&gt; to provide a nice API in the context of PostgREST. Like previously stated, this package falls into the category of backend specific request builders.&lt;/p&gt;

&lt;p&gt;This post is about the core abstractions found in the elm-postgrest package and how those abstractions may be relevant to similar packages. All examples will be based on the work from &lt;code&gt;john-kelly/elm-postgrest-spa-example&lt;/code&gt;, which is an almost complete port of &lt;code&gt;rtfeldman/elm-spa-example&lt;/code&gt; to PostgREST. For those unfamiliar with PostgREST, here's an excerpt from their official documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PostgREST is a standalone web server that turns your PostgreSQL database directly into a RESTful API. The structural constraints and permissions in the database determine the API endpoints and operations ... The PostgREST philosophy establishes a single declarative source of truth: the data itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The mental model for how these 3 pieces fit together:&lt;/p&gt;




&lt;h4&gt;
  
  
  elm-postgrest (client) ⇄ PostgREST (server) ⇄ PostgreSQL (db)
&lt;/h4&gt;




&lt;p&gt;&lt;em&gt;In case you're wondering, no knowledge of PostgREST is necessary to make it through this post, however, intermediate knowledge of Elm and technologies like REST, GraphQL, JSON API, Firebase, Parse, or other remote data server specifications will be helpful.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Alright. Now that we have some context, let's dig into the code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Our First Request
&lt;/h1&gt;

&lt;p&gt;Our first request will retrieve all articles from our remote data server.&lt;/p&gt;

&lt;p&gt;For this example, we'll assume that we have a collection of article resources at &lt;code&gt;example.com/api/articles&lt;/code&gt;. Each article has a title, a body, and the count of the number of favorites.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I take a top down approach for the code in this example. Keep this in mind! Later sections will help you better understand earlier sections.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Types
&lt;/h2&gt;

&lt;p&gt;We're going to start out by looking at 4 of the core types in elm-postgrest. I provide the internal implementation of each type, however, don't get bogged down in the definition. I show the implementation in an attempt to ground the new PostgRest types to something you're familiar with.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;PostgRest&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;
    &lt;span class="k"&gt;exposing&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Selection&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Request&lt;/strong&gt; - A fully constructed request. The only thing left to do is convert this value into an &lt;code&gt;Http.Request&lt;/code&gt; and send it off to the Elm runtime. As we'll learn, a &lt;code&gt;Request&lt;/code&gt; can be constructed with a &lt;code&gt;Selection&lt;/code&gt; and a &lt;code&gt;Schema&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Read&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Parameters&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Decode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Decoder&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Selection&lt;/strong&gt; - The &lt;code&gt;Selection&lt;/code&gt; is one of the primary means to build requests against the data model. Specifically, the &lt;code&gt;Selection&lt;/code&gt; represents which fields to select and which related resources to embed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Selection&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Selection&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;
         &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;attributeNames&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
            &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embeds&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;Parameters&lt;/span&gt;
            &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Decode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Decoder&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Schema&lt;/strong&gt; - The &lt;code&gt;Schema&lt;/code&gt; is the means to describe the capabilities of a data model. &lt;em&gt;Capabilities&lt;/em&gt; means what we can select, what we can filter by, and what we can order by. We're only going to cover selection in this post.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Attribute&lt;/strong&gt; - An individual select-able unit of a &lt;code&gt;Schema&lt;/code&gt;. For example, the article resource has a title &lt;code&gt;Attribute String&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Decode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Decoder&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Encode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Value&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;urlEncoder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Request
&lt;/h2&gt;

&lt;p&gt;Here we're constructing a &lt;code&gt;Request&lt;/code&gt; which will result in a &lt;code&gt;List String&lt;/code&gt;. The mental model for this type should be the same as that of an &lt;code&gt;Http.Request&lt;/code&gt;: "If we were to send this &lt;code&gt;Request&lt;/code&gt;, we can expect back a &lt;code&gt;List String&lt;/code&gt;."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;getArticles&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;getArticles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readAll&lt;/span&gt; &lt;span class="n"&gt;articleSchema&lt;/span&gt; &lt;span class="n"&gt;articleSelection&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a look at the function signature of &lt;code&gt;PG.readAll&lt;/code&gt; before moving on to the next section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;readAll&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Selection&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see by the signature of &lt;code&gt;readAll&lt;/code&gt;, a &lt;code&gt;Request&lt;/code&gt; can be constructed with a &lt;code&gt;Selection&lt;/code&gt; and a &lt;code&gt;Schema&lt;/code&gt;. Let's now take a look at our &lt;code&gt;Selection&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selection
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Selection&lt;/code&gt; type has 2 type parameters: &lt;code&gt;attributes&lt;/code&gt; and &lt;code&gt;a&lt;/code&gt;. The mental model for reading this type is "If given a &lt;code&gt;Schema&lt;/code&gt; of &lt;code&gt;attributes&lt;/code&gt;, a value of type &lt;code&gt;a&lt;/code&gt; could be selected."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;articleSelection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;Selection&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
            &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;articleSelection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things will look vaguely familiar if you've worked with &lt;code&gt;Json.Decode.field&lt;/code&gt;. This is intentional. Overall, you'll find that the &lt;code&gt;Selection&lt;/code&gt; API is quite similar to the &lt;code&gt;Decoder&lt;/code&gt; API. Let's examine the signature of &lt;code&gt;PG.field&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Selection&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A field &lt;code&gt;Selection&lt;/code&gt; is composed of a dot accessor for an &lt;code&gt;Attribute&lt;/code&gt;. If we remember back to the mental model for a &lt;code&gt;Selection&lt;/code&gt;, we'll recall that we're in need of a &lt;code&gt;Schema&lt;/code&gt; to fulfill the &lt;code&gt;Selection&lt;/code&gt;. Given that the first type parameter of our &lt;code&gt;articleSelection&lt;/code&gt; is &lt;code&gt;{ attributes | title : Attribute String }&lt;/code&gt;, our &lt;code&gt;Schema&lt;/code&gt; will likely itself have this record of &lt;code&gt;Attribute&lt;/code&gt;s. Let's take a look!&lt;/p&gt;

&lt;h2&gt;
  
  
  Schema
&lt;/h2&gt;

&lt;p&gt;In theory, we could pass anything as the second parameter to the &lt;code&gt;PG.schema&lt;/code&gt; function, but in practice this value will always be an Elm record of &lt;code&gt;Attribute&lt;/code&gt;s.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;articleSchema&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;favoritesCount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;articleSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;articles"&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title"&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;body"&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;favoritesCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;favorites_count"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;PG.schema&lt;/code&gt; takes a &lt;code&gt;String&lt;/code&gt; which corresponds to the path to our resource (ex: example.com/api/&lt;em&gt;articles&lt;/em&gt;) and a record of &lt;code&gt;Attribute&lt;/code&gt;s. This record of &lt;code&gt;Attribute&lt;/code&gt;s describes the capabilities of a data model. In our specific case, it describes what we are able to select! &lt;/p&gt;

&lt;p&gt;Let's take a look at how &lt;code&gt;Schema&lt;/code&gt; and &lt;code&gt;PG.schema&lt;/code&gt; are defined internally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;

&lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
&lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, we'll see that a &lt;code&gt;Schema&lt;/code&gt; is nothing more than a wrapper around a record of &lt;code&gt;Attribute&lt;/code&gt;s. And this is true, but it's important to highlight that it's an &lt;strong&gt;opaque&lt;/strong&gt; wrapper around a record of &lt;code&gt;Attribute&lt;/code&gt;s. It may not be immediately obvious, but it is this API that guides users towards a separation of the description of capabilities (&lt;code&gt;Schema&lt;/code&gt;) from the building of requests (&lt;code&gt;Selection&lt;/code&gt;). A user can't just write something like &lt;code&gt;PG.field mySchema.title&lt;/code&gt; because the record is wrapped, and a user can't just unwrap the &lt;code&gt;Schema&lt;/code&gt; because it's opaque! They are forced to use the functions provided by the package to compose things (namely &lt;code&gt;PG.field&lt;/code&gt;). This API guides users towards writing selections in terms of an eventual record of attributes!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hopefully the previous explanation sheds a bit of light on why &lt;code&gt;PG.field&lt;/code&gt; takes a dot accessor for an &lt;code&gt;Attribute&lt;/code&gt; rather than an &lt;code&gt;Attribute&lt;/code&gt; directly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before moving on, let's review a few of these type signatures side by side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readAll&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Selection&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;articleSelection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;Selection&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
            &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kt"&gt;String&lt;/span&gt;


&lt;span class="n"&gt;articleSchema&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;favoritesCount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just take a moment to take this all in. It's pretty cool how the pieces fit together, and we can thank Elm's extensible record system for that!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Just to wrap things up for those who are curious, there exists a function of type &lt;code&gt;PG.toHttpRequest : PG.Request -&amp;gt; Http.Request&lt;/code&gt;. From there you can convert to a &lt;code&gt;Task&lt;/code&gt; with &lt;code&gt;Http.toTask&lt;/code&gt; or directly to a &lt;code&gt;Cmd&lt;/code&gt; with &lt;code&gt;Http.send&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Did we meet our design goals?
&lt;/h3&gt;

&lt;p&gt;Yes! In our example, we built a request to read all the titles (Request Builder) of our article collection resource (Schema Representation) as opposed to making an HTTP GET request to the &lt;code&gt;api/articles?select=title&lt;/code&gt; URL (Transfer Protocol) and decoding the JSON response (Interchange Format). The former is how we expressed our request in the example, and the latter is an implementation detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  What has this design bought us?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Type Safety&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reuse&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Type Safety
&lt;/h4&gt;

&lt;p&gt;If the &lt;code&gt;Schema&lt;/code&gt; is valid, our &lt;code&gt;Request&lt;/code&gt; will be valid. Our &lt;code&gt;Selection&lt;/code&gt; is defined &lt;em&gt;in terms of&lt;/em&gt; a &lt;code&gt;Schema&lt;/code&gt;, and we can only construct a &lt;code&gt;Request&lt;/code&gt; if the &lt;code&gt;Schema&lt;/code&gt; and &lt;code&gt;Selection&lt;/code&gt; agree statically. Put another way, a subset of request building errors become static errors rather than logic errors.&lt;/p&gt;

&lt;p&gt;For example, let's say we mistype &lt;code&gt;.title&lt;/code&gt; when we're constructing our &lt;code&gt;Selection&lt;/code&gt;. If our &lt;code&gt;Schema&lt;/code&gt; correctly describes our remote resource, we'll get a nice compiler message. Let's take a look at that error message!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The definition of `articleSelection` does not match its type annotation.

18| articleSelection :
19|     Selection
20|         { attributes
21|             | title : Attribute String
22|         }
23|         String
24| articleSelection =
25|&amp;gt;    PG.field .titl

The type annotation for `articleSelection` says it is a:

    Selection { attributes | title : ... } String

But the definition (shown above) is a:

    Selection { b | titl : ... } a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty cool. However...&lt;/p&gt;

&lt;p&gt;Close readers will argue that we've just moved the logic error to the &lt;code&gt;Schema&lt;/code&gt; from the &lt;code&gt;Decoder&lt;/code&gt;. This is true, however, the difference is that we only have 1 &lt;code&gt;Schema&lt;/code&gt; for an entire entity as opposed to a &lt;code&gt;Decoder&lt;/code&gt; for each way we wish to decode the entity. A &lt;code&gt;Schema&lt;/code&gt; represents a single source of truth for all &lt;code&gt;Selection&lt;/code&gt; capabilities of a remote resource. This in turn reduces the surface area of decoding logic errors. &lt;/p&gt;

&lt;p&gt;So, in summary: If the &lt;code&gt;Schema&lt;/code&gt; is valid, our &lt;code&gt;Request&lt;/code&gt; will be valid.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reuse
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;Selection&lt;/code&gt; can be reused to construct &lt;code&gt;Request&lt;/code&gt;s with &lt;em&gt;any&lt;/em&gt; &lt;code&gt;Schema&lt;/code&gt; that has the proper &lt;code&gt;Attribute&lt;/code&gt;s! For example, if our remote data server had both article resources and book resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;articleSchema&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;favoritesCount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;articleSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;articles"&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title"&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;body"&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;favoritesCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;favorites_count"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;bookSchema&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;Schema&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pages&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;bookSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;books"&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title"&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pages"&lt;/span&gt;
        &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;authorName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;author_name"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could use the same &lt;code&gt;Selection&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;titleSelection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;Selection&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;
            &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Attribute&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;titleSelection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To construct our 2 separate requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;getArticles&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;getArticles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readAll&lt;/span&gt; &lt;span class="n"&gt;articleSchema&lt;/span&gt; &lt;span class="n"&gt;titleSelection&lt;/span&gt;

&lt;span class="n"&gt;getBooks&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;getBooks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;PG&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readAll&lt;/span&gt; &lt;span class="n"&gt;bookSchema&lt;/span&gt; &lt;span class="n"&gt;titleSelection&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty cool. However...&lt;/p&gt;

&lt;p&gt;To be completely honest, I have not yet had a need for this reuse feature. With that being said, there's still something about it that makes the API feel right.&lt;/p&gt;

&lt;p&gt;So, in summary: Extensible records in &lt;code&gt;Selection&lt;/code&gt; API grant us reuse.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which ideas could find their way into similar projects?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Schema&lt;/code&gt; as single source of truth for &lt;code&gt;Selection&lt;/code&gt; capabilities&lt;/li&gt;
&lt;li&gt;Separation of &lt;code&gt;Schema&lt;/code&gt; and &lt;code&gt;Selection&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Extensible records central to design of this separation&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Selection&lt;/code&gt; API similar to that of &lt;code&gt;Decoder&lt;/code&gt; API&lt;/li&gt;
&lt;li&gt;And more.. we'll discuss those in the future posts&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Future
&lt;/h1&gt;

&lt;p&gt;In the interest of space, time and boredom, I have not included all of the API designs of the &lt;code&gt;elm-postgrest&lt;/code&gt; package in this post. In the future, I may write posts to highlight the concepts which were left out here. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combining Selections&lt;/li&gt;
&lt;li&gt;Schema Relationships and Embedding Selections&lt;/li&gt;
&lt;li&gt;Conditions and Orders&lt;/li&gt;
&lt;li&gt;Create, Update, and Delete&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you'd like to view some more simple examples, here's a link to &lt;a href="https://github.com/john-kelly/elm-postgrest-example"&gt;the examples on github&lt;/a&gt;. Take a look at each individual git commit.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you'd like to see a more "RealWorld" example application, here's a link to &lt;a href="https://github.com/john-kelly/elm-postgrest-spa-example"&gt;john-kelly/elm-postgrest-spa-example&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you're interested in taking a look at the development of &lt;code&gt;john-kelly/elm-postgrest&lt;/code&gt;, head over to the &lt;a href="https://github.com/john-kelly/elm-postgrest/tree/dev"&gt;dev branch&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>elm</category>
      <category>api</category>
      <category>postgrest</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
