<?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: Patrick Arminio</title>
    <description>The latest articles on DEV Community by Patrick Arminio (@patrick91).</description>
    <link>https://dev.to/patrick91</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%2F13546%2F5e8ac08e-bc41-4df8-af30-496547900e61.jpg</url>
      <title>DEV Community: Patrick Arminio</title>
      <link>https://dev.to/patrick91</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patrick91"/>
    <language>en</language>
    <item>
      <title>Using Apollo Federation with local schemas</title>
      <dc:creator>Patrick Arminio</dc:creator>
      <pubDate>Fri, 28 Aug 2020 11:10:54 +0000</pubDate>
      <link>https://dev.to/patrick91/using-apollo-federation-with-local-schemas-1ppc</link>
      <guid>https://dev.to/patrick91/using-apollo-federation-with-local-schemas-1ppc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Originally posted on &lt;a href="https://patrick.wtf/posts/apollo-federation-local-services"&gt;https://patrick.wtf/posts/apollo-federation-local-services&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Recently I need to create a proxy to a third party API in our GraphQL Federation schema but didn’t want to spin up a new service just for this. This blog posts explains how to use a local schemas inside the Apollo Federation Gateway.&lt;/p&gt;

&lt;h1&gt;
  
  
  A bit of context
&lt;/h1&gt;

&lt;p&gt;At &lt;a href="https://team.pollen.co"&gt;work&lt;/a&gt; we use Prismic to power the content of our site (at least most of it). We are currently working on a new project that is using Apollo Federation as our Gateway&lt;sup id="fnref1"&gt;1&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;We wanted to add the Prismic API to our Gateway, so to use only one API Jon our frontend. Unfortunately when a GraphQL API doesn’t provide support for Apollo Federation things aren’t as easy as specifying the service in Apollo’s services list.&lt;/p&gt;

&lt;h1&gt;
  
  
  A basic Federation example
&lt;/h1&gt;

&lt;p&gt;Let’s supposed that we have one Apollo federation that’s consuming one service for the time being.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gateway&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ApolloGateway&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;serviceList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;products&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:4002&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;gateway&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see the ApolloGateway requires a list of GraphQL services as objects with a name and a url. This is a bit unfortunate as this means that using a local schema doesn’t work by default.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; we can’t use Prismic’s GraphQL API directly as they don’t provide the necessary fields for federation. In addition to that they also use GET requests instead of POST requests for the API call, and ApolloGateway always sends POST requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; we won’t be using Prismic API in the examples of this blog post, as I wanted to keep the code simpler sine Prismic API needs a bit of work to fetch the &lt;code&gt;ref&lt;/code&gt; version, which is not really useful for this example. I’ll be happy to provide an example repo with Prismic if it’s needed. Drop me a &lt;a href="(https://countries.trevorblades.com/"&gt;tweet&lt;/a&gt; in that case!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  What can we do to make this work?
&lt;/h1&gt;

&lt;p&gt;So, what makes Federation work with multiple service is the Apollo Gateway, which basically acts as glue between the services. It knows how to compose the schemas and how to create a query plan for executing a query.&lt;/p&gt;

&lt;p&gt;From my understanding of the source code, Apollo Gateway seems to support local services, but, as mentioned in this &lt;a href="https://github.com/apollographql/apollo-server/issues/3250"&gt;GitHub Issue&lt;/a&gt; it doesn’t allow to support both local and remote services at the same time by default.&lt;/p&gt;

&lt;p&gt;Fortunately for us, we can use &lt;code&gt;buildService&lt;/code&gt; to provide create a custom DataSource for our remote schema.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;DataSource is what Apollo Gateway uses to fetch data from a service. Which means we can create a DataSource that proxies the requests to Prismic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will be using &lt;a href="https://countries.trevorblades.com/"&gt;Trevor Blades’ Countries API&lt;/a&gt;, a public GraphQL API that doesn’t support Apollo Federation.&lt;/p&gt;

&lt;p&gt;So, &lt;code&gt;buildService&lt;/code&gt; is called for each service specified in the &lt;code&gt;serviceList&lt;/code&gt; configuration parameter for Apollo Gateway. Let’s go and update that to have a service for the countries service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gateway&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ApolloGateway&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;serviceList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;products&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:4002&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;countries&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://countries&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;gateway&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The only caveat that we have is that we have need to provide a &lt;code&gt;url&lt;/code&gt; for our service, even it is fake.&lt;/p&gt;

