<?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 Gerald Monaco on Medium]]></title>
        <description><![CDATA[Stories by Gerald Monaco on Medium]]></description>
        <link>https://medium.com/@devknoll?source=rss-7261dc6f120d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WJBna3xu_ePzV613.jpg</url>
            <title>Stories by Gerald Monaco on Medium</title>
            <link>https://medium.com/@devknoll?source=rss-7261dc6f120d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:41:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@devknoll/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[Bernoulli Streams: An Idea]]></title>
            <link>https://medium.com/@devknoll/bernoulli-streams-an-idea-7d4fe345f45d?source=rss-7261dc6f120d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d4fe345f45d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[streaming]]></category>
            <dc:creator><![CDATA[Gerald Monaco]]></dc:creator>
            <pubDate>Wed, 01 Sep 2021 06:16:42 GMT</pubDate>
            <atom:updated>2021-09-01T06:16:42.347Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve grown frustrated with stream abstractions lately. I just want something that’s <strong>simple</strong> in both theory and implementation, supports backpressure, and allows explicit flushing.</p><p>One thing I noticed is that many solutions tend to treat backpressure as an afterthought. Having a similarly laissez-faire attitude while engineering the physical streams and pipes that we model our systems after, like in public drainage &amp; sanitary infrastructure, would be catastrophic.</p><p>Perhaps there has been a catastrophic failure in our abstractions after all, and that’s the source of my frustration?</p><p>Imagine we treated a Source and a Sink as two different water tanks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UVMI1MbcPAz8OvnPG2INEw.png" /></figure><p>Initially, the Sink is empty. And just like in real life, these tanks could also have different capacities:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tG_mK2pSSKtiJTVCqz8gZQ.png" /></figure><p>Now, if we wanted to move the water from the Source to the Sink, we’d put a pipe between them. We’ll add a pump too, so that we can overcome any differences in elevation, pressure, and so on. If we let the pump run, eventually everything will be transferred:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BLZCB4neEWf_G-M45a0Qfg.png" /></figure><p>There are a couple of things to note here:</p><ul><li>The Sink tank is nearly full. If Source had more water and we continued to try to pump it, Sink could experience a catastrophic failure.</li><li>Conversely, the Source tank is now empty. By pumping the water to Sink, we have freed up additional space in Source for more water.</li></ul><p>Of course, this is a closed system and so it’s not very interesting. Let’s imagine that we also have a water <em>producer</em> that can somehow pump water into Source, and a water <em>consumer</em> that can somehow pump water out of Sink:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q6UMx03QcI2TaZGVg0XrLQ.png" /><figcaption>Pretend the Sink also has a one-way valve to prevent water from flowing backward.</figcaption></figure><p>I said pump a lot, but it turns out we only need one pump, at the consumer, because the consumer decides when they need more water. The rest of the water will move as a result of the pressure differential:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*25ieW_iSG7ot_43D6Qz9Vw.png" /><figcaption>Don’t forget that Sink has a one-way valve.</figcaption></figure><p>Now, depending on how quickly the consumer can consume, the producer may eventually need to stop producing water, or the system will fill up and a catastrophic failure will occur.</p><p>Note too that in a physical system, as the remaining capacity in the Sink tank decreases, the pressure of the water flowing from the Source tank will need to increase in order to overcome the water attempting to flow back out. <em>This is what we call backpressure</em>.</p><p>But we can see that it’s not so much a property of the system itself, as it is an emergent property that comes from the interactions and relative velocities of all of the different water molecules between the two tanks, dictated at any given time by the respective quantities of water.</p><p>This is the key insight behind this post: that fundamentally we need only communicate tank capacities (which is simple) to also support backpressure, and explicit flushing.</p><p>So far we’ve achieved two out of the three, but let’s consider explicit flushing. Imagine that instead of water, we are pumping condiments: ketchup and mustard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g_3rl_PmNvvFWQZ_RapKWQ.png" /><figcaption>We pump our ketchup first.</figcaption></figure><p>Pretend we’re running a bottling line. We’re bottling ketchup first, and now we want to switch over to mustard. But we can’t let them mix. Using just tank capacities, how could the Source tell the Sink that it needs to get rid of the rest of the ketchup before we’ll flow the mustard?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IY4hKW4NWGBdC3OvwIu0uw.png" /><figcaption>Source won’t let the mustard flow yet.</figcaption></figure><p>The answer is straightforward: the Sink must be empty. Or put another way, it must have its maximum capacity available.</p><h4>Towards an API</h4><p>Finally, we can talk about what this might look like as an API. And it’s simple:</p><pre>type Source&lt;T&gt; = (capacity: number, sink: Sink&lt;T&gt;): void</pre><pre>type Sink&lt;T&gt; = (capacity: number, payload?: T): void</pre><p>Inspired by <a href="https://github.com/callbag/callbag">callbag</a>, Source and Sink are just functions. We <em>connect</em> them by calling Source with a sink and its current capacity:</p><pre>source(10, sink)</pre><p>As long as the sink has capacity, source can push data to it by calling sink . Each time sink is called, the capacity decreases. If we assume the units of capacity is elements (as opposed to bytes), then we might see something like this:</p><pre>// We connected sink with a max capacity of 10 elements</pre><pre>source(10, sink)</pre><pre>// source pushes an element by calling sink, and knows</pre><pre>// the current capacity is 9</pre><pre>sink(1, &quot;Foo&quot;)</pre><pre>// source pushes another element, and knows the current</pre><pre>// capacity is now 8</pre><pre>sink(1, &quot;Bar&quot;)</pre><p>At some point in the future, depending on how quickly sink is able to consume elements, it will inform source of the changing capacity:</p><pre>// sink has buffered two elements, &quot;Foo&quot; and &quot;Bar&quot;, taking</pre><pre>// it from a total capacity of 10 to a current capacity of 8</pre><pre>// After sink processes &quot;Foo&quot;, it will tell source it has room</pre><pre>source(9, sink)</pre><pre>// After sink processes &quot;Bar&quot;, it will tell the source it has room</pre><pre>source(10, sink)</pre><p>This is achieved by just calling source again with the same sink and the new capacity.</p><p>Notice the 1 that source has been calling sink with. This is communicating that sink needs a capacity of at least 1 to receive another payload. Now it’s clear how explicit flushes (which I am now calling “purges”, to continue the plumbing theme) can work: source can call sink with the max capacity (i.e. the first reported capacity) of 10 to tell it that it will need to reach a capacity of 10 before we will send it another payload.</p><pre>sink(10)</pre><p>What’s fascinating about this is that it need not always be 1 or the max capacity: the unit of capacity could also be bytes in a byte stream, and the number can simply be the size of the next chunk we have buffered to send. In either case, we make enough room in the sink by telling it how much space it will need before it will receive anything else.</p><p>Theoretically, a sink could then lie and say that it has enough room, but then it starts to run the risk of either having to throw away data (because there’s no affordance in this protocol for resending data), or experiencing a catastrophic failure if it tries and fails to allocate additional space.</p><p>Ultimately, it’s in the best interest of the sink (and any underlying streams) to play fairly. After all, the entire reason they are subscribed to our stream is so that they can receive the latest T as soon as possible.</p><p>This API really seems to be all we need. We don’t need to explicitly support stream termination like error or complete, because finite streams are just a special case of infinite streams and those values can be encoded in T by a particular implementation of source:</p><pre>type Value&lt;T&gt; =</pre><pre>  | { kind: &#39;value&#39;, value: T }</pre><pre>  | { kind: &#39;error&#39;, error: Error }</pre><pre>  | { kind: &#39;complete&#39; }</pre><p>We don’t need an explicit subscribe, because that’s covered by calling a source with a sink and a capacity. We don’t need an explicit unsubscribe, because that can be signaled by calling source with a capacity of Number.NEGATIVE_INFINITY.</p><p>I haven’t given this a try yet, but given how seemingly simple it is, I’m really excited to. In the meantime, feel free to poke holes in it or share your own thoughts.</p><p>Let me know if you’ve seen someone with a similar take on streaming (going beyond just the concept of pipes and backpressure). I called them “Bernoulli Streams” in the title of this post because “Pascal Streams” would be <a href="https://en.wikipedia.org/wiki/Pascal_(programming_language)">impossible to search for</a>, and the mental picture of liquids flowing between two vessels reminds me of <a href="https://en.wikipedia.org/wiki/Communicating_vessels">communicating vessels</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d4fe345f45d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Demystifying GraphQL]]></title>
            <link>https://medium.com/@devknoll/demystifying-graphql-86fb2febee14?source=rss-7261dc6f120d------2</link>
            <guid isPermaLink="false">https://medium.com/p/86fb2febee14</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gerald Monaco]]></dc:creator>
            <pubDate>Tue, 15 Sep 2015 07:00:19 GMT</pubDate>
            <atom:updated>2015-09-15T07:00:19.260Z</atom:updated>
            <content:encoded><![CDATA[<p>As GraphQL starts to get more and more attention, I found myself getting frustrated when reading comments about it. It seemed to click pretty quickly for me, but others seemed to approach it with a sense of confusion and uncertainty.</p><p><em>Why doesn’t everyone else get it?!</em></p><p>After giving it a bit thought, I think I may have figured it out. The name <em>GraphQL</em> is a combination of two buzzwords in the latest tech vernacular. No wonder people were getting confused — they’re probably trying to pattern match it and compare it to other new things they’ve heard of lately.</p><p>Of course, it’s not their fault. But I really like GraphQL, and I really want more people to start using it, so I feel like it’s partially my responsibility to try to clear up some of the confusion.</p><p>My suggestion is to mentally replace <em>GraphQL</em> in your mind with either <em>ProductQL</em> or <em>BackendQL</em> for a moment, and then read on.</p><ul><li>It‘s not a storage layer</li><li>It’s not a query language (in the sense you’re probably thinking)</li><li>It has little to do with graphs (in the sense that you’re probably thinking)</li><li>It has no notion of fetching/sorting (as you might initially believe)</li></ul><p>What is it? It’s a <em>language</em> for defining a <em>strongly typed</em> API/interface for your <em>product</em> or service. Backend developers write a thin, typed interface over your <em>existing</em> application layer and business logic, keeping in mind any performance or technical constraints (i.e. caching anything expensive or not exposing it at all).</p><p>This interface is accessed via a <em>single</em> HTTP endpoint by <em>product</em> developers, using string based queries that are closer to the actual mental model of a product than, for example, a REST API.</p><p>Simply put, GraphQL is a language that your server engineers use to expose a better API for your product developers to use in order to build nice things.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86fb2febee14" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rendering React with Amazon Lambda]]></title>
            <link>https://medium.com/@devknoll/rendering-react-with-amazon-lambda-e4e85a788257?source=rss-7261dc6f120d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4e85a788257</guid>
            <dc:creator><![CDATA[Gerald Monaco]]></dc:creator>
            <pubDate>Sun, 09 Aug 2015 22:37:43 GMT</pubDate>
            <atom:updated>2015-08-09T22:45:06.895Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been playing around with Amazon’s new AWS service, <a href="https://aws.amazon.com/lambda/">Lambda</a>. It lets you write write functions and execute them on demand without having to worry about provisioning or scaling servers. You could also theoretically write your entire backend with just lambda functions.</p><p>I like React, but I wanted to see if I could speed up initial page loads (and support browsers that have JavaScript disabled, and SEO…) without worrying about running a server. Enter an even newer service from Amazon, <a href="https://aws.amazon.com/api-gateway/">API Gateway</a>.</p><p>With API Gateway, you can build an interface for accessing and securing text-based APIs via proxying or Lambda functions. As it turns out, you can also render and serve your React application too.</p><p>I’ll quickly go through the steps that I used to get this working. I assume you are already familiar with developing React apps using the NPM, but that you’re not terribly familiar with Lambda or the API Gateway.</p><h3>Our Lambda Function</h3><p>I just started a new NPM module for my test, adding in the <em>react</em> package. Node.js lambda functions use a <em>&lt;FunctionName&gt;.js</em> format, where the module exports a <em>handler</em> function. So my <em>index.js</em> file looks like this:</p><pre>var React = require(‘react’),<br>    DOM = React.DOM;</pre><pre>var testComponent = React.createFactory(React.createClass({<br> render: function() {<br>   return DOM.span(null, “hello”);<br> }<br>}));</pre><pre>exports.handler = function(e, context) {<br> context.succeed(React.renderToStaticMarkup(DOM.body(null,<br>   testComponent(null)<br> )));<br>};</pre><p>Pretty simple. When <em>exports.handler</em> is called by Amazon, we’ll make the lambda succeed and pass in our rendered HTML markup.</p><p>Next, zip up your <em>index.js</em> and <em>node_modules</em> (make sure that index.js is at the root, not inside a folder). Next, create a new Lambda function, skipping the blueprint selection:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eCrWh5vw9Xr4udMEOCE3jA.png" /></figure><p>Select a role, then create the function. In my case, the defaults were fine.</p><h3>Our API Gateway</h3><p>Start by creating a new API:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ei932PfXnhP-hNt25t0wbw.png" /></figure><p>Next we need to create a <em>GET</em> method for when the browser hits our API, and have it call our lambda. I just created one on the root like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zNUjTNmiIzlv8yS27zimXA.png" /></figure><p>Now the confusing part. By default, API Gateway assumes your API is <em>application/json</em>. We need <em>text/html</em>. So enter your Method Response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*tP6dTp3WOrPwFH0QIn-lkw.png" /></figure><p>And change the <em>Response Model for 200</em> to <em>text/html</em>. Now go back and edit the <em>Integration Response</em>. Edit the <em>application/json</em> mapping template to <em>text/html</em>, and change <em>Output Passthrough</em> to <em>Mapping Template</em>. Finally, enter:</p><pre>$input.path(‘$’)</pre><p>This is just telling Amazon to take the output from our function (a JSON string) and write it out directly, instead of as JSON. It should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UZG2_xztrBmiULzwUBYYvw.png" /></figure><p>Click the check next to <em>Mapping Template</em> to save it. Now you just need to <em>Deploy API</em> and test it out!</p><p>If everything was done correctly, you should see <em>hello</em> as the output. You may want to verify that the correct headers are being set too.</p><h3>Where To Go Next</h3><p>This is a pretty simple example. For a production app, you would want to set up routing in your React app. Unfortunately, API Gateway doesn’t currently seem to support wildcard or RegEx in paths (though simple <em>Resources</em> can be parameterized). That means if you want to render more than just the home page, you’ll have to somehow import some of your routes into API Gateway.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4e85a788257" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL in Production]]></title>
            <link>https://medium.com/@devknoll/graphql-in-production-77098078b54d?source=rss-7261dc6f120d------2</link>
            <guid isPermaLink="false">https://medium.com/p/77098078b54d</guid>
            <dc:creator><![CDATA[Gerald Monaco]]></dc:creator>
            <pubDate>Wed, 22 Jul 2015 19:19:57 GMT</pubDate>
            <atom:updated>2015-07-22T22:31:57.796Z</atom:updated>
            <content:encoded><![CDATA[<p>At <a href="https://www.makeschool.com/">Make School</a>, we are using GraphQL and a Relay-like interface to power our Summer Academy. Our students have a SPA interface that they can use to browse the curriculum, watch recorded lectures, and ask questions.</p><p>As GraphQL is relatively new and there are lots of questions about it, I thought it would be beneficial to show a subset of our own schema and how we implement a few features on top of it.</p><h3>Schema</h3><p>Here’s a subset of our schema that we’ll use to implement a few features. We have about six other types and seven additional mutation types not shown here, which span curriculum content/progress, user profile management, editing questions / selecting best answers, etc.</p><pre>interface IUser {<br>  name: String,<br>  profile_image_url: String,<br>  checked_in: Boolean<br>}</pre><pre>type User : IUser {<br>  name: String,<br>  profile_image_url: String,<br>  checked_in: Boolean<br>}</pre><pre>type Viewer : IUser {<br>  name: String,<br>  profile_image_url: String,<br>  current_track: Track,<br>  accessible_tracks: [Track],<br>}</pre><pre>type Track {<br>  id: String,<br>  name: String,<br>  image_url: String,<br>  users: [IUser],<br>}</pre><pre>type QueryRoot {<br>  viewer: Viewer<br>}</pre><pre>type MutationRoot {<br>  set_profile_picture(url: String),<br>  change_track(id: String)<br>}</pre><h3>Feature: Static Navigation Bar</h3><p>We have a NavBar component on most pages that shows the user some information about their current track and user profile.</p><p>This is implemented in React using a <a href="http://facebook.github.io/react/blog/2015/03/19/building-the-facebook-news-feed-with-relay.html">Relay</a>-like interface to specify all of our data requirements via GraphQL:</p><pre>var NavBar = React.createClass({<br> // …<br>});</pre><pre>var NavBarContainer = Delay.createContainer(NavBar, {<br> queries: {<br>   viewer: graphql`<br>     {<br>       viewer {<br>         current_track {<br>           name,<br>           image_url,<br>         },<br>         name,<br>         profile_image_url<br>       }<br>     }<br>   `<br> }<br>});</pre><p>The Relay-like API ensures that our NavBar component is rendered once all of the data is retrieved. The data itself is passed in via props.</p><p>But what if our user is visiting the page for the first time, and they don’t have a track or profile picture?</p><p>This seemed like a good place to insert our “first time user” flow. Inside NavBar, we have an invariant that looks like:</p><pre>if (!this.props.viewer.current_track || <br>  !this.props.viewer.profile_image_url) {<br> this.transitionTo(‘/welcome’);<br>}</pre><h3>Feature: Welcome Page</h3><p>We have another component that represents our Welcome page. Using the same Relay-like API, that looks like:</p><pre>var WelcomePageContainer = Delay.createContainer(WelcomePage, {<br> queries: {<br>   viewer: graphql`<br>     {<br>       viewer {<br>         accessible_tracks {<br>           id,<br>           name,<br>           image_url,<br>         }<br>       }<br>     }<br>   `<br> }<br>});</pre><p>We use the accessible_tracks to render a grid that allows the user to select a track. Once selected, we fire off a change_track mutation, passing in the ID.</p><p>We do the same thing with the profile picture. Our front-end has a picture selection UI and produces a URL, which is passed on to set_profile_picture.</p><p>Once we’re done, we can redirect back to the previous page.</p><h3>Bonus: Authentication</h3><p>There’s a little bit of confusion regarding user authentication with GraphQL and Relay. I’m sure we’ll hear more details from Facebook about this in the future, but in the meantime, I’ll explain our approach:</p><p>Cookies!</p><p>Our login flow is separate from our SPA. If the user isn’t authenticated from the Rails end, they are redirected as appropriate. Once the SPA is rendered, they must be logged in, and therefore all AJAX calls going across the wire already have all of the necessary cookies. We pull out the cookie on the other end to authenticate the user.</p><p>There are no other changes to the server necessary for verification, beyond what you would already be doing in a model or controller. We’re using <a href="https://github.com/rmosolgo/graphql-ruby">graphql-ruby</a>, so our change_track mutation might look something like this:</p><pre>field.resolve -&gt; (obj, args, ctx) do<br>  if ctx[:user].has_access_to?(args[&#39;track&#39;])<br>    ctx[:user].change_track(args[&#39;track&#39;])<br>  end<br>end</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77098078b54d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>