<?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 Vlad on Medium]]></title>
        <description><![CDATA[Stories by Vlad on Medium]]></description>
        <link>https://medium.com/@suciuvlad?source=rss-e5ccdcb5ed10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Vlad on Medium</title>
            <link>https://medium.com/@suciuvlad?source=rss-e5ccdcb5ed10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:29:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suciuvlad/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[GraphQL Remote Schema Stitching in a Multi-Service Architecture]]></title>
            <link>https://medium.com/@suciuvlad/graphql-remote-schema-stitching-in-a-multi-service-architecture-ac329037f082?source=rss-e5ccdcb5ed10------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac329037f082</guid>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[apollo-client]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Vlad]]></dc:creator>
            <pubDate>Mon, 16 Jul 2018 11:50:50 GMT</pubDate>
            <atom:updated>2019-04-21T06:24:21.855Z</atom:updated>
            <content:encoded><![CDATA[<h4>A look at how to merge remote GraphQL schemas and speed up responses by leveraging through Apollo Engine.</h4><p>Micro-services have been gaining popularity and are now becoming the status-quo of building and designing web applications. Many of the common patterns of multi-service architectures involve the use of numerous APIs where each has their own purpose and are eventually used as the building block for the frontend application.</p><h3>GraphQL over REST</h3><p>GraphQL is a query language for your API that gives you the power to ask exactly for the data you need and nothing more, making it easier to evolve your API over time. You also get a complete documentation of your endpoints — making GraphQL completely badass!</p><p>One common misconception is that in order to use GraphQL one must completely rethink the existing infrastructure from scratch when in fact is as easy as adding a very light wrapper around your existing API, enabling you to get started right away.</p><h3>Remote schema stitching</h3><p>As your product evolves, you often end up having multiple schemas. Perhaps some are local, while others are different database layers like CosmicJS, which comes with its own schema.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gkbdsBTpwDJmuq0jNLr_TA.jpeg" /></figure><h4>Introspecting GraphQL APIs</h4><p>One useful property of GraphQL is that it supports introspection. This means that you can ask GraphQL schema informations about what queries it supports by sending an introspection query.</p><p>We can use that to create a new GraphQLSchema instance with a schema definition that is identical to the existing one. To achieve this purpose we make use of a set of utilities called <strong>graphql-tools </strong>that allows us to use remote GraphQL endpoints as if they were local schemas.</p><h3>Getting Started</h3><p>To demonstrate the ease and flexibility of GraphQL and for the purpose of this tutorial, I’ve created a <a href="https://github.com/suciuvlad/graphql-microservices-example">working project on GitHub</a> which is composed of two services.</p><h4>1. Movie Service</h4><p>This is pretty straightforward: A simple service to list all the movies or find a movie by id. Nothing fancy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05a69d8874b6154d2f1adf1d918f547e/href">https://medium.com/media/05a69d8874b6154d2f1adf1d918f547e/href</a></iframe><p><a href="https://launchpad.graphql.com/w5vwzkn9zz">Run this example on the Apollo Launchpad</a></p><h4>2. Main Service</h4><p>This is where the magic happens. The main service combines multiple GraphQL APIs allowing us to get all the data we need in one request through schema stitching. In order to do that we need to create a GraphQL schema object for each API. This is done in three steps:</p><ol><li><em>We use</em><strong><em> Apollo Link</em></strong> to request and fetch the GraphQL results.</li><li><strong><em>introspectSchema</em></strong> to get the schema from the remote endpoint and pass it to makeRemoteExecutableSchema</li><li><strong><em>makeRemoteExecutableSchema:</em></strong> Creates a local proxy for the schema that knows how to call the remote API. Which basically returns the schema definition + the resolvers.</li></ol><blockquote>makeRemoteExecutableSchema receives two arguments: a schema definition that was obtained via an introspection query and a link that is connected to the existing remote GraphQL to be proxied. The former is actually forwarding the queries and the mutations to the underlying GraphQL API.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db280efb0d630274d83c4baa4b987043/href">https://medium.com/media/db280efb0d630274d83c4baa4b987043/href</a></iframe><p><a href="https://launchpad.graphql.com/9jrk80m33r">Launch this example on the Apollo Launchpad</a></p><h3>Running the Application</h3><p>To facilitate a faster bootstrapping of the application we use Lerna and Yarn workspaces. This makes it easier to manage multiple packages. Before we run the application we need to run the <strong>bootrap</strong> command at the root of the repository, which will install all the dependencies and connect dependent projects with symlinks. Lastly, we use Lerna to start the services.</p><pre>git clone git@github.com:suciuvlad/graphql-microservices-example.git<br>cd graphql-microservices-example<br>git checkout remote-schema-stitching<br>npm install --global lerna<br>lerna bootstrap<br>lerna run start:dev --parallel --concurrent</pre><p>By now we’re up and running and the main GraphQL API can be accessed at: <a href="http://localhost:3000/graphiql">http://localhost:4000/graphiql</a> or online on the <a href="https://launchpad.graphql.com/9jrk80m33r">Apollo Launchpad</a>.</p><h3>API Caching</h3><p>API caching is a common pattern to reduce the load on your servers and get faster response times. When it comes to GraphQL there are couple of pain points the community seems to complain of:</p><ul><li>GraphQL requests are sent via POST requests making it rather difficult to cache.</li><li>As the application grows in complexity the GraphQL size of the individual queries is getting larger as well.</li><li>It’s very easy to fall into the trap of the classic N +1 issue.</li></ul><blockquote>There are many fantastic resources on how to fix the N+1 issue and for that reason, we’re not going to go into the details. The most popular solution involves using Facebook’s DataLoader.</blockquote><h3>Improve GraphQL Performance with Apollo Engine &amp; Response Caching</h3><p>Running a couple of queries reveals the fact that our response times are rather huge: ~<strong><em>650 ms</em></strong> for requests made to the 3rd party GraphQL schema provided by CosmicJS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w0vIpbj-_7Y7j4tTH6obLg.png" /></figure><p>To fix this we’re going to setup Apollo Engine that comes out of the box with a bunch of cool features like <strong>automatic persisted queries</strong>, <strong>query batching</strong>, <strong>performance tracing, error tracking </strong>and lastly it also acts as a CDN. Let’s get started:</p><ol><li>We need to add Apollo Engine to our list of packages.</li><li>Then enable Tracing and Cache Control in the Apollo Server.</li><li>Next, we have to <a href="http://engine.apollographql.com/">login into Apollo Engine</a> and generate an API key that’s eventually going to be passed to the Engine constructor.</li></ol><p>git checkout remote-schema-stitching-cache</p><p>By now your code should look something in the lines of:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6d60746c81cc48eb61ddb161a817a75/href">https://medium.com/media/b6d60746c81cc48eb61ddb161a817a75/href</a></iframe><h4>Specifying cache length</h4><p>It’s imperative to understand that while we did specify a defaultMaxAge cache, this can be overridden by using cache hints inside the schema definitions. However, there’s a gotcha:</p><blockquote>Schema stitching doesn’t yet support the ability to pass through cache control extensions from the remote server.</blockquote><p>What this means is that we need to add cache hints at the stitching server layer instead, through resolvers and by making use of a handy new method called <strong>delegateToSchema </strong>that allows forwarding parts of the queries or even new queries to schemas.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3636549515462de7f51ef4627ad0e377/href">https://medium.com/media/3636549515462de7f51ef4627ad0e377/href</a></iframe><blockquote>Engine proxy will strip out both the tracing and caching extensions out of responses by default so we passed some additional options to the constructor in order to verify if the new resolvers are working as expected.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xq4H5hlO1kl-S9YbrUdgbQ.png" /></figure><p>By now everything is setup and the requests are going to be cached by Apollo Engine according to the caching rules and you’ll have access to your performance metrics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pngByYxey7qoeVxO6WLwKQ.png" /></figure><h3>Summary</h3><p>In this article, we learned how to merge multiple schemas together using <strong>makeRemoteExecutableSchema</strong> from <strong>graphql-tools</strong>. Next we explored how easy it easy to add Apollo Engine to our application in order to get faster response times and use resolvers to override the default cache control by delegating schemas.</p><p><em>Did you find this useful? Subscribe to our newsletter to i</em>ncrease your knowledge and expertise as a web developer today.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff03f89%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Ff03f89%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252Ff03f89%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/cae66749b8354aa178abf9f4ae827fae/href">https://medium.com/media/cae66749b8354aa178abf9f4ae827fae/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac329037f082" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>