&lt;p&gt;The next step is to provide a &lt;code&gt;buildService&lt;/code&gt; function, which looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gateway&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ApolloGateway&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;serviceList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;products&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:4002&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;countries&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://countries&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;buildService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://countries&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;LocalGraphQLDataSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getCountriesSchema&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;RemoteGraphQLDataSource&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;gateway&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ok, the last step is to create the function that will return the schema for the countries service. Similar to &lt;code&gt;RemoteGraphQLDataSource&lt;/code&gt; Apollo Gateway also provides a &lt;code&gt;LocalGraphQLDataSource&lt;/code&gt; which accepts a &lt;code&gt;schema&lt;/code&gt; as parameter instead of a &lt;code&gt;url&lt;/code&gt;. This means that we can provide our own schema!&lt;/p&gt;

&lt;h1&gt;
  
  
  GraphQL Tools
&lt;/h1&gt;

&lt;p&gt;So now we know we can provide our own schema to Apollo Gateway, but how can we proxy an existing schema? We could do this manually, but it would take a bit of time, luckily for us there’s a package called &lt;a href="https://www.graphql-tools.com"&gt;&lt;code&gt;graphql-tools&lt;/code&gt;&lt;/a&gt; that allows to use a &lt;a href="https://www.graphql-tools.com/docs/remote-schemas"&gt;remote schema&lt;/a&gt; as if it were local.&lt;/p&gt;

&lt;p&gt;In addition to that we also need to use &lt;a href="https://github.com/0xR/graphql-transform-federation"&gt;&lt;code&gt;graphql-transform-federation&lt;/code&gt;&lt;/a&gt; to provide the required fields for Apollo Federation. Let’s see how our function looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;executor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;variables&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://countries.trevorblades.com/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;variables&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fetchResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getCountriesSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;wrapSchema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;introspectSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;transformSchemaFederation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&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;p&gt;There’s a few things going on there, first we have a function called &lt;code&gt;executor&lt;/code&gt;, this is our core function that proxies the calls to the countries service. This is not doing anything special, it is getting the document and variables and sending them to the countries services. For Prismic we’d add headers for authentication and change the request to be a &lt;code&gt;GET&lt;/code&gt;. But the rest would be pretty much the same.&lt;/p&gt;

&lt;p&gt;Finally, the last piece of the puzzle, &lt;code&gt;getCountriesSchema&lt;/code&gt;, which uses the utilities from &lt;code&gt;graphql-tools&lt;/code&gt; and &lt;code&gt;graphql-transform-federation&lt;/code&gt; to create a proxy schema with support for Federation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getPrismicSchema&lt;/code&gt; uses &lt;code&gt;wrapSchema&lt;/code&gt; to create a schema based on our the Countries GraphQL API. We need to pass a schema, an executor and some optional transforms. We are not use any transform here, but one use case is to rename the types so they don’t conflict with other services.&lt;/p&gt;

&lt;p&gt;Finally it calls &lt;code&gt;transformSchemaFederation&lt;/code&gt; to obtain a Federation compatible schema, the only option we are passing here is telling Federation that this schema is extending the &lt;code&gt;Query&lt;/code&gt; type, so all the fields provided here will be composed into the federated schema. We can also pass more options here, but at the moment we didn’t need that.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; the code in the example is a bit broken as we are not awaiting &lt;code&gt;getCountriesSchema&lt;/code&gt; in our &lt;code&gt;buildServices&lt;/code&gt;, the final code provided in the &lt;a href="https://github.com/patrick91/apollo-federation-local-services/blob/main/server.js#L11"&gt;git repo has the correct solution&lt;/a&gt;, I wanted to keep the code example simple here.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;As you can see you need a little bit of code to proxy a non federated GraphQL service with Apollo Gateway, but it is not too bad and I think it is pretty cool that we can do this as we can move the code that deals with multiple schemas from our clients to our gateway, simplifying how they fetch data.&lt;/p&gt;

&lt;p&gt;Also &lt;code&gt;graphql-transform-federation&lt;/code&gt; allows to specify a Federation configuration so it is even possible to use Apollo Federation features to extend types with a non federated service, which can definitely make the schema easier to use.&lt;/p&gt;

&lt;p&gt;To see a fully working example of this head to the &lt;a href="https://github.com/patrick91/apollo-federation-local-services"&gt;example repo I’ve written for this blog post&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; all of this post might be deprecated in future when &lt;a href="https://youtu.be/MvHzOwdLb_o"&gt;project constellation&lt;/a&gt; gets released to the public, which should allow federation of service that don’t have the (currently) required fields. In the meantime, feel free to try this approach.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;This is part of a bigger rewrite of our internal API, more on that in future blog posts hopefully. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>graphql</category>
      <category>apollo</category>
      <category>node</category>
      <category>api</category>
    </item>
  </channel>
</rss>
