<?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 Yonathan Amir on Medium]]></title>
        <description><![CDATA[Stories by Yonathan Amir on Medium]]></description>
        <link>https://medium.com/@yonathanamir?source=rss-56bbe93c09f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*uyh9LwgCrdXdj8PZ.</url>
            <title>Stories by Yonathan Amir on Medium</title>
            <link>https://medium.com/@yonathanamir?source=rss-56bbe93c09f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 01:55:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yonathanamir/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[Overcoming the Monolith with GraphQL]]></title>
            <link>https://medium.com/@yonathanamir/overcoming-the-monolith-with-graphql-487ac3ecc5d4?source=rss-56bbe93c09f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/487ac3ecc5d4</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Yonathan Amir]]></dc:creator>
            <pubDate>Sat, 23 Dec 2017 16:19:07 GMT</pubDate>
            <atom:updated>2018-01-17T14:43:54.328Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*RHQ7lpGDV_M3yWRa9DiR2g.png" /></figure><p>We’ve been using <a href="http://graphql.org/">GraphQL</a> in production for a few months now, and have been very delighted with it. Our products, their relations with each other and their architecture behind the scenes aren’t so simple, and our GraphQL server had to account for various constraints and requirements. I feel like our usecase isn’t a typical one, and thought I would share our GraphQL architecture for the world to see — and improve.</p><p>I won’t go into the basics of GraphQL here, so if you’re unfamiliar with it, I invite you to check out <a href="https://medium.freecodecamp.org/so-whats-this-graphql-thing-i-keep-hearing-about-baf4d36c20cf">this great </a>introduction to the technology.</p><h3>Some Product Background</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RqUiYSZhBO38bQi96793XA.jpeg" /></figure><p>I work in an enterprise providing personally-tailored analytic services. To keep it short, we have a bunch of analysts spread into teams, and each team given a specific client. In addition, we have quite a lot of data curators, who manually sift through our client’s raw data and prepare it for our analysts.</p><p>Our business becomes convoluted when you introduce the fact that our clients insist their data remain confidential — that means each analyst is allowed to work on a very specific subset of data and documents. In addition, some clients require their specific analysts to cooperate on specific projects, which means that client A’s analyst may be allowed to work on client B’s data with client B’s analyst, but not the other way around. This system also extends to our data curators.</p><p>When you factor in hundreds of clients with thousands of analysts and data curators, things get messy. We have an inside classification system for this exact issue, which defines which user is allowed to see documents and reports from which source. It’s called M-Class, and we’ll get to it a bit later.</p><h3>Where does GraphQL fit in?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*86pn3KGI8Zk3diPI257x1g.gif" /><figcaption>Source: www.dilbert.com</figcaption></figure><p>A few years back a new app was created for our analysts called Bone. Our company was just shifting from desktop to web, and without any experience, a small team created a monolithic PHP/MySql application that quickly transformed into some of the ugliest code you can imagine. However, for our users, the site offered a multitude of new features, and a lot of analysts loved it.</p><p>As the years passed, Bone was expanded and the monolith grew, until 3 years ago when we started to break it down into microservices. We already had a few in place (Bone needed to integrate with a few legacy systems written in .NET, so we had adapter microservices between the PHP monolith and those .NET Web Services), and Node was chosen for most of the new services.</p><p>Meanwhile, Bone wasn’t good enough for the data curators, who kept using their old desktop applications. 2 years ago we created an alternative to our curators’ desktop apps called Mountain. It kind of looks like a specialized Jira/Trello/Wekan, but integrated closely with our analyst’s site, Bone. Behind the scenes, Mountain was communicating with Bone through a variety of API calls, sometimes using the PHP server as an API gateway to various microservices.</p><p>Enter Lampoon, a new application which we started developing at the end of last year. Lampoon was designed to give both our curators and analysts a common app to view various reports. You could do the same in Bone, but the UX was terrible and instead of rebuilding it on top of Bone’s mess of a codebase, we decided to once again create a “satellite application”, using API calls to integrate our new app with Bone’s data.</p><p>However, Lampoon had very different requirements from Bone’s infrastructure. Lampoon offered a smart way to view lots of reports, so it had to do a lot of “selects” against Bone and because it’s mostly a reading application, the amount of “inserts” is practically none. If at around 7k Bone users and 2k Mountain users we saw around 3.5k selects/s on our DB, with only 50 Lampoon users that number jumped to 4.5k — and we were planning on getting at least half of Bone’s users to start working with Lampoon. After a successful pilot with 5 users, we ran some stress tests and realized that expanding the system wouldn’t work as things stood — API calls were taking too long to return, and adding too many users made Bone crawl to a halt.</p><p>While Bone’s database had no problem handling these queries (it’s not that big, &lt;100Gb), it was our old API calls that failed us. Bone is built around an ancient ORM wrapping the DB, and over the years it became incredibly inefficient at handling requests with multiple items. Not only that, but as development on Bone continued, the basic Item was expanded, so not only was the ORM not built to deal with multiple items, the items it was built to handle became a lot more complex.</p><p>In order for the data to be consistent across all 3 systems, neither Mountain nor Lampoon save any data regarding Bone’s items. This means that interacting with an item through any system carries the changes to the others, but burdens Bone with being a single point of failure and an obvious bottleneck.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*MTC-ingrY2ZVFzB3oCBnXQ.gif" /></figure><p>At this point we realized that Bone was becoming more of an infrastructure than a standalone application, and decided to divert resources into improving connectivity with said infrastructure. Bone had to perform better in order to enable the satellite applications and as we saw it, we had 2 options:</p><ol><li>Take a deep breath, dive into some horrific PHP code and fix Bone’s ancient ORM or,</li><li>Create a new ORM layer, querying Bone’s DB directly.</li></ol><p>We chose #2, and we decided to try our hand at using GraphQL for the project, with the main argument being that we foresaw lots of development both on Mountain and Lampoon, and we wanted Bone’s infrastructure to be as flexible as possible.</p><h3>Our Schemas (and Loaders)</h3><p><em>Before I start, I’ll just mention that we’re using </em><a href="https://github.com/graphql/graphql-js"><em>graphql-js</em></a><em> and </em><a href="https://github.com/graphql/express-graphql"><em>express-graphql</em></a><em> as far as packages go.</em></p><p>Like every GraphQL server, we started by describing our schemas. Bone and Mountain are both based around a basic Item entity, which has multiple Versions, attached Files, Metadata and Comments. In addition, each item version, file or comment has an owner, which is its creating User.</p><p>So, our schemas will be Item, Version, File, Metadata, Comment and User. I’ve also included each entity’s various fields:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*55WkQRMXKGHjHiwMVxsZtQ.png" /><figcaption><em>Our schema model. Capitalized fields are of the connected type.</em></figcaption></figure><p>As for actually fetching the data, we use <a href="https://github.com/facebook/dataloader">Facebook’s DataLoader</a> to handle batching (it’s great, check it out). All of the entities are stored in our main MySql DB, so the loaders simply build suitable SQL queries, JOINing all of the relevant tables, before batching the queried IDs into a nice IN clause.</p><p>So we started playing around with GraphQL, and for a start we exposed a simple Schema that accepts Item IDs and fetches them, along with all of their entities, straight from the DB. Running it through our load simulations showed a lot of promise — Lampoon requests that would take up to 15 seconds to complete against the API were fetched in around 300ms! It looked like we were heading on the right track, but we still had some work to do before before deploying to production.</p><h3>Filters</h3><p>As things stood right now, our server was pretty standard. Here’s what it looked like, with each component being its own GraphQLObjectType:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*XzZfTw5m0JOPHofTsi3d0g.png" /></figure><p>But we weren’t finished just yet. Remember our M-Class system, the one that keeps each user from seeing unauthorized documents and reports? Bone had it integrated into its ORM, so an API call for a specific user would only fetch the Items that said user was allowed to view. Files also had to be included into M-Class, as some users would be authorized to view a report but not its attached files. When writing our GraphQL server, we had to somehow filter out those unwanted entities.</p><p>In order to determine whether a user is allowed to view an entity, we have to fetch that user’s Approved M-Class values, called “areas”, and compare them to the entity’s area. If the entity’s area is included in the user’s approved areas, the user is authorized to view the entity.</p><p>Ideally, when resolving the GraphQL query against the DB, you’d simply add a WHERE clause comparing each item’s area to the user’s authorized areas. Working with DataLoader and batches can make this not as trivial, but you’d simply need to provide a context to the loader. This can be done by either a singleton utility class that can be accessed by the loader, or you can fetch the user’s areas when creating the DataLoader (you should create a new DataLoader for every request).</p><p>Our problem? The entity’s area is stored in a different DB and microservice, and the user’s approved areas are in yet another service (there are other apps in our company that use M-Class). Because we were already planning on fetching the user areas separately before processing and injecting it into a singleton context before executing the GraphQL query, having the data somewhere else wasn’t a problem. However, seeing as the entity’s areas weren’t stored in our own DB, we couldn’t add that nice WHERE clause to our SQL query.</p><p>To get around this issue we decided on separating the filter logic (WHERE clause) from the fetching logic (SELECT clause) — given a list of IDs, we’d first run them through a filter (called M-Class Filter, obviously), which would kick out any unauthorized entities. Given our new, completely authorized list, we’d send it to our previous GraphQL resolver and violla! Our client has a fresh list of approved, data filled Items to populate Lampoon with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*amt56-xU4RWJh_UPu4Kntw.png" /><figcaption>Our GraphQL Query resolution flow, now with the M-Class Filter.</figcaption></figure><h3>Finders</h3><p>As things stood, we were ready to go — and we did. Bone’s GraphQL server was deployed and with it Lampoon, and the results were incredible. The system was blazing fast, our users happy with the new UX, and our infrastructure stood firm. It was now time to look ahead into the future.</p><p>At its core, Lampoon creates a personalized Item feed, split into categories our users define. Lampoon works by listening to Bone’s Pub/Sub, subscribing to a specific event (which indicates an Item or report is ready, and should be viewed by other users). When that event is published, Lampoon runs the relevant item through its own labeling engine, calculates which users should see it in their own feeds, and saves that indication in Lampoon’s own DB.</p><p>When we started work on Lampoon we already anticipated that our users would ask to view specific feeds, built according to various conditions, in addition to their personalized one. For example, some users would like to view all items with specific Metadata. GraphQL easily supports this with different queries, built around the same GraphQL components.</p><p>Our Item Schema Resolver can fetch Items given a collections of IDs, and our basic query was a simple pipe — it recieved those same IDs as an argument, and passed them (through the M-Class Filter) to the resolver. In order to build our new Metadata query using the same components as before, we simply needed to fetch a list of IDs matching the metadata condition. To accomplish this we created another bulding block in our architecture — Finders. Their role was to construct a list of IDs given a specific condition from a query, and pass it along to the Item resolver. This is how our ByMetadata query flow looked like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q3I_N18Gz9E2c-S0U7HgXg.png" /></figure><p>Another usecase is that our users wanted all Items created within a certain date range. Just like with the Metadata Finder, we can easily create a Date Finder and plug it as a new GraphQL Query straight into the M-Class Filter and Item Resolver:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-vlpMNOrK4pquBXdhGa-JA.png" /></figure><p>Creating new queries, and with them new feeds, is as easy as defining a new Finder, and because the finding mechanism is completely separate from both the filtering and fetching components, we can even find items according to data which is completely external to Lampoon or Bone (for example — all items which had gone through a specific process in Mountain).</p><h3>M-Class and Files — Combining Filters and Finders</h3><p>As mentioned earlier, M-Class involves both items and their attached files — a user may be authorized to view an item, but not its attached report. When we started writing our GraphQL service this posed a problem as all M-Class logic was already finished, but after we created our Filter architecture, we were able to reuse the same component for different entities, in different parts of our codebase.</p><p>In order to plug the M-Class filter before fetching the files, we first need to get all files attached to an item — or, in other words, create a “File by Item Finder”, fetch the relevant file IDs, pass them through the filter and only then resolve them. Both components slid in quite nicely, and we were quite satisfied with the results. Finally, a complete query follows the following diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BcqySa5CfShyR6FrUg2wIQ.png" /></figure><h3>Final Words</h3><p>While we are still figuring out how to get the most out of GraphQL, our experience so far has been extremely positive. From the ease of development to the performance it offered us, we are keen to keep improving and adding functionality to it. We still have some dilemmas ahead (can we “concatenate” Finders? How generic do I want them to be? Will seperating the finding, filtering and fetching requests come back to bite us in the future?), but so far we feel comfortable moving forward with our current architecture.</p><p><em>If you have any comments, insights, questions or suggestions I would love to hear from you.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=487ac3ecc5d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Game Overs and Video Game Narratives]]></title>
            <link>https://medium.com/@yonathanamir/game-overs-and-video-game-narratives-621bc43d98fe?source=rss-56bbe93c09f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/621bc43d98fe</guid>
            <category><![CDATA[storytelling]]></category>
            <category><![CDATA[game-design]]></category>
            <category><![CDATA[videogames]]></category>
            <category><![CDATA[narrative]]></category>
            <dc:creator><![CDATA[Yonathan Amir]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 22:08:37 GMT</pubDate>
            <atom:updated>2017-09-12T12:32:00.502Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*FnambYzx_CiiP00y4tHcQw.gif" /></figure><blockquote>Fail-states are a part of gameplay, and create a conflict between the game and any meaningful narrative.</blockquote><p>Narratives have to be crafted meticulously in order to be significant. Impacting others is tough, so artists utilize every tool their medium possess in order to make you feel <em>something</em>. Frame compositions, sound mixing and literary allegories all serve the same purpose — getting or advancing a sincere emotional response from the consumer.</p><p>Unlike books, films or other narrative-driven works, video games have to account for user input, and, as a result, user failure. Stories, on the other hand, depend on characters to fulfill their specific arcs successfully and serve their narrative role. Fail-states pose a problem to the storyteller, as characters can die prematurely and leave a gaping hole in the game’s story (or stop it altogether).</p><p>Most games simply ignore the issue — a game over screen drop you back at your last checkpoint, ready to try again. The drawback here is that without any consequences, the narrative falls flat and ends up meaningless. Just like the characters in a movie, the player has to risk something in order to be truly involved.</p><h3>Shadow of Mordor’s Approach</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*QtohpuGYESxGZcA7-FgDYA.gif" /></figure><blockquote><em>The gameplay is propelled by fail-states, which are justified in the narrative.</em></blockquote><p>2014’s <a href="https://en.wikipedia.org/wiki/Middle-earth:_Shadow_of_Mordor">Shadow of Mordor</a> surprised everyone with its approach to death. As a combat-centered action adventure, it’s expected for the player to die multiple times. It has become a genre staple that such deaths are greeted by a generic “game over” screen, with the player resuming the action from the last checkpoint — with no consequence whatsoever(or a small one, like losing some items or currency). The main deterrent for not dying is the denial of progress.</p><p>To set itself apart, Shadow of Mordor introduced the Nemesis system — if an enemy killed you in combat, he would get promoted, become stronger, and pose a bigger challenge the time you’ll face him. The deterrent here is simple — dying means you’ll have to work harder in the future.</p><p>What I really like about the Nemesis system is that in addition to supplying an incentive to staying alive, it’s also supported by the narrative. Talion, the player character, is immortal, and reincarnated every time he falls in battle. The promoted enemies also remember killing Talion, and will mention it in following encounters .</p><h3>What Wario Land 3 Did</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*_QdCxnrcJdocSBBxHbwEgA.gif" /></figure><blockquote>There are no fail-states, and thus no conflict between the gameplay and the narrative.</blockquote><p><a href="https://en.wikipedia.org/wiki/Wario_Land_3">Wario Land 3</a> (and 2, see note below) is very particular in the platforming scene — the player is immortal. With platformers being so focused on avoiding environmental hazards until the end of the stage, such a change is almost genre-breaking. However, simply taking out fail-states from the gameplay poses a simple (yet effective) solution to the conflict with the narrative.</p><p>Instead of dying, when Wario is hit by an enemy, he is physically altered. For example, touching a fiery enemy causes Wario to ignite and begin running around uncontrollably, messing up jumps. However, many areas and puzzles in the game are solved by the same status effects that constitute Wario Land 3’s fail-states. In the fire example, after running around for a while, Wario is completely engulfed in flames and is able to move around (although slowly and without jumping) and break special blocks (as shown in the gif above). Almost all of the different fail-states the game impede the player’s progression in one part, but become a solution in another.</p><p><em>Note — this mechanic was actually introduced in Wario Land 2, but I chose to focus on 3 because that was the one I played.</em></p><h3>Braid’s Solution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*w56JEYWrsMSAJtTFXDJNaA.gif" /></figure><blockquote>The gameplay, and with it the fail-states, are a part of the narrative.</blockquote><p><a href="https://en.wikipedia.org/wiki/Braid_(video_game)">Braid</a>, another puzzle-platformer, has my favorite solution. The game’s core mechanic is the ability to reverse time, so when Tim (the main character) is hit by an enemy or mistimes a jump, the player will simply rewind a bit and attempt it again. As the game progresses, the time manipulation gets a bit more complex, but the same idea remains — if you reach a fail-state, you can go back and try again.</p><p>The beauty in Braid’s solution, and what differentiates it from game overs and checkpoints, is how the gameplay mechanic, and fail-states, tie into the overall narrative. Braid tells a story of regret and the desire to undo mistakes (and the Atom bomb, if you buy into that theory), just like the time-rewinding mechanics allows the player to retry any fail-state they approach. Braid interweaves its plot and deeper meanings with the game itself, and the result is a wonderful combination of narrative and gameplay.</p><h3>Final Words</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*SoadnV6opwe_gFQW03gVaQ.gif" /></figure><p>It’s completely fine for video games to have player deaths and game overs. I’ve enjoyed every bit of <a href="https://en.wikipedia.org/wiki/The_Legend_of_Zelda:_Breath_of_the_Wild">Breath of the Wild</a> I’ve played, even if I was returned to one of the many saves I had each time I died, and I’ll still love the next <a href="https://en.wikipedia.org/wiki/Fallout_(series)">Fallout</a> game, even if it has a big focus on narrative along with player death.</p><p>That being said, by directly involving the consumer, video games offer unique narrative opportunities that no other medium can match. There are plenty of ways to tackle the conflict between fail-states and narrative, and I feel that those games who do take the extra effort just end up being a notch better.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=621bc43d98fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>