<?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 Miles Bardon on Medium]]></title>
        <description><![CDATA[Stories by Miles Bardon on Medium]]></description>
        <link>https://medium.com/@milesbardon?source=rss-bfd0e208ec29------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-k4hVi0vJR4D60G0opJiQg.jpeg</url>
            <title>Stories by Miles Bardon on Medium</title>
            <link>https://medium.com/@milesbardon?source=rss-bfd0e208ec29------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:59:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@milesbardon/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[Migrating from Apollo Server v2 to v3 — Part 2: Server Upgrade]]></title>
            <link>https://medium.com/lego-engineering/migrating-from-apollo-server-v2-to-v3-part-2-server-upgrade-e9a9b7cafad5?source=rss-bfd0e208ec29------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9a9b7cafad5</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[graphql-apollo-server]]></category>
            <category><![CDATA[server-upgrade]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[apollo-server]]></category>
            <dc:creator><![CDATA[Miles Bardon]]></dc:creator>
            <pubDate>Mon, 04 Apr 2022 08:38:53 GMT</pubDate>
            <atom:updated>2022-04-04T08:38:53.972Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*n86BNfqFT1geN4d5" /><figcaption>Photo by <a href="https://unsplash.com/@wocintechchat?utm_source=medium&amp;utm_medium=referral">Christina @ wocintechchat.com</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Migrating from Apollo Server v2 to v3 — Part 2: Server Upgrade</h3><p><em>In the </em><a href="https://medium.com/lego-engineering/migrating-from-apollo-server-v2-to-v3-part-1-ea073462fab"><em>last section</em></a><em>, I wrote about how we upgraded our tooling in preparation for our full Apollo Server 3 upgrade. In the final article on this topic, I’ll talk about the steps we took to mitigate and resolve all the breaking changes in v3, as well as how we upgraded the live server in place.</em></p><p>Quite a few <a href="https://www.apollographql.com/docs/apollo-server/migration/">breaking changes</a> occurred with the upgrade to v3. Among these, our particular server was affected by the loss of extensions and the removal of the schemaDirectives option. In the next few sections, I’ll walk through how we resolved these breaking changes.</p><h3>Migrating Extensions to Plugins</h3><p>One of the key changes between v2 and v3 is the removal of support for Extensions, which in our case were used to extend the logging capabilities for all logs across the server. This includes removing debug logs in production and tracking request execution times.</p><p>The task required us to migrate our extension to the new Plugin pattern as described <a href="https://www.apollographql.com/docs/apollo-server/integrations/plugins/">in the documentation</a>. The majority of this work was converting the old <a href="https://www.npmjs.com/package/graphql-extensions">Extension</a> <em>class</em> to the Plugin <em>object</em>, with class methods of the same name in the Extension becoming fields in the Plugin object. For example, this Extension class</p><pre>export class LoggingExtension extends GraphQLExtension {<br>  constructor(logger: Logger) {<br>    super();<br>    this.logger = logger;<br>  }</pre><pre>requestDidStart(o: ExtensionPayload) {<br>    this.operationName = displayOperationName(o.operationName);<br>    this.validationSuccess = false;<br>    this.startWallTime = Date.now();<br>  }</pre><pre>validationDidStart() {<br>    return () =&gt; {<br>      this.endWallTime = Date.now();<br>    };<br>  }<br>}</pre><p>becomes this Plugin object</p><pre>export const createLoggingPlugin = (logger: Logger) =&gt; {<br>  let operationName = &quot;*&quot;;<br>  let validationSuccess = false;<br>  let startWallTime = Date.now();<br>  let endWallTime = 0;<br>  return {<br>    async requestDidStart(requestContext: GraphQLResponse) {<br>      operationName = displayOperationName(requestContext.operationName);<br>      validationSuccess = false;<br>      startWallTime = Date.now();<br>      return {<br>        async validationDidStart() {<br>          parsingSuccess = true;<br>          return () =&gt; {<br>            endWallTime = Date.now();<br>          };<br>        },<br>      };<br>    },<br>  };<br>};</pre><p>Of course, much of the plugin has been omitted for brevity, but the work to migrate this extension was fairly minimal, and gave us the opportunity to write more unit tests!</p><h3>Migrating SchemaDirectives out of Apollo</h3><p>With the schemaDirectives option completely removed, we needed another way to use our custom directives. One of these directives allows us to customise our authorization for requests that require it, with the most minimal effort by engineers, meaning it was imperative that it was brought along on our upgrade journey.</p><p>Whilst the Apollo migration documentation recommends applying the directives directly in the makeExecutableSchema, this <a href="https://www.apollographql.com/docs/apollo-server/migration/#schemadirectives">recommends using an older version</a> of GraphQL tools which we would like to avoid.</p><p>The recommended method for including schema directives in executable schemas for V8 of @graphql-tools/schema is to pass the generated schema to your implementation of these custom directives.</p><pre>let schema = makeExecutableSchema({<br>  typeDefs,<br>  resolvers,<br>});</pre><pre>const { authDirectiveTransformer } = authDirective(&quot;auth&quot;);</pre><pre>schema = authDirectiveTransformer(schema);</pre><p>Now, the schema object we had before has been <a href="https://javascript.info/currying-partials">curried</a> with the authDirective and can be passed into the Apollo Server configuration as normal.</p><p>Actually migrating the directive was a bit more challenging. Before the migration, schema directives were implemented as classes, provided by graphql-tools V4 (deprecated long ago!).</p><pre>class AuthDirective extends SchemaDirectiveVisitor</pre><p>These classes would have methods such as visitObject and wrapField that would be called at various stages in the schema’s lifecycle. In the latest version at the time of writing, the directive is implemented as an object that exposes a transformer function, as shown in <a href="https://www.graphql-tools.com/docs/schema-directives#implementing-schema-directives">the documentation</a>.</p><p>Conveniently, the documentation also lists an <a href="https://www.graphql-tools.com/docs/schema-directives#enforcing-access-permissions">example auth directive</a>, which we were able to use as a starting point for our converted directive. In this case, all we needed to do was replace the fieldConfig.resolve = function (source, args, context, info) {…} function with our own custom implementation, as well as the directive definition.</p><pre>export const authDirective = (directiveName: string) =&gt; {<br>  const typeDirectiveArgumentMaps = {};</pre><pre>return {<br>    authDirectiveTypeDefs: `directive @${directiveName}(<br>        skipOnFail: Boolean = false <br>        service: Service<br>      ) on OBJECT | FIELD_DEFINITION`,<br>    authDirectiveTransformer: (schema: GraphQLSchema) =&gt;<br>      mapSchema(schema, {<br>        [MapperKind.TYPE]: (type) =&gt; {<br>          ...<br>        },<br>        [MapperKind.OBJECT_FIELD]: (fieldConfig, _fieldName, typeName) =&gt; {<br>          ...<br>          }<br>        },<br>      }),<br>  };<br>};</pre><p>With the schema directives migrated, we were finally ready to make the <strong>Big Switch.</strong></p><h3>Migrating the server</h3><p>The final step in our long journey is to actually upgrade the server itself. We’ve brought all our dependant services and configurations up to the latest and greatest standard, so now all that stands in our way is upgrading the Apollo Server dependency to v3, and making sure it all works as normal.</p><p>Our own Apollo Server doesn’t use the vanilla implementation; we actually use apollo-server-express to get a better experience with our express server, which handles more than just GraphQL; it also includes a health check endpoint. The migration documentation references many of these <a href="https://www.apollographql.com/docs/apollo-server/migration/#changes-to-framework-integrations">framework integrations</a> that have been changed in the newest version.</p><p>Before, we could just define the server, add our express app as middleware, and start listening on our designated port. Now we have a few details to change and reorder.</p><p>1. The server has a .start() method that must be called <em>before</em> any middleware is applied, and directly <em>after</em> the server is created. This method used to be optional in v2, but in v3 it’s now <strong>mandatory</strong>.</p><p>2. The server starting routine is now asynchronous, meaning the entire setup function must be run in a Promise-like fashion. In our case, the server is started with the Node process, meaning we have a function along these lines;</p><pre>const startServer = async () =&gt; {<br>  const httpServer = http.createServer(app);</pre><pre>const server = new ApolloServer({<br>    schema,<br>  });</pre><pre>await server.start();</pre><pre>server.applyMiddleware({<br>    app,<br>    bodyParserConfig: true,<br>    path: &quot;/graphql&quot;,<br>  });</pre><pre>const PORT = process.env.PORT;</pre><pre>// This `listen` method launches a web-server<br>  const expressServer = httpServer.listen({ port: PORT }, () =&gt; {<br>    logger.info(<br>      `🚀 Server ready at <a href="http://localhost:${PORT}${server.graphqlPath}`">http://localhost:${PORT}${server.graphqlPath}`</a><br>    );<br>  });<br>};</pre><pre>startServer();</pre><p>And that’s it! With those steps, our server is ready to run and we can begin taking advantage of all the new features Apollo Server 3 has to offer, such as Federation support to spread responsibility for our schema around the codebase, and Apollo Studio Explorer replacing the old, bloated Playground.</p><h3>Conclusion</h3><p>The whole journey, from the investigation beginning, until the final merge of Apollo Server 3 took around 6 months to complete. During this time, numerous upgrades, refactors and other such updates kept us on our toes, continually testing our GraphQL server and putting our architecture through its paces.</p><p>Not only did we learn a lot of technical knowledge about running and managing a server of this size, but also how to effectively coordinate the whole team, of which there were many sub-teams, into testing the changes in a manner that prioritised completeness over speed.</p><p>Personally, I’m really happy with the results of this extraordinary undertaking and it has left us with a bright GraphQL future ahead.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9a9b7cafad5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lego-engineering/migrating-from-apollo-server-v2-to-v3-part-2-server-upgrade-e9a9b7cafad5">Migrating from Apollo Server v2 to v3 — Part 2: Server Upgrade</a> was originally published in <a href="https://medium.com/lego-engineering">Engineers @ The LEGO Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Combining GraphQL Schemas for Federated Subgraphs]]></title>
            <link>https://medium.com/better-programming/combining-graphql-schemas-for-federated-subgraphs-c50c5e8e9de0?source=rss-bfd0e208ec29------2</link>
            <guid isPermaLink="false">https://medium.com/p/c50c5e8e9de0</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Miles Bardon]]></dc:creator>
            <pubDate>Sun, 27 Mar 2022 09:42:07 GMT</pubDate>
            <atom:updated>2022-04-25T04:40:53.183Z</atom:updated>
            <content:encoded><![CDATA[<h4>Convert multiple schema files into one</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FBmwqiNujXjLydKA" /><figcaption>Photo by <a href="https://unsplash.com/@jeshoots?utm_source=medium&amp;utm_medium=referral">JESHOOTS.COM</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>TL;DR — </em><em>@graphql-tools/utils exports the </em><em>printSchemaWithDirectives function to convert your </em><em>GraphQLSchema object back to a schema string.</em></p><p>An interesting conundrum presented itself whilst planning to migrate a monolith GraphQL server to Apollo Federation. Our schema is split up into multiple files to ease the pain of having half a dozen teams work on the same schema, and we use the # import syntax to join them all together.</p><p>In this short blog, I’ll explain how we’re able to merge these multiple files into one, which can then be used to create a starting subgraph for a federated gateway.</p><h3>Schema files</h3><p>For the purposes of this article, I’ve created 4 files that will represent our schema distributed over multiple .graphql files.</p><pre>// schema.graphl</pre><pre># import Post from &quot;posts.graphql&quot;</pre><pre>type Query {<br>  posts: [Post]<br>}</pre><pre>// posts.graphql</pre><pre># import Comment from &#39;comments.graphql&#39;</pre><pre>&quot;&quot;&quot;<br>A post with comments<br>&quot;&quot;&quot;<br>type Post {<br>  comments: [Comment]<br>  id: ID!<br>  text: String!<br>  tags: [String]<br>}</pre><pre>// comments.graphql</pre><pre># import uppercase from &#39;./directives.graphql&#39;</pre><pre>type Comment {<br>  id: ID!<br>  text: String! <a href="http://twitter.com/uppercase">@uppercase</a><br>}</pre><pre>// directives.graphql</pre><pre>directive @uppercase on FIELD_DEFINITION</pre><p>These files show off a variety of common schema syntax, including nested types (i.e. Comment), comments (i.e. on Post ) and directives (i.e. @uppercase )</p><h3>Loading the schema</h3><p><a href="https://www.graphql-tools.com/">graphql-tools</a> gives us the functions we need to load in all these schema files into a single GraphQLSchema object that we can then print out. If you’re following along, create a new Node package in the same directory that you wrote the schema files;</p><pre>$ npm init -y<br>$ npm i -D graphql @graphql-tools/graphql-file-loader @graphql-tools/load</pre><p>The schema files can be loaded into a single object with this short script</p><pre>const { loadSchemaSync } = require(&quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/load&quot;);<br>const { GraphQLFileLoader } = require(&quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/graphql-file-loader&quot;);</pre><pre>const schema = loadSchemaSync(&quot;./schema.graphql&quot;, {<br>  loaders: [new GraphQLFileLoader()],<br>});</pre><p>The # import keyword tells the loadSchemaSync function where to find the required types, and as long as there aren’t any syntax errors in your schema files, this function will execute without any errors.</p><h3>Printing the schema</h3><p>graphql-tools/utils exposes another helpful function, printSchemaWithDirectives, built on top of the printSchema function from the graphql.js package. Using this, we can add to our previous script to finally write the printed schema to a combined.graphql schema.</p><pre>const fs = require(&quot;fs&quot;);</pre><pre>const { loadSchemaSync } = require(&quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/load&quot;);<br>const { GraphQLFileLoader } = require(&quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/graphql-file-loader&quot;);<br>const { printSchemaWithDirectives } = require(&quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/utils&quot;);</pre><pre>const schema = loadSchemaSync(&quot;./schemas/schema.graphql&quot;, {<br>  loaders: [new GraphQLFileLoader()],<br>});</pre><pre>fs.writeFileSync(&quot;combined.graphql&quot;, printSchemaWithDirectives(schema));</pre><p>The resulting file looks like this;</p><pre>// combined.graphql</pre><pre>schema {<br>  query: Query<br>}</pre><pre>directive <a href="http://twitter.com/uppercase">@uppercase</a> on FIELD_DEFINITION</pre><pre>type Query {<br>  posts: [Post]<br>}</pre><pre>&quot;&quot;&quot;A post with comments&quot;&quot;&quot;<br>type Post {<br>  comments: [Comment]<br>  id: ID!<br>  text: String!<br>  tags: [String]<br>}</pre><pre>type Comment {<br>  id: ID!<br>  text: String! <a href="http://twitter.com/uppercase">@uppercase</a><br>}</pre><h3>Conclusion</h3><p>We’ve converted our multiple schema files into one, and can now run it through the <a href="https://www.apollographql.com/docs/rover/">rover</a> CLI tool to generate our supergraph schema.</p><p>For some, like us, this may be the first step to federating a monolith graph and, hopefully, this ability to recreate a schema file from a GraphQLSchema object is useful to you in this endeavor.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c50c5e8e9de0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/combining-graphql-schemas-for-federated-subgraphs-c50c5e8e9de0">Combining GraphQL Schemas for Federated Subgraphs</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Node-Canvas in AWS Lambda]]></title>
            <link>https://awstip.com/node-canvas-in-aws-lambda-3eed41ec36c3?source=rss-bfd0e208ec29------2</link>
            <guid isPermaLink="false">https://medium.com/p/3eed41ec36c3</guid>
            <category><![CDATA[nodecanvas]]></category>
            <category><![CDATA[canvas]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Miles Bardon]]></dc:creator>
            <pubDate>Sun, 13 Feb 2022 21:06:34 GMT</pubDate>
            <atom:updated>2022-02-18T08:33:05.967Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*63GJ8BiLX6CW6Frw" /><figcaption>Photo by <a href="https://unsplash.com/@justynwarner?utm_source=medium&amp;utm_medium=referral">Justyn Warner</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>node-canvas is a great NodeJS library for image manipulation serverside, but it comes with caveats when running on serverless infrastructure such as AWS Lambda. In this article, we’ll dive into some solutions to run </em><em>node-canvas applications easily on AWS Lambda.</em></p><h3>The problem with Lambda</h3><p><a href="https://docs.aws.amazon.com/lambda/latest/dg/welcome.html">AWS Lambda</a> allows developers to run their code without provisioning servers. You choose your runtime, upload your production-ready code, and everything just works. The problem is, you have very little control over the operating system and installed libraries on these servers. Of course, you could build and deploy your own <a href="https://docs.aws.amazon.com/lambda/latest/dg/images-create.html">container</a> to Lambda, but this puts a lot of pressure on the developer to manage everything about your environment, including pushing you past the free tier.</p><p>node-canvas is one of many packages that require some very specific Linux libraries to be installed on the host machine, and helpfully these are <a href="https://github.com/Automattic/node-canvas/releases">provided by the maintainers</a> in each release. However, none of these libraries is installed in the Lambda environment by default. In the next few sections, I’ll go over some methods to get your apps to work with these libraries.</p><h3>Include libraries in the deployment package</h3><p>This method is useful for developers;</p><ul><li>That cannot use Lambda Layers, such as on the free version of <a href="http://localstack.cloud/">Localstack</a>.</li><li>With only one function that uses node-canvas</li><li>With small functions that don’t exceed 10MB in size</li></ul><h4>Build the lib package</h4><p>An easy way to package the libraries with one command is to build a Docker container based on the <a href="https://hub.docker.com/_/amazonlinux">amazonlinux:latest image</a> and have <em>that </em>install the node-canvas package as well as all the libraries for the correct architecture that will be running on the Lambda runner.</p><p>The following Dockerfile performs the following actions</p><ul><li>Installs cairo , pango and other image manipulation libraries from yum</li><li>Installs the latest version of node-canvas along with all relevant provided libraries</li><li>Gathers the missing libraries and overwrites their counterparts downloaded from the node-canvas release</li></ul><pre>FROM amazonlinux:latest</pre><pre>ARG LIBS=/usr/lib64<br>ARG OUT=/root<br>ARG NODE_VERSION=14</pre><pre># set up container<br>RUN yum -y update \<br>&amp;&amp; yum -y groupinstall &quot;Development Tools&quot; \<br>&amp;&amp; curl --silent --location <a href="https://rpm.nodesource.com/setup_$%7BNODE_VERSION%7D.x">https://rpm.nodesource.com/setup_${NODE_VERSION}.x</a> | bash - \<br>&amp;&amp; yum install -y nodejs gcc-c++ cairo-devel libjpeg-turbo-devel pango-devel giflib-devel</pre><pre># will be created and become working dir<br>WORKDIR $OUT/nodejs</pre><pre>RUN npm install canvas@next</pre><pre># will be created and become working dir<br>WORKDIR $OUT/lib</pre><pre># gather missing libraries<br>RUN cp $LIBS/libblkid.so.1 . \<br>&amp;&amp; cp $LIBS/libmount.so.1 . \<br>&amp;&amp; cp $LIBS/libuuid.so.1 . \<br>&amp;&amp; cp $LIBS/libfontconfig.so.1 . \<br>&amp;&amp; cp $LIBS/libpixman-1.so.0 .</pre><pre>WORKDIR $OUT/dist/lib</pre><pre># copy prebuilt and missing libs<br>RUN cp -r $OUT/nodejs/node_modules/canvas/build/Release/. . \<br>&amp;&amp; cp -a $OUT/lib/. .</pre><p>Now, we can simply build the container, start it, and copy the files out;</p><pre>$ docker build -t canvas-libs .<br>$ id=$(docker create canvas-libs)<br>$ docker cp $id:/root/dist ./path/to/function/dist</pre><p>Once you have the files in place, you’ll have to place them in the root of your function distribution directory. The directory will look like this (if you’ve bundled with Webpack, as I did);</p><pre>.<br>├── lib<br>├    └── &lt;libraries here&gt;<br>├── edfd4b118a6fe59dcdd2e46a9536fa03.node<br>└── index.js</pre><h4>Configure the application</h4><p>Finally, the application needs to be configured to actually read these libraries over the defaults included in the Lambda runtime.</p><pre>process.env[&quot;PATH&quot;] =<br> process.env[&quot;PATH&quot;] + &quot;:&quot; + process.env[&quot;LAMBDA_TASK_ROOT&quot;] + &quot;/lib&quot;;<br>process.env[&quot;LD_LIBRARY_PATH&quot;] = process.env[&quot;LAMBDA_TASK_ROOT&quot;] + &quot;/lib&quot;;<br>process.env[&quot;PKG_CONFIG_PATH&quot;] = process.env[&quot;LAMBDA_TASK_ROOT&quot;] + &quot;/lib&quot;;<br>  }</pre><p>By appending /lib to the main PATH , LD_LIBRARY_PATH and PKG_CONFIG_PATH we’re telling the underlying OS to look there as well as all the usual places<em>.</em></p><h4>Caveats</h4><p>In practice, at least with version 2.9 of node-canvas there’s an issue where the wrong libz.so.1 library is loaded because Lambdas look within /lib64 prior to loading any other libraries. This can be easily solved by adding another environment variable to the Lambda deployment</p><pre>LD_PRELOAD = &quot;/var/task/lib/libz.so.1&quot;</pre><p>This will load the library included in the deployment package first, before attempting to load it from /lib64 .</p><h3>Include libraries in a Lambda Layer</h3><p>This method is useful for developers;</p><ul><li>Who are developing and testing solely for real AWS, or paid Localstack</li><li>With multiple functions that rely on node-canvas</li></ul><h4>Build the layer</h4><p>Much in the same way as the previous method, we can define a Dockerfile to create and package the Layer. The following is an example of such a configuration;</p><pre>FROM amazonlinux:latest</pre><pre>ARG LIBS=/usr/lib64<br>ARG OUT=/root<br>ARG NODE_VERSION=14</pre><pre># set up container<br>RUN yum -y update \<br>&amp;&amp; yum -y groupinstall &quot;Development Tools&quot; \<br>&amp;&amp; curl --silent --location <a href="https://rpm.nodesource.com/setup_$%7BNODE_VERSION%7D.x">https://rpm.nodesource.com/setup_${NODE_VERSION}.x</a> | bash - \<br>&amp;&amp; yum install -y nodejs gcc-c++ cairo-devel libjpeg-turbo-devel pango-devel giflib-devel</pre><pre># will be created and become working dir<br>WORKDIR $OUT/nodejs</pre><pre>RUN npm install canvas@next</pre><pre># will be created and become working dir<br>WORKDIR $OUT/lib</pre><pre># gather missing libraries<br>RUN cp $LIBS/libblkid.so.1 . \<br>&amp;&amp; cp $LIBS/libmount.so.1 . \<br>&amp;&amp; cp $LIBS/libuuid.so.1 . \<br>&amp;&amp; cp $LIBS/libfontconfig.so.1 . \<br>&amp;&amp; cp $LIBS/libpixman-1.so.0 .</pre><pre>WORKDIR $OUT/layers/lib</pre><pre># copy prebuilt and missing libs<br>RUN cp -r $OUT/nodejs/node_modules/canvas/build/Release/. . \<br>&amp;&amp; cp -a $OUT/lib/. .</pre><pre>WORKDIR $OUT/layers</pre><pre>RUN zip -r -9 layer.zip *</pre><p>The same steps apply as above, except we now zip the lib and nodejs directories into the layer.zip as prescribed by the <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path">documentation</a>.</p><p>Copying the Layer out of the container is very similar to the previous method;</p><pre>$ id=$(docker create canvas-libs)<br>$ docker cp $id:/root/layers/layer.zip ./<br>$ docker rm -v $id</pre><h4>Configure the application</h4><p>There are many ways to deploy your function with the Layer attached, but I chose to use <a href="http://terraform.io/">Terraform</a>. In this scenario, the following configuration will deploy the packaged Layer and apply it to your already bundled code zip.</p><pre>resource &quot;aws_lambda_layer_version&quot; &quot;node_canvas_layer&quot; {<br>  filename         = &quot;./layer.zip&quot;<br>  source_code_hash = filebase64sha256(&quot;./layer.zip&quot;)<br>  layer_name       = &quot;node-canvas-layer&quot;</pre><pre>  compatible_runtimes = [&quot;nodejs14.x&quot;]<br>}</pre><pre>resource &quot;aws_lambda_function&quot; &quot;function&quot; {<br>  filename      = data.archive_file.lambda_zip.output_path<br>  function_name = &quot;example-function&quot;<br>  role          = var.lambda_iam_role_arn<br>  handler       = &quot;index.handler&quot;</pre><pre>  source_code_hash = filebase64sha256(data.archive_file.lambda_zip.output_path)</pre><pre>  runtime     = var.runtime<br>  timeout     = var.timeout<br>  memory_size = var.memory_size</pre><pre>  layers = [ aws_lambda_layer_version.node_canvas_layer.arn ]</pre><pre>  environment {<br>    variables = {<br>      LD_PRELOAD = &quot;/opt/lib/libz.so.1&quot;<br>    }<br>  }<br>}</pre><p>You’ll also notice the inclusion of the LD_PRELOAD = &quot;/opt/lib/libz.so.1&quot; as explained in the last part’s Caveat section. Whilst the application no longer requires any configuration of the PATH or other environment variables, we still need to load this library first (note the changed location, as this library is now in the Layer, expanded into the opt directory on creation).</p><h3>Conclusion</h3><p>These are just two possible ways to get node-canvas working in AWS Lambda. These steps are of course adaptable to many other packages that require specific platform and architecture libraries, and whilst the versions will change over time, I expect any further library discrepancies can be solved in the same manner.</p><p>Got other ways to get such libraries working in serverless functions? Share them in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3eed41ec36c3" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/node-canvas-in-aws-lambda-3eed41ec36c3">Node-Canvas in AWS Lambda</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrating from Apollo Server v2 to v3 — Part 1]]></title>
            <link>https://medium.com/lego-engineering/migrating-from-apollo-server-v2-to-v3-part-1-ea073462fab?source=rss-bfd0e208ec29------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea073462fab</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo-server]]></category>
            <category><![CDATA[graphql-schema]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Miles Bardon]]></dc:creator>
            <pubDate>Mon, 31 Jan 2022 09:38:19 GMT</pubDate>
            <atom:updated>2022-01-31T09:38:19.802Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Photo from the moon looking back at the earth" src="https://cdn-images-1.medium.com/max/1024/0*QrpNlZPLiTv3hbx4" /><figcaption>Photo by <a href="https://unsplash.com/@nasa?utm_source=medium&amp;utm_medium=referral">NASA</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Migrating from Apollo Server v2 to v3 — Part 1: GraphQL Tools</h3><p><em>Over the past 6 months, the LEGO.com team have been undertaking efforts to migrate their Apollo GraphQL Server to the latest version. In this multi-part series, we will share our methodologies and findings, as well as resources to help your team make the same transition.</em></p><p>In this blog, I’ll detail the journey we took to understand the prerequisites to the server upgrade, which focuses on using the GraphQL Tools packages along with their best practices, and the challenges faced in following them on an ever-changing, giant codebase. Part 2 will cover the upgrade process in detail, with some more technical considerations that had to be made to make it a reality.</p><h3>Identifying the problem</h3><p>In my second week at the LEGO Group, I was presented with what appeared to be a fairly simple task; Ensure we were using the GraphQL Import syntax correctly within our Schema (.graphql) files. For the uninitiated, GraphQL Import allows schemas to be split up into smaller files, that will then be pieced together with import statements in the same vein as JavaScript ESM imports.</p><p>In all honesty, I’d barely touched GraphQL before but was eager to learn in a slow, steady fashion by addressing some small syntax issues.</p><p>3 months later, I merged the PR with a total of 10,448 line changes, 48 files affected and 7 full team approvals. I can tell you now, this was no mean feat…</p><figure><img alt="Screenshot of pull request title in github saying refactor: SET-12808 Migrate Graphql Import to Graphql Tools showing 4799 line additions and 5649 line removals" src="https://cdn-images-1.medium.com/max/1024/1*JwTynCTHnXtyMExvVMq8Dw.png" /><figcaption>A summary of the final PR</figcaption></figure><p>To start tackling this issue, I turned to the documentation. Surely they’d have some best practices on how to correctly use their tool, right? Well, I was greeted by a lovely red message at the top of the <a href="https://www.npmjs.com/package/graphql-import">GraphQL Import npm page</a>.</p><figure><img alt="Red box with message reading: This package has been deprecated. Author message: GraphQL Import has been deprecated and merged into GraphQL Tools, so it will no longer get updates. Use GraphQL Tools instead to stay up-to-date!" src="https://cdn-images-1.medium.com/max/1024/1*9lA8AhTjVoLTUS6-5x7yRQ.png" /><figcaption>Uh oh… deprecated libraries in production?!</figcaption></figure><p>It’s a good thing we spotted that, and it even has a set of instructions on how to migrate from the old, deprecated library to this shiny new one! “This is going to be easy”, thought young(er) Miles.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FQWIDyuEzsnYXu%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FQWIDyuEzsnYXu%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FQWIDyuEzsnYXu%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="234" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dadad5ada0002f2d006d0c34bf4a276/href">https://medium.com/media/7dadad5ada0002f2d006d0c34bf4a276/href</a></iframe><h3><strong>Explicitly defining imports</strong></h3><p>Following the link explained how we could simply insert loadSchemaSync with an GraphQLFileLoader in place of the importSchema function. However, only applying these changes resulted in dozens, if not hundreds, of errors all stating that various schema types, interfaces and custom directives just couldn’t be found, such as this one;</p><pre>node_modules/@graphql-tools/load/index.js:83<br>            throw error;<br>            ^</pre><pre>Error: Couldn&#39;t find type deprecatedWithMetrics in any of the schemas.</pre><p>Documentation on this issue was lacking, to say the least. So much so, that I <a href="https://github.com/ardatan/graphql-tools/issues/3274">had to raise an issue to the developers</a> to finally understand what was going wrong. graphql-import was very lenient with the order of import statements, due to the nature of the way it progressively scanned through files and appended them to one big schema.graphql at the end. This meant that if even one file imported another, that second file would be accessible to every other schema file that was imported next. This works very differently to graphql-tools/schema which imports files and types based on the order they’re imported, if at all. A short example might be as follows;</p><pre># app.graphql<br># import Order from  &quot;./orders.graphql&quot;<br># import Price from &quot;./prices.graphql&quot;</pre><pre>type Query {<br>  order: Order!<br>  price: Price!<br>}</pre><pre># orders.graphql<br># import Price from &quot;./prices.graphql&quot;</pre><pre>type Order {<br>  id: ID!<br>  price: Price!<br>}</pre><pre># prices.graphql</pre><pre>type Price {<br>  value: Int!<br>}</pre><pre># unused.graphql</pre><pre>type OldPrice {<br>  value: String!<br>}</pre><p>Here we have 4 files, and they will be loaded with the following code;</p><pre>import { loadSchemaSync } from &quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/load&quot;;<br>import { GraphQLFileLoader } from &quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/graphql-file-loader&quot;;<br>import { addResolversToSchema } from &quot;<a href="http://twitter.com/graphql">@graphql</a>-tools/schema&quot;;<br>import { join } from &quot;path&quot;;</pre><pre>const schema = loadSchemaSync(join(__dirname, &quot;app.graphql&quot;), {<br>  loaders: [new GraphQLFileLoader()],<br>});</pre><p>In this case, the Order and Price types will only be loaded into the schema once, and the OldPrice type will be completely ignored, as it’s not imported anywhere. If we were to remove the Price import from orders.graphql we’d get an error like</p><pre>Error: Couldn’t find type Price in any of the schemas.</pre><p>This was our first big hurdle in adopting GraphQL tools; <strong>Updating every file to import exactly the types and interfaces it needed</strong>. In fact, this accounted for 27 of the files changed in the PR.</p><h3><strong>“Unknown Type” errors</strong></h3><p>With the first batch of errors resolved, the next lot could come out of hibernation. Progress at last!</p><p>We still had a lot of types not being found by the schema loader, but these types were often present in the same file as the types that were using them, and not explicitly imported anywhere. <a href="https://github.com/ardatan/graphql-tools/issues/1591">Others appeared to have the same issue</a>, though this had been reported to be fixed. With some further digging and experimentation, we found that whilst the fix referenced in this issue targeted imports nested 3 levels deep, that’s where it stopped.</p><p>Our schema is large, and often has types that nest more than 8 times! <a href="https://github.com/ardatan/graphql-tools/issues/3327">I put forward another Issue</a> with a replication of the issue showing how only 4 nested imports (be they explicit or implicit, within the same file), would break the schema.</p><p>As we were very unfamiliar with the tools, we did try to understand where the problem was and fix it but were grateful to be beaten to the mark by GitHub user jacob-carpenter, who <a href="https://github.com/ardatan/graphql-tools/pull/3536">submitted a fix</a> that proved to solve our problems (almost).</p><h4><strong>And then there were 3</strong></h4><p>We were on the home stretch — only 3 type errors remained once this new version of GraphQL Tools was applied to the server. These errors actually occurred <em>after</em> the schema was collated, when being read by Apollo Server</p><pre>throw new Error(errors.map(function (error) {<br>    ^</pre><pre>Error: Unknown type &quot;ProductSection&quot;. Did you mean &quot;BasicProductSection&quot;, &quot;CodedSection&quot;, &quot;LayoutSection&quot;, &quot;GridSection&quot;, or &quot;PollsSection&quot;?</pre><pre>Unknown type &quot;ProductSection&quot;. Did you mean &quot;BasicProductSection&quot;, &quot;CodedSection&quot;, &quot;LayoutSection&quot;, &quot;GridSection&quot;, or &quot;PollsSection&quot;?</pre><pre>Unknown type &quot;ProductSection&quot;. Did you mean &quot;BasicProductSection&quot;, &quot;CodedSection&quot;, &quot;LayoutSection&quot;, &quot;GridSection&quot;, or &quot;PollsSection&quot;?</pre><p>For reference, these types followed this pattern (redacted for brevity);</p><pre>interface ProductSection {<br>  id: ID!<br>}</pre><pre>type BasicProductSection implements ContentSection &amp; ProductSection {<br>  id: ID!<br>  removePadding: Boolean<br>}</pre><pre>type DisruptorProductSection implements ContentSection &amp; ProductSection {<br>  id: ID!<br>  disruptor: Disruptor!<br>}</pre><pre>type CountdownProductSection implements ContentSection &amp; ProductSection {<br>  id: ID!<br>  countdown: CountdownBannerChild!<br>}</pre><p>It took some experimentation to find what was causing only these three types to error out, and we found the answer after inspecting the contents of the schema object created above. Simply, in this case, only the ProductSection is not found, so why is ContentSection not throwing an error, too? Let’s look at the object created by loadSchemaSync again edited for brevity:</p><pre>&quot;_typeMap&quot;: {<br>    &quot;Query&quot;: &quot;Query&quot;,<br>    &quot;String&quot;: &quot;String&quot;,<br>    &quot;Float&quot;: &quot;Float&quot;,<br>    ...<br>    &quot;ContentSection&quot;: &quot;ContentSection&quot;,<br>    ...<br>}</pre><p>Quite conveniently, we can trace the _typeMap all the way through our schema to find how each type is found. Query is the first type found, as it’s at the top of our app.schema file, and the next types used are the built-in String and Float types. This is completely dependent on your schema, however, so your mileage may vary.</p><p>Working our way down the schema, and weaving into and out of nested types, we eventually find the ContentSection used as an interface on a type accessible from the root Query. However, nowhere in this _typeMap exists ProductSection! A quick search shows that this interface, and these types, aren’t used in a single place in the schema, but are referenced as types in the Resolvers, that are passed to the Apollo Server.</p><h4><strong>What <em>not</em> to do</strong></h4><p>My first thought was “how do we get the ProductSection into the type map?”. One way is to create a field that has the ProductSection as its type and immediately mark it as deprecated, or otherwise hidden. Whilst this way “works”, it exposes a field on the schema that is never intended for any use. In the case of the LEGO® Shop, we are the only consumers of our Graph, but for other teams with a public audience, this wouldn’t be desirable at all.</p><h4><strong>A better solution</strong></h4><p>Going back to basics, the only real problem that the server has with our schema is that the types shown above are not imported. So, why not just import them manually? Luckily for us, the loadSchemaSync API allows for an array of file pointers to be passed, that will all be imported as long as there are no type clashes (that is to say two type names aren’t identical and cannot be merged without conflicts). Here’s what worked for us;</p><pre>const baseSchema = &quot;./schemas/app.graphql&quot;;<br>// The file with the problem types/interfaces<br>const extraTypeSchemas = [&quot;./schemas/content.graphql&quot;];</pre><pre>const typeDefs = loadSchemaSync([baseSchema, ...extraTypeSchemas], {<br>  loaders: [new GraphQLFileLoader()],<br>});</pre><p>And with that, our server could launch without trouble!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F2xO491sY6f0cM%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fcommunity-things-2xO491sY6f0cM&amp;image=https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2F2xO491sY6f0cM%2Fgiphy.gif%3Fcid%3D790b76117449061af9d121f66ed4734ae8436d873f2bfae5%26rid%3Dgiphy.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="397" frameborder="0" scrolling="no"><a href="https://medium.com/media/f60e64fa37ae70492f7fcb1971cad470/href">https://medium.com/media/f60e64fa37ae70492f7fcb1971cad470/href</a></iframe><h3>Conclusion</h3><p>I’ve gone through in fine detail the steps we took to upgrade our tooling to the latest version of GraphQL Tools, including our pitfalls and successes, particularly in the Open Source realm.</p><p>Throughout this task, one of the most challenging things was to stay motivated and keep plugging away at the problem, and with the help of my colleagues and the wider GraphQL community, we were able to accomplish what many would have written off as unfixable!</p><p>In the next instalment in this blog series, I will take a deep dive into the migration between the two Apollo versions, and detail some more technical challenges faced there, so stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea073462fab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lego-engineering/migrating-from-apollo-server-v2-to-v3-part-1-ea073462fab">Migrating from Apollo Server v2 to v3 — Part 1</a> was originally published in <a href="https://medium.com/lego-engineering">Engineers @ The LEGO Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>