<?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[Graphcool Blog - Medium]]></title>
        <description><![CDATA[Guides and articles about GraphQL, Relay, Apollo &amp; more - Medium]]></description>
        <link>https://medium.com/graphcool?source=rss----3691a1eb1fee---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Graphcool Blog - Medium</title>
            <link>https://medium.com/graphcool?source=rss----3691a1eb1fee---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 11:05:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/graphcool" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Stay in Touch with the Prisma Community]]></title>
            <link>https://medium.com/graphcool/stay-in-touch-with-the-prisma-community-b1654db1c248?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/b1654db1c248</guid>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[graphcool]]></category>
            <category><![CDATA[relational-databases]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Nikolas Burk]]></dc:creator>
            <pubDate>Mon, 18 Jan 2021 10:42:50 GMT</pubDate>
            <atom:updated>2021-01-18T10:42:50.608Z</atom:updated>
            <content:encoded><![CDATA[<h4>Graphcool is now <a href="https://www.prisma.io/">Prisma</a>. Join the community and stay up-to-date about the best learning resources for GraphQL, databases and a lot more!</h4><p>Happy 2021 friends! 👋</p><p>2020 was an eventful year since the <a href="https://www.graph.cool/">Graphcool sunset</a>, we’ve launched a lot of new features as <a href="https://www.prisma.io/">Prisma</a>. We’ve also been focusing on and growing the Prisma community with lots of new events, videos, and articles being published weekly.</p><p>The Graphcool Medium was something we loved maintaining and wanted to make sure that for folks that find topics like databases, Node.js, TypeScript, and, yes, some GraphQL development interesting, that they know there are lots of places to join us in the Prisma community.</p><h4><strong>For those who want to join the conversation</strong></h4><p>If you aren’t yet a part of our Prisma public slack channel, we recommend signing up to stay in touch. It’s a great way to connect to others in our community, see what people are building with Prisma, be alerted about new events and articles published:</p><p>▶️ <a href="https://slack.prisma.io/">Prisma Slack</a></p><h4><strong>For those looking for events</strong></h4><p>Prisma is still hosting tons of events and meetups, all happening on the <a href="https://www.youtube.com/channel/UCptAHlN1gdwD89tFM3ENb6w">Prisma YouTube channel</a>.</p><p>The YouTube channel is headquarters for all sorts of interactive livestreams and meetups, but you can be informed about upcoming events ahead of time by joining of our four main Prisma Meetup Groups, all of whose events are hosted online:</p><p><strong>📌</strong> <a href="https://www.meetup.com/Berlin-Prisma-Meetup/">Berlin Prisma Meetup</a></p><p>📌 <a href="https://www.meetup.com/TypeScript-Berlin/">TypeScript Berlin</a></p><p>📌 <a href="https://www.meetup.com/advanced-typescript-trickery/">Advanced TypeScript Trickery</a></p><p>📌 <a href="https://www.meetup.com/graphql-berlin/">GraphQL Berlin</a></p><p>Additionally Prisma is gearing up to host its first enterprise event. We invite interested folks to stay in the loop and share their input about they’d like to see:</p><p>💼 <a href="https://prisma103696.typeform.com/to/lw1kNjUl">Enterprise Event Sign Up</a></p><h4><strong>For those who want to read</strong></h4><p>In addition to the community activities that Prisma hosts regularly, we are still focused on publishing content. While this Medium no longer hosts articles, there are a variety of places where we’re publishing articles, tutorials, and educational content:</p><p>📒 <a href="https://www.prisma.io/blog/">Prisma Blog</a>: homebase for all new Prisma announcements and development tutorials</p><p>📒 <a href="https://www.prisma.io/dataguide">Prisma’s Data Guide</a>: educational knowledgebase about everything to do with databases</p><p>📒 <a href="https://dev.to/prisma">Prisma’s Dev To</a>: additional guides and resources on Prisma and related technology</p><p>We’ve loved working on Graphcool and consider the community around any tool one of its defining features. We hope to see you soon as part of the Prisma community! 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1654db1c248" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/stay-in-touch-with-the-prisma-community-b1654db1c248">Stay in Touch with the Prisma Community</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL Binding 2.0: Improved API, schema transforms & automatic codegen]]></title>
            <link>https://medium.com/graphcool/graphql-binding-2-0-improved-api-schema-transforms-automatic-codegen-5934cd039db1?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/5934cd039db1</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[articles]]></category>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Thu, 10 May 2018 17:33:32 GMT</pubDate>
            <atom:updated>2018-05-10T17:52:44.503Z</atom:updated>
            <content:encoded><![CDATA[<p>In a <a href="https://blog.graph.cool/reusing-composing-graphql-apis-with-graphql-bindings-80a4aa37cff5">recent blog post</a>, we introduced the concept of GraphQL bindings as a way for <em>reusing</em> and <em>composing</em> GraphQL APIs. Since then, we have received great feedback from the community and thought a lot about further use cases and scenarios for bindings — today we are proud to present version 2.0 🎉</p><blockquote><a href="https://github.com/graphql-binding/graphql-binding">Go to the </a><a href="https://github.com/graphql-binding/graphql-binding"><em>graphql-binding</em> GitHub repo.</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ErvlEPgvCl_LH2sKLJvpsQ.png" /></figure><p>Here’s an overview of the major changes &amp; new features:</p><ul><li>More <strong>flexible and powerful API</strong> (e.g. passing context, applying <a href="https://www.apollographql.com/docs/graphql-tools/schema-transforms.html">schema transforms</a> or modifying the <a href="https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a">info</a> object)</li><li>It’s now super easy to<em> </em><strong>create your own GraphQL bindings</strong></li><li>Powerful tooling for automatic <strong>code generation</strong></li><li>Seamless <strong>integration</strong> with <a href="https://github.com/graphcool/graphql-config">graphql-config</a></li></ul><p>In this blog post, you’ll learn <a href="https://blog.graph.cool/graphql-binding-2-0-improved-api-schema-transforms-easier-creation-5934cd039db1#c432">what GraphQL bindings are</a>, <a href="https://blog.graph.cool/graphql-binding-2-0-improved-api-schema-transforms-easier-creation-5934cd039db1#126e">how to use them</a> as well as <a href="https://blog.graph.cool/graphql-binding-2-0-improved-api-schema-transforms-easier-creation-5934cd039db1#e198">how you can create your own GraphQL binding</a>.</p><h3>What are GraphQL Bindings?</h3><h4>GraphQL bindings allow you to send queries and mutations in your programming language</h4><p>GraphQL bindings provide a convenient way for interacting with a GraphQL API: Instead of sending queries as strings via HTTP, you invoke a binding function which constructs the query and sends it to the GraphQL server for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I4zR2442zdvwlwmCH5x0FQ.png" /></figure><h4>Auto-generated SDKs for your programming language</h4><p>The best thing about GraphQL bindings is that they’re automatically generated, meaning they effectively come for free — think of them like an <em>auto-generated SDK for a specific GraphQL API</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RzlbHpIbo1g46x3x2lPUkQ.png" /></figure><h3>How to use GraphQL Bindings?</h3><h4>Use cases</h4><p>GraphQL bindings are an extremely flexible tool that can be used in a variety of scenarios. Here are a few use cases where using bindings comes in handy:</p><ul><li>GraphQL-based <em>service-to-service communication</em></li><li>Building <em>GraphQL gateways</em></li><li>Accessing GraphQL APIs from a <em>script</em></li><li>Adding <em>custom operations</em> to a GraphQL API</li><li>Sharing <em>runnable instances</em> of GraphQL APIs</li></ul><p>In the following two sections, we’ll explain two common usage scenarios for GraphQL bindings — the examples are based on this GraphQL schema:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d3cb7833d997eeb7d0d928f9c2b9e97/href">https://medium.com/media/3d3cb7833d997eeb7d0d928f9c2b9e97/href</a></iframe><p>Let’s also assume this GraphQL API would be available as a GraphQL binding through an NPM package called graphql-binding-users.</p><h4>Scenario 1: Usage in a simple Node script</h4><p>The easiest way to use GraphQL bindings is with a simple Node script.</p><p>A GraphQL binding for the above schema will now let you send the users query and createUser mutation by invoking binding functions in JavaScript, here is what the code for the createUser mutation looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65ac340a6903b0132d6a1f35a1161a5c/href">https://medium.com/media/65ac340a6903b0132d6a1f35a1161a5c/href</a></iframe><p>The mutation property on the userBinding exposes functions for all mutations. Each binding function takes (at least) two arguments:</p><ul><li>The <strong>arguments</strong> of the operation</li><li>The <a href="https://medium.com/front-end-developers/graphql-selection-sets-d588f6782e90"><strong>selection set</strong></a><strong> </strong>of the operation</li></ul><p>In the above case, the arguments are wrapped in an object and contain the name of the new User, the selection set only specifies the id field. This invocation of the createUser function will construct and send the following mutation to the GraphQL API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30e9e502eda473d46119394ba246d605/href">https://medium.com/media/30e9e502eda473d46119394ba246d605/href</a></iframe><p>The users query which is sent in the following code snippet does not receive any arguments:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed071f79071e0ab6490f1f60e0ef96a9/href">https://medium.com/media/ed071f79071e0ab6490f1f60e0ef96a9/href</a></iframe><p>This function call is translated into the following query:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2505a126f325f34ad8085fc93725a489/href">https://medium.com/media/2505a126f325f34ad8085fc93725a489/href</a></iframe><p>In these cases, the binding is used as a convenient way to interact with a GraphQL API. Using bindings in statically typed languages even allows to have <em>typed inputs and outputs </em>for the binding functions which allows for further features like autocompletion and compile-time error checking.</p><h4>Scenario 2: Usage in a GraphQL gateway</h4><p>When building gateways for GraphQL-based microservices, bindings are especially helpful. In the previous example, you have seen binding functions where the selection sets for the constructed operations were passed as (static) <em>strings</em>.</p><p>For the current use case of a GraphQL gateway, the binding API allows to generate the selection set <em>dynamically </em>by passing on the <a href="https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a">info</a> argument of the GraphQL resolvers.</p><p>Let’s assume we have a gateway server which puts another GraphQL layer on the initial schema — here is what the gateway schema looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2ecfb788b40c848cd3666f6a6f82ac5/href">https://medium.com/media/c2ecfb788b40c848cd3666f6a6f82ac5/href</a></iframe><p>The API defined by this schema allows to retrieve a single User by their id and create new Users with random names.</p><p>The resolvers for this schema can be implemented as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fdd093f560f778fb8fce6904aaab155/href">https://medium.com/media/3fdd093f560f778fb8fce6904aaab155/href</a></iframe><p>The major difference compared to the previous usage scenario is that this time the selection set is not hardcoded any more — instead it’s determined based on the selection set of the incoming query.</p><p>The <a href="https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a">info</a> object carries the <a href="https://medium.com/@cjoudrey/life-of-a-graphql-query-lexing-parsing-ca7c5045fad8">AST</a> of the incoming query, so it “knows” the requested fields from the selection set. Because the operations in the gateway layer are both of type User and the ones in the underlying GraphQL API are of type User as well, the query can be <a href="https://blog.graph.cool/graphql-schema-stitching-explained-schema-delegation-4c6caf468405">delegated</a> by simply passing on the info object.</p><h3>How to create your own GraphQL Binding?</h3><p>Let’s now discuss how you can actually create your own bindings (which for example could then be shared via NPM).</p><p>To create your own binding, you need to extend the Binding class from the graphql-binding package and provide an <em>executable </em>instance of <a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e#e3b9">GraphQLSchema</a>. Your Binding subclass will then expose all operations from the executable schema as binding functions.</p><h4>Example 1: Creating a binding from a local schema</h4><p>In this example, the gateway server will use an executable schema that’s implemented locally in the (instead of referring to a deployed GraphQL API).</p><p>The GraphQL-based microservice consumed by the gateway exposes CRUD operations for a Post type with the this schema:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e041b1ebf33b50430b8a68cb31daeba/href">https://medium.com/media/9e041b1ebf33b50430b8a68cb31daeba/href</a></iframe><p>The gateway exposes domain-specific operations on the Post types (rather than the generic CRUD operations):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0bebbce2003155506b35684bec346a6/href">https://medium.com/media/f0bebbce2003155506b35684bec346a6/href</a></iframe><p>You can check out the inner workings of the entire gateway server in the embedded <a href="http://www.glitch.com">Glitch</a> element (note that for simplicity, all data is stored in-memory rather than being persisted to a database):</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fglitch.com%2Fembed%2F%23%21%2Fembed%2Fexample-1%3Fpath%3Dgateway%2Fsrc%2Findex.js%26previewSize%3D0&amp;dntp=1&amp;url=https%3A%2F%2Fglitch.com%2Fembed%2F%23%21%2Fembed%2Fexample-1&amp;image=https%3A%2F%2Fglitch.com%2Fedit%2Fimages%2Flogos%2Fglitch%2Fsocial-card%402x.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=glitch" width="600" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3a7b23bf524f62bb652da9dbc7dcd97/href">https://medium.com/media/a3a7b23bf524f62bb652da9dbc7dcd97/href</a></iframe><p>Here’s an overview of the different files:</p><ul><li>post-service/src/schema.js : Creates and exports the executable GraphQLSchema object (by providing a schema definition and implementing corresponding GraphQL resolvers).</li><li>gateway/src/PostBinding.js: Creates and exports the PostBinding class which extends the Binding class from graphql-binding.</li><li>gateway/src/index.js: Implements the GraphQL gateway server by delegating all requests to the underlying microservice using PostBinding.</li></ul><h4>Example 2: Creating a binding from a deployed GraphQL API</h4><p>In the first example, the executable schema was created by providing a schema definition and matching resolvers. In this section, we’re going to create a <a href="https://blog.graph.cool/how-do-graphql-remote-schemas-work-7118237c89d7"><em>remote executable schema</em></a><em> </em>which requires the URL of a deployed GraphQL API.</p><p>For the purpose of this tutorial, we’ve quickly deployed the GraphQL microservice from the previous example using <a href="https://blog.graph.cool/deploying-graphql-servers-with-zeit-now-85f4757b79a7">Zeit Now</a>, here is the URL of the deployed API: <a href="https://post-service-nyvvyjtbyt.now.sh">https://post-service-nyvvyjtbyt.now.sh</a></p><p>Now, we adjust the way how the PostBinding class is created and create a remote executable schema based on the deployed API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3145ea388d75804a19956859dee5694/href">https://medium.com/media/d3145ea388d75804a19956859dee5694/href</a></iframe><p>Again, you explore and test the entire gateway server using Glitch:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fglitch.com%2Fembed%2F%23%21%2Fembed%2Fexample-2%3Fpath%3Dgateway%2Fsrc%2Findex.js%26previewSize%3D0&amp;dntp=1&amp;url=https%3A%2F%2Fglitch.com%2Fembed%2F%23%21%2Fembed%2Fexample-2&amp;image=https%3A%2F%2Fglitch.com%2Fedit%2Fimages%2Flogos%2Fglitch%2Fsocial-card%402x.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=glitch" width="600" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/5204e21ebd48e69373f238126d03608c/href">https://medium.com/media/5204e21ebd48e69373f238126d03608c/href</a></iframe><h4>Example 3: Creating a gateway for two GraphQL APIs</h4><p>In this example, we’re adding another GraphQL-based microservice to be consumed by the gateway and create a new abstraction which makes it a bit easier to create a GraphQL binding based on a deployed GraphQL API.</p><p>Here is what the schema for the new GraphQL microservice looks like, it provides CRUD operations for a User type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0865d296eee61a5329c85ac053dd800c/href">https://medium.com/media/0865d296eee61a5329c85ac053dd800c/href</a></iframe><p>Note that we’ve deployed a version of this API using Now to this URL: <a href="https://user-service-sfhcesmzmg.now.sh">https://user-service-sfhcesmzmg.now.sh</a></p><p>Since we now have two GraphQL APIs for which we want to generate a GraphQL binding, we’re introducing a new abstraction to reduce code duplication:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/212855d104224eb64c03eb6677cf103c/href">https://medium.com/media/212855d104224eb64c03eb6677cf103c/href</a></iframe><p>We’re also attaching both binding instances to the context rather than using “global” versions of them inside the resolvers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8e9aef02e28062259c8270eda019ea6/href">https://medium.com/media/a8e9aef02e28062259c8270eda019ea6/href</a></iframe><p>Feel free to play around with the running version of the gateway server in Glitch:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fglitch.com%2Fembed%2F%23%21%2Fembed%2Fexample-3%3Fpath%3Dgateway%2Fsrc%2Findex.js%26%26previewSize%3D0&amp;dntp=1&amp;url=https%3A%2F%2Fglitch.com%2Fembed%2F%23%21%2Fembed%2Fexample-3&amp;image=https%3A%2F%2Fglitch.com%2Fedit%2Fimages%2Flogos%2Fglitch%2Fsocial-card%402x.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=glitch" width="600" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/98c2e09c448743face545c1adcf68702/href">https://medium.com/media/98c2e09c448743face545c1adcf68702/href</a></iframe><h3>Stay tuned for upcoming blog posts</h3><p>In this blog post, we only covered the most basic use cases of GraphQL bindings, using them in scripts or in GraphQL gateway servers. There are several important topics that we haven’t touched on yet, most notably:</p><ul><li>Extending a GraphQL binding with custom functionality</li><li>Code generation and type safety for GraphQL bindings</li><li>Using schema transforms</li></ul><p>We’ll discuss those topics in-depth in a number of upcoming blog posts, stay tuned… 👀</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F38bbbd%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F38bbbd%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F38bbbd%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href">https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5934cd039db1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/graphql-binding-2-0-improved-api-schema-transforms-automatic-codegen-5934cd039db1">GraphQL Binding 2.0: Improved API, schema transforms &amp; automatic codegen</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prisma now supports Postgres]]></title>
            <link>https://medium.com/graphcool/prisma-now-supports-postgres-aad74ba479cb?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/aad74ba479cb</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[announcements]]></category>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[postgres]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Tue, 08 May 2018 17:17:53 GMT</pubDate>
            <atom:updated>2018-05-10T15:46:57.823Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, we’re releasing Prisma 1.8 which introduces support for PostgreSQL. This has been <a href="https://github.com/graphcool/prisma/issues/1641">one of Prisma’s most requested features</a> so far.</p><p>Simply run npm install -g prisma to install the latest version. You can find the <a href="https://github.com/graphcool/prisma/releases/tag/1.8.0">full changelog here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BUUYQwOhLtnG1_UJuYayMQ.png" /></figure><h3>Our ambitious goal: Support for all major databases</h3><p>The most common question we’re getting from developers and companies is: <strong>When will Prisma add support for database XYZ?</strong></p><p>Some applications use Postgres as their primary source of truth and sync data to Elasticsearch for advanced text search. Other applications are based on a scale-out database like Cassandra or DynamoDB for primary data and maintain projections into a SQL or Mongo database in order to retrieve all data for a particular view in a performant, single query.</p><p>In today’s infrastructure ecosystem it’s the default to pick specialized databases and combine their capabilities to maximize performance and flexibility. Embracing this new way of organizing data, the Prisma’s goal is to unify the data layer for modern applications through the simplicity and power of GraphQL.</p><p>To fulfil this promise, Prisma has to support a multitude of different databases. This is the reason why our highest priority for 2018 is to add support for many more databases starting with <a href="https://github.com/graphcool/prisma/issues/1641">Postgres</a>, <a href="https://github.com/graphcool/prisma/issues/1643">MongoDB</a> and <a href="https://github.com/graphcool/prisma/issues/1665">Elasticsearch</a> which are the most frequently requested databases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3jWNPvmjGcl9hai_Hj9ktQ.png" /></figure><p>Adding support for Postgres is especially interesting since more and more newer databases (such as <a href="https://github.com/cockroachdb/cockroach">CockroachDB</a> and <a href="https://github.com/timescale/timescaledb">TimescaleDB</a>) are adopting the PostgreSQL syntax and are therefore compatible with Prisma out of the box.</p><p>So let’s see how you can use Prisma together with Postgres today:</p><h3>Getting started with Prisma and Postgres</h3><p>Once you’ve installed the Prisma CLI via npm install -g prisma on your machine, the easiest way to get started is by running prisma init:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8pzOn6PlEK4i9HRgf1NzFg.png" /></figure><p>Here you can choose to set up a new Prisma server (using Docker) or use an already existing Prisma server. When setting up a new server Prisma allows you to connect to an existing database or create a new one running in Docker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hC_OiN9nDSFSp5GD5Lsidg.png" /></figure><p>In this case we’ve chosen the “Create new database” option and selected the “Postgres” database option. The Prisma CLI creates all necessary files for you to get started including the docker-compose.yml which looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/297505d57b19215808283e1f90f23d84/href">https://medium.com/media/297505d57b19215808283e1f90f23d84/href</a></iframe><p>Now we can simply run docker-compose up -d to start both the Prisma server and the Postgres database (this requires Docker to be installed locally).</p><p>Once both containers are running, you can deploy the Prisma service via prisma deploy. This will migrate the underlying Postgres database based on your datamodel specified in the datamodel.graphql file and makes your Prisma GraphQL API available under http://localhost:4466.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dw0Zf3Hhw_pcnrqZ6JwznA.png" /></figure><p>You’re now ready to start using your GraphQL API. Simply explore the API capabilities via the <a href="https://github.com/graphcool/graphql-playground">GraphQL Playground</a> or build your own GraphQL server using Prisma as a GraphQL ORM layer based on prisma-binding. <a href="https://www.prisma.io/docs/tutorials/prisma-basics/getting-started-ouzia3ahqu">Read more.</a></p><h3>Connect to an existing database (experimental)</h3><p>Besides using Prisma together with a new database, Prisma 1.8 introduces experimental support for connecting to an existing Postgres database. This let’s you <strong>turn your existing database into a powerful GraphQL API</strong>.</p><p>The usage flow is very similar to using a new database. By simply configuring your database credentials while running prisma init, Prisma will <a href="https://www.prisma.io/docs/1.8/reference/service-configuration/data-model/introspection-aeb6diethe">introspect</a> your existing database and create a matching datamodel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CH3QRfRyfo1s41D2Nj_8fA.png" /></figure><p>The created datamodel is a mapping to your existing database and serves as the foundation for the auto-generated GraphQL CRUD API. You can use Postgres-specific directives (e.g. @pgTable or @pgRelation) to control how your Postgres database is mapped into a GraphQL API.</p><p>Here is how the datamodel could look like after the initial introspection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4170e8b1abd86b6c009943112f6dc63f/href">https://medium.com/media/4170e8b1abd86b6c009943112f6dc63f/href</a></iframe><p>Making Prisma work out of the box for existing databases in every scenario is a hard technical problem requiring a lot of work (and tests), so there are still <a href="https://github.com/graphcool/prisma/issues/2377">some known limitations and bugs</a>.</p><p>We’re expecting to release a stable version of this feature in one of the next upcoming versions. If you’re running into any edge cases or bugs while testing this feature, please <a href="https://github.com/graphcool/prisma/issues/new">open an issue on Github</a>.</p><h3>What’s next?</h3><p>This release marks an important milestone in the history of Prisma adding support for Postgres — one of the most popular open-source databases used by millions of applications.</p><p>However, this is also just the beginning of bringing the power of the Postgres ecosystem to the world of GraphQL. We are already looking into ways how to add support for <a href="https://github.com/graphcool/prisma/issues/1183">full-text search</a>, <a href="https://github.com/graphcool/prisma/issues/1760">Postgis</a> and other Postgres extensions.</p><p>Besides Postgres we’re already working on other database connectors. Keep an eye out for the <a href="https://github.com/graphcool/prisma/issues/1643">upcoming MongoDB connector</a>. 👀</p><p><strong>Thank you, to all beta testers 🙏</strong></p><p>A special thanks goes out to all of our amazing beta testers who have helped us over the last few weeks to find bugs in the Postgres connector. We’re looking forward to your findings regarding the support for existing databases.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F38bbbd%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F38bbbd%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F38bbbd%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href">https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aad74ba479cb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/prisma-now-supports-postgres-aad74ba479cb">Prisma now supports Postgres</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tutorial: Building a Realtime GraphQL Server with Subscriptions]]></title>
            <link>https://medium.com/graphcool/tutorial-building-a-realtime-graphql-server-with-subscriptions-2758cfc6d427?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/2758cfc6d427</guid>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Thu, 26 Apr 2018 16:58:37 GMT</pubDate>
            <atom:updated>2018-04-30T08:48:12.095Z</atom:updated>
            <content:encoded><![CDATA[<h4>GraphQL subscriptions allow clients to receive realtime updates from a GraphQL API upon specific events</h4><p>Added to the <a href="http://facebook.github.io/graphql/draft/#sec-Subscription">official GraphQL spec</a> only about a year ago, GraphQL subscriptions are one of the newer and less explored features of GraphQL. In this tutorial, you’ll learn how you can easily add subscriptions to your GraphQL API using <a href="https://www.prisma.io">Prisma</a> and <a href="https://github.com/graphcool/prisma-binding">Prisma bindings</a>.</p><blockquote>The finished project of this tutorial can be found on <a href="https://github.com/nikolasburk/subscriptions">GitHub</a>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z89KhXhbI0jjH4VZ_qNCNQ.png" /></figure><h3>Overview</h3><h4>Subscriptions allow clients to receive event-based realtime updates</h4><p>One convenient property of GraphQL subscriptions is that they’re using the exact same syntax as queries and mutations. From a client perspective, this means there’s nothing new to learn to benefit from this feature.</p><p>The major difference between subscriptions and queries/mutations lies in the <em>execution</em>. While queries and mutations follow typical request-response cycles (just like regular HTTP requests), subscriptions don’t return the requested data right away. Instead, when a GraphQL server receives a subscription request, it creates a <em>long-lived connection</em> to the client which sent the request.</p><p>With that request, the client expressed interest in data that’s related to a specific <em>event</em>, for example a specific user liking a picture. The corresponding subscription might look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74222fd2e34e7c152ade50bb928ee533/href">https://medium.com/media/74222fd2e34e7c152ade50bb928ee533/href</a></iframe><p>When the user in question now likes a picture, the server pushes the requested data to the subscribed client via their connection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef919f3a3854e6f0d48ca73def0c907b/href">https://medium.com/media/ef919f3a3854e6f0d48ca73def0c907b/href</a></iframe><h4>Implementing subscriptions with WebSockets</h4><p>Subscriptions are commonly implemented with <a href="https://en.wikipedia.org/wiki/WebSocket">WebSockets</a>. Apart from the realtime logic (which is typically handled via pub/sub-systems), you need to implement the official <a href="https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md">communication protocol</a> for GraphQL subscriptions. Only if your server follows the flow defined in the protocol, clients will be able to properly initiate requests and receive event data.</p><p>Dealing with realtime logic and pub/sub-systems, properly accessing databases and taking care of implementing the subscription protocol can become fairly complex. Authentication and authorization logic further complicate the implementation of GraphQL subscriptions on the server. In these cases, it’s helpful to use proper abstractions that make your life easier.</p><p>One such abstraction is provided by Prisma in combination with Prisma bindings. Think of that combo as a “GraphQL ORM” layer where realtime subscriptions are supported out-of-the-box, making it easy for you to add subscriptions to your API.</p><h3>1. Project setup</h3><h4>1.1. Download and explore the starter project</h4><p>The first step in this tutorial is to get access to the starter project. If you don’t want to actually follow the tutorial but are only interested in what the subscription code looks like, feel free to <a href="https://medium.com/@graphcool/tutorial-building-a-realtime-graphql-server-with-subscriptions-2758cfc6d427#07a8">skip ahead</a>.</p><p>You can download the starter project from <a href="https://github.com/nikolasburk/subscriptions">this</a> repository using the following terminal command. Also, directly install the npm dependencies of the project:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c486b75d219d2907b90186d7aeb2f083/href">https://medium.com/media/c486b75d219d2907b90186d7aeb2f083/href</a></iframe><p>The project contains a very simple GraphQL API with the following schema:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa5c8aae74fa7e9da7ed43d11e0e7c7c/href">https://medium.com/media/aa5c8aae74fa7e9da7ed43d11e0e7c7c/href</a></iframe><p>The Post type is defined via the <a href="https://github.com/nikolasburk/subscriptions/blob/starter/database/datamodel.graphql">Prisma data model</a> and looks as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1cf47853d2d51f0843d403d7e4a19ae/href">https://medium.com/media/f1cf47853d2d51f0843d403d7e4a19ae/href</a></iframe><p>The goal for this project will be to add two subscriptions to the API:</p><ul><li>A subscription that fires when a new Post is <em>created</em> or the title of an existing Post is <em>updated</em>.</li><li>A subscription that fires when an existing Post is <em>deleted</em>.</li></ul><h4>1.2. Deploy the Prisma database API</h4><p>Before starting the server, you need to ensure the Prisma database API is available and can be accessed by your GraphQL server (via Prisma bindings).</p><p>To deploy the Prisma API, run the yarn prisma deploy command inside the subscriptions-starter directory.</p><p>The CLI will then prompt you with a few questions regarding <em>how </em>you want to deploy the API. For the purpose of this tutorial, choose any of the <strong>Prisma Sandbox</strong> options ( sandbox-eu1 or sandbox-us1), then simply hit <strong>Enter</strong> to select the suggested values for the <em>service name</em> and <em>stage</em>. (Note that if you have <a href="https://www.docker.com">Docker</a> installed, you can also deploy <em>locally</em>).</p><p>Once the API is deployed, the CLI prints the HTTP endpoint<em> </em>for the Prisma database API. Copy that endpoint and paste it into index.js where your GraphQLServer is instantiated. Note that you need to <em>replace</em> the current placeholder __PRISMA_ENDPOINT__. After you did this, the code will look similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e310c2bae181b75bae859010e092b312/href">https://medium.com/media/e310c2bae181b75bae859010e092b312/href</a></iframe><h4>1.3. Open a GraphQL Playground</h4><p>You can now start the server and open up a <a href="https://github.com/graphcool/graphql-playground">GraphQL Playground</a> by running the yarn dev command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GoxKn35GhQpmHZ6bnKbDZA.png" /></figure><p>Feel free to explore the project and send a few queries and mutations.</p><blockquote><strong>Note:</strong> The Playground shows you the two GraphQL APIs which are defined in <a href="https://github.com/nikolasburk/subscriptions/blob/master/.graphqlconfig.yml">.graphqlconfig.yml</a>. The <strong>app</strong> project represents the <strong>application layer</strong> and is defined by the GraphQL schema in /src/schema.graphql. The <strong>database</strong> project represents your <strong>database layer</strong> and is defined by the auto-generated Prisma GraphQL schema in /src/generated/prisma.graphql.</blockquote><blockquote><strong>Learn more: </strong>To learn more about this architecture, be sure to check out the corresponding <a href="https://www.prisma.io/docs/reference/introduction/architecture-jaeraegh6e">documentation</a>. For an in-depth learning experience, follow the <a href="https://www.howtographql.com/graphql-js/0-introduction/">Node tutorial on How to GraphQL</a></blockquote><h3>2. Understanding Prisma’s subscription API</h3><h4>2.1. Overview</h4><p>Before starting to implement the subscriptions, let’s take a brief moment to understand the subscription API provided by Prisma since that’s the API you’ll be piggybacking with Prisma bindings.</p><p>In general, Prisma lets you <em>subscribe</em> to three different kinds of events (per type in your data model). Taking the Post type from this tutorial project as an example, these events are:</p><ul><li>a new Post is <em>created</em></li><li>an existing Post is <em>updated</em></li><li>an existing Post is <em>deleted</em></li></ul><p>The corresponding definition of the Subscription type looks as follows (this definition can be found in /src/generated/prisma.graphql):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58812c7432be0e0f55fdbea9e1e387d4/href">https://medium.com/media/58812c7432be0e0f55fdbea9e1e387d4/href</a></iframe><p>If not further constrained through the where argument, the post subscription will fire for all of the events mentioned above.</p><h4>2.2. Filtering for specific events</h4><p>The where argument allows clients to specify exactly what events they’re interested in. Maybe a client always only wants to receive updates when a Post gets <em>deleted</em> or when a Post where the title contains a specific keyword is <em>created</em>. These kinds of constraints can be expressed using the where argument. The type of where is defined as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c642d39e1fbb6b8b2e3c698d3e0703e9/href">https://medium.com/media/c642d39e1fbb6b8b2e3c698d3e0703e9/href</a></iframe><p>The two examples mentioned above could be expressed with the following subscriptions in the Prisma API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2db0907cab91de1d4147058ec193d90d/href">https://medium.com/media/2db0907cab91de1d4147058ec193d90d/href</a></iframe><h4>2.3. Exploring the selection set of a subscription</h4><p>You now have a good understanding how you can subscribe to the events that interest you. But how can you now ask for the data related to an event?</p><p>The PostSubscriptionPayload type defines the fields which you can request in a post subscription. Here is how that type is defined:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26fe7aca86e3dc3d27cf2203dc47b5e3/href">https://medium.com/media/26fe7aca86e3dc3d27cf2203dc47b5e3/href</a></iframe><p>Let’s discuss each of these fields in a bit more detail.</p><p><strong>2.3.1 </strong><strong>mutation: MutationType!</strong></p><p>MutationType is an enum with three values:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ab2bbe81635b58ff9f675fd2f8a8ee3/href">https://medium.com/media/8ab2bbe81635b58ff9f675fd2f8a8ee3/href</a></iframe><p>The mutation field on the PostSubscriptionPayload type therefore carries the information what <em>kind</em> of mutation happened.</p><p><strong>2.3.2 </strong><strong>node: Post</strong></p><p>This field represents the Post element which was <em>created</em>, <em>updated</em> or <em>deleted</em> and allows to retrieve further information about it.</p><p>Notice that for DELETED-mutations, node will always be null. If you need to know more details about the Post that was deleted, you can use the previousValues field instead (more about that soon).</p><blockquote><strong>Note</strong>: The terminology of a <strong>node</strong> is sometimes used in GraphQL to refer to single elements. A node essentially corresponds to a <strong>record</strong> in the database.</blockquote><p><strong>2.3.3 </strong><strong>updatedFields: [String!]</strong></p><p>One piece of information you might be interested in for UPDATED-mutations is which <em>fields</em> have been updated with a mutation. That’s what the updatedFields field is used for.</p><p>Assume a client has subscribed to the Prisma API with the following subscription:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f43bd02b0e8d952222505db819baca58/href">https://medium.com/media/f43bd02b0e8d952222505db819baca58/href</a></iframe><p>Now, assume the server receives the following mutation to update the title of a given Post:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e737fc889ed993f2a5a67d1f13b13870/href">https://medium.com/media/e737fc889ed993f2a5a67d1f13b13870/href</a></iframe><p>The subscribed client will then receive the following payload:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05b2198e7827ec9663a333e31b4413d5/href">https://medium.com/media/05b2198e7827ec9663a333e31b4413d5/href</a></iframe><p>This is because the mutation only updated the Post’s title field - nothing else.</p><p><strong>2.3.4 </strong><strong>previousValues: PostPreviousValues</strong></p><p>The PostPreviousValues type looks very similar to Post itself:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39840690bfce8c6515c1cc7f16725903/href">https://medium.com/media/39840690bfce8c6515c1cc7f16725903/href</a></iframe><p>It basically is a <em>helper</em> type that simply mirrors the fields from Post.</p><p>previousValues is only used for UPDATED- and DELETED-mutations. For CREATED-mutations, it will always be null (for the same reason that node is null for DELETED-mutations).</p><p><strong>2.3.5 Putting everything together</strong></p><p>Consider again the sample updatePost-mutation from the section <a href="https://blog.graph.cool/tutorial-building-a-realtime-graphql-server-with-subscriptions-2758cfc6d427#bf6a"><strong>2.3.3</strong></a>. But let’s now assume, the subscription query includes <em>all</em> the fields we just discussed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/892c070c39448d2be0e822764f603bae/href">https://medium.com/media/892c070c39448d2be0e822764f603bae/href</a></iframe><p>Here’s what the payload will look like that the server pushes to the client after it performed the mutation from before:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fc2bc5bd55085a55fff8170a5dc0bbe/href">https://medium.com/media/6fc2bc5bd55085a55fff8170a5dc0bbe/href</a></iframe><p>Note that this assumes the updated Post had the following title before the mutation was performed: “GraphQL servers are best built with conventional ORMs”.</p><h3>3. Add the `publication` subscription</h3><p>Equipped with the knowledge about the Prisma’s subscription API, you’re now ready to consume precisely that API to implement your own subscriptions on the application layer. Let’s start with the subscription that should fire when a new Post is <em>created</em> or the title of an existing Post is <em>updated</em>.</p><h4>3.1. Extend the application schema</h4><p>The first step is to extend the GraphQL schema of your application layer and add the corresponding subscription definition.</p><p>Open schema.graphql and add the following Subscription type to it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4a2443b00191ab180107d2ba44bc301/href">https://medium.com/media/a4a2443b00191ab180107d2ba44bc301/href</a></iframe><p>The referenced PostSubscriptionPayload is directly taken from the Prisma GraphQL schema. It thus also needs to be imported at the top of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14d9abc56aae13a4db2cdde9c44b6f35/href">https://medium.com/media/14d9abc56aae13a4db2cdde9c44b6f35/href</a></iframe><blockquote><strong>Note:</strong> The comment-based import syntax is used by the <a href="https://github.com/graphcool/graphql-import">graphql-import</a> package. As of today, GraphQL SDL does not have an official way to import types across files. <a href="https://github.com/graphql/graphql-wg/blob/master/notes/2018-02-01.md#present-graphql-import">This might change soon</a>.</blockquote><h4>3.2. Implement the subscription resolver</h4><p>Similar to queries and mutations, the next step when adding a new API feature is to implement the corresponding <em>resolver</em>. Resolvers for subscriptions however look a bit different.</p><p>Instead of providing only a single resolver function to resolve a subscription operation from your schema definition, you provide an <em>object </em>with at least one field called subscribe. This subscribe field is a function that returns an <a href="https://jakearchibald.com/2017/async-iterators-and-generators/">AsyncIterator</a>. That AsyncIterator is used to return the values for each individual event. Additionally, you might provide another field called resolve that we’ll discuss in the next section — for now let’s focus on subscribe.</p><p>Update the resolvers object in index.js to now also include Subscription:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24850b85829ae2bb044786c2389491cd/href">https://medium.com/media/24850b85829ae2bb044786c2389491cd/href</a></iframe><p>Prisma bindings are doing the work for you here since db.subscription.post(...) returns the AsyncIterator that emits a new value upon every event on the Post type.</p><p>Note that you’re specifically filtering for CREATED- and UPDATED-mutations to ensure the publications subscription only fires for those events.</p><h4>3.3. Test the subscription</h4><p>For testing the subscription, you need to start the server and open up a Playground which you can do by running yarn dev in your terminal.</p><p>In the Playground that opened, run the following subscription:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bee282696dceadd9de65c5e7d0678c4/href">https://medium.com/media/5bee282696dceadd9de65c5e7d0678c4/href</a></iframe><blockquote><strong>Note: </strong>The GraphQL Playground sometimes shows this <a href="https://github.com/graphcool/graphql-playground/issues/646">bug</a> where the subscription directly returns a payload of null. If this happens to you, try this <a href="https://github.com/graphcool/graphql-playground/issues/646#issuecomment-382614189">workaround</a>.</blockquote><p>Once the subscription is running, you’ll see a loading indicator in the response pane and the <strong>Play</strong>-button turns into a red <strong>Stop</strong>-button for you to stop the subscription.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9fDpnhWdRrgN-vxB3HmLeQ.png" /></figure><p>You can now open another tab and send a mutation to trigger the subscription:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fc099a6572a7728b513dfd9c2eb9d43/href">https://medium.com/media/5fc099a6572a7728b513dfd9c2eb9d43/href</a></iframe><p>Navigating back to the initial tab, you’ll see that the subscription data now appeared in the response pane 🙌</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c6bexq2J0bTeT5UioGjl_A.png" /></figure><p>Feel free to play around with the updateTitle mutation as well.</p><h3>4. Add the `postDeleted` subscription</h3><p>In this section, you’ll implement a subscription that fires whenever a Post gets <em>deleted</em>. The process will be largely similar to the publications resolver, except that you’re now going to return just the deleted Post instead of an object of type PostSubscriptionPayload.</p><h4>4.1. Extend the application schema</h4><p>The first step, as usual when adding new features to a GraphQL API, is to express the new operation as a <a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e#9e63"><em>root field</em></a> in the GraphQL schema.</p><p>Open /src/schema.graphql and adjust the Subscription type to look as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5a2a232c8a4da1079330f4ec4d03b03/href">https://medium.com/media/d5a2a232c8a4da1079330f4ec4d03b03/href</a></iframe><p>Instead of returning the PostSubscriptionPayload for postDeleted, you simply return the Post object that was deleted.</p><h4>4.2. Implement the subscription resolver</h4><p>In section <strong>3.2.</strong>, we briefly mentioned that the object that you use to implement subscription resolvers can hold a second function called resolve (next to subscribe which is required). In this section, you’re going to use it.</p><p>Here is what the implementations of both subscribe and resolve look like to resolve the postDeleted subscription:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/275f31e464b00248ad78f3f238198fe1/href">https://medium.com/media/275f31e464b00248ad78f3f238198fe1/href</a></iframe><p>The most important thing to realize about combining the subscribe and resolve functions is that the values emitted by the AsyncIterator (which is returned by subscribe) correspond to the payload argument that’s passed into resolve! This means you can use resolve to transform and/or filter the event data emitted by the AsyncIterator according to your needs.</p><p>Note that in this scenario, you’re also passing a <em>hardcoded</em> selection set to the post binding function instead of passing the info object along as you’re doing most of the time. The invocation of the binding function thus corresponds to the following subscription request against the Prisma API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d4f99a236983352549de490315452b4/href">https://medium.com/media/3d4f99a236983352549de490315452b4/href</a></iframe><p>The info object carries the <a href="https://medium.com/@cjoudrey/life-of-a-graphql-query-lexing-parsing-ca7c5045fad8">AST</a> (and therefore the <em>selection set</em>) of the incoming GraphQL operations (queries, mutations and subscriptions alike). In this case however, the incoming selection set can’t be applied to the post subscription from the Prisma API. The reasons for that are the following:</p><ul><li>The return type of the incoming subscription is simply Post as you defined in schema.graphql.</li><li>The return type of the post subscription from the Prisma GraphQL API is PostSubscriptionPayload.</li></ul><p>This means the incoming info object does not match the shape that would be required for the post subscription. Hence, you’re specifying the selection set for the post subscription manually as a string.</p><blockquote>This is a bit tricky to understand at first. If you have trouble following right now, be sure to check out <a href="https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a">this</a> technical deep-dive about the info object and its role within GraphQL resolvers.</blockquote><p>In fact, this situation is not ideal either since for types with many fields, this approach can quickly get out of hand. Also, it might be that the incoming subscription doesn’t request <em>all</em> the fields of a type, so you’re <em>overfetching</em> at this point. The best solution would be to manually retrieve the requested fields from the info object and pass those along to the post subscription as described <a href="https://github.com/graphql-binding/graphql-binding/issues/85">here</a>.</p><p>In any case, by hardcoding the selection you’re guaranteed that the payload argument for resolve has the following structure:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3162aba2f0cd124b6d7ea167b5f097e/href">https://medium.com/media/b3162aba2f0cd124b6d7ea167b5f097e/href</a></iframe><p>That’s why inside resolve you can simply return payload.post.previousValues and what you get is an object that adheres to the structure of the Post type 💡(Note that checking for payload with the <em>ternary operator</em> is just a sanity check to ensure it’s not undefined, since this might break the subscription.)</p><h4>4.3. Test the subscription</h4><p>Before testing the new subscription, you need to restart the server to ensure your changes get applied to the API. You can kill the server by pressing <strong>CTRL+C</strong> and then restart it using the yarn dev command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TisP6j41cu4itqI__dDE-w.png" /></figure><p>Once the subscription is running, you can send the following mutation (you need to replace the __POST_ID__ placeholder with the id of an actual Post from your database):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/300ec57d8ab528540d406d84e62bb73f/href">https://medium.com/media/300ec57d8ab528540d406d84e62bb73f/href</a></iframe><p>Navigating back to the subscription tab, you’ll see that the id and title have been pushed in the response pane, as requested by the active subscription.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1vMUL8w54yIAxOPVSR0UjQ.png" /></figure><h3>Summary</h3><p>In this tutorial, you learned how to add realtime subscriptions to a GraphQL API using <a href="https://www.prisma.io/">Prisma</a> and <a href="https://github.com/graphcool/prisma-binding">Prisma bindings</a>.</p><p>Similar to implementing queries and mutations with Prisma, you are piggybacking on Prisma’s GraphQL API, leaving the heavy-lifting of database access and pub/sub logic to the powerful Prisma query engine.</p><p>If you want to play around with the project yourself, you can check out the final result of the tutorial on <a href="https://github.com/nikolasburk/subscriptions">GitHub</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F38bbbd%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F38bbbd%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F38bbbd%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href">https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2758cfc6d427" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/tutorial-building-a-realtime-graphql-server-with-subscriptions-2758cfc6d427">Tutorial: Building a Realtime GraphQL Server with Subscriptions</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to deploy a Prisma cluster to AWS Fargate using Docker & CloudFormation]]></title>
            <link>https://medium.com/graphcool/how-to-deploy-a-prisma-cluster-to-aws-fargate-using-docker-cloudformation-293aa8727b89?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/293aa8727b89</guid>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[fargate]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Thu, 05 Apr 2018 16:19:07 GMT</pubDate>
            <atom:updated>2018-05-17T09:35:56.780Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to use the new AWS ECS container service Fargate to deploy a Prisma Cluster with a connected database (Tutorial)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6kZO8kBIc4hQPRYS-Q47nQ.jpeg" /></figure><p><a href="https://www.prisma.io/docs/reference/clusters/overview-eu2ood0she">Prisma clusters</a> provide the runtime environment for your Prisma services. In this tutorial, you’re going to learn how to deploy a Prisma cluster to <a href="https://aws.amazon.com/fargate/">AWS Fargate</a>. The cluster will be backed by a MySQL database which you’re going deploy to AWS RDS first.</p><blockquote>AWS Fargate is a new tool for Amazon ECS and EKS that allows you to run <a href="http://aws.amazon.com/what-are-containers">containers</a> without having to manage servers or clusters.</blockquote><h3>Overview</h3><p>When building GraphQL servers with Prisma, there are three backend components that need to be deployed:</p><ul><li>Your GraphQL server (commonly implemented with <a href="https://github.com/graphcool/graphql-yoga">graphql-yoga</a>)</li><li>Your Prisma cluster &amp; services</li><li>Your database</li></ul><p>Here is a high-level diagram indicating the architecture of your backend:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*p17uapNIac8Grmm8r7wp6Q.png" /></figure><p>In previous tutorial on this blog, you learned how to deploy the GraphQL server using <a href="https://blog.graph.cool/deploying-graphql-servers-with-zeit-now-85f4757b79a7">Zeit Now</a> and <a href="https://blog.graph.cool/deploying-graphql-servers-with-apex-up-522f2b75a2ac">Apex Up</a>. Today, you’re going to learn how to deploy a Prisma cluster using <a href="https://aws.amazon.com/fargate/">AWS Fargate</a>. Since every Prisma cluster is backed by a database, you’ll also learn how to deploy a database along with it.</p><p>You can deploy to Fargate in several ways. For this tutorial, you’re going to use preconfigured <a href="https://aws.amazon.com/cloudformation/">CloudFormation</a> templates and the AWS Console GUI.</p><h3>1. Deploying a MySQL database to RDS</h3><p>While there are many ways to deploy databases with RDS (e.g. using the AWS GUI), you’re going to use a CloudFormattion template for this tutorial.</p><h4>1.1. Getting the CloudFormation template</h4><p>You can obtain the CloudFormation template for the MySQL database from our <a href="https://github.com/graphcool/database-templates">database-templates</a> GitHub repository. Simply clone or download the entire repo. It currently contains templates for MySQL (<a href="https://github.com/graphcool/database-templates/blob/master/aws/mysql.yml">aws/mysql.yml</a>) and AWS Aurora (<a href="https://github.com/graphcool/database-templates/blob/master/aws/aurora.yml">aws/aurora.yml</a>).</p><h4>1.2. Creating your CloudFormation stack</h4><p>Next, you’re going to create the CloudFormation stack for the MySQL database based on the template you just downloaded. Navigate to <a href="https://console.aws.amazon.com/cloudformation/">https://console.aws.amazon.com/cloudformation/</a> and login (if necessary).</p><p><strong>Attention:</strong> Fargate is currently only available in the <strong>US East (N. Virginia)</strong> region. So be sure to select this region in the top-right corner of the AWS console!</p><p>Once you have the <strong>US East (N. Virginia)</strong> region selected, click the <strong>Create Stack </strong>button. On the next screen, you then need to provide your CloudFormation template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5YhGCnRB5d9PLoBnKsRXNw.png" /></figure><p>Click the <strong>Choose File</strong> button and select the mysql.yml file from the location where you previously downloaded the <a href="https://github.com/graphcool/database-templates">database-templates</a> repo. Then click <strong>Next</strong>.</p><blockquote><strong>Note:</strong> If you’d like the aurora.yml template instead, you’ll need to create a service linked tole in IAM using this CLI command: aws iam <a href="http://docs.aws.amazon.com/cli/latest/reference/iam/create-service-linked-role.html">create-service-linked-role</a> — aws-service-name ecs.amazonaws.com . You can find more info about this <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using-service-linked-roles.html">here</a>. Thanks to <a href="https://medium.com/u/930a302f98a3">John Walsh</a> for figuring that out!</blockquote><p>On the next screen, you need to provide the <strong>Stack name</strong> as well as a <strong>Database Password</strong>. You can choose anything you like for that — note that the <strong>Database Password</strong> needs to be at least 8 characters long.</p><p>For this tutorial, we’re choosing the following values:</p><ul><li><strong>Stack name: </strong>my-mysql-db</li><li><strong>Database Password</strong>: Prisma123 (be sure to pick a secure password and note it down, you’ll need in the next section)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kgag1gFM31blWqGs2VNtIQ.png" /></figure><p>You can leave the remaining parameters as they are and click <strong>Next</strong>.</p><p>On the next screen, you don’t need to configure anything and can directly click <strong>Next</strong> again.</p><p>On the last screen, go ahead and click the <strong>Create</strong> button to launch the stack.</p><h4>1.3. Wait ️️️️☕️</h4><p>That’s it for the DB setup — you can now go and grab a coffee, it usually takes around 15 minutes until the stack is available.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b7kdadnpKB9s5ptF1pSm2g.png" /></figure><p>While you’re waiting, here are a few articles you might find interesting 😏</p><ul><li><a href="https://blog.graph.cool/how-to-wrap-a-rest-api-with-graphql-8bf3fb17547d">How to wrap a REST API with GraphQL</a></li><li><a href="https://blog.graph.cool/reusing-composing-graphql-apis-with-graphql-bindings-80a4aa37cff5">Reusing &amp; Composing GraphQL APIs with GraphQL Bindings</a></li><li><a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e">GraphQL Server Basics: GraphQL Schemas, TypeDefs &amp; Resolvers Explained</a></li></ul><h4>1.4. Save database details</h4><p>When setting up the Prisma cluster in the next section, you’ll need to connect it to the database which you just deployed. You will do so by providing a <em>host</em> and <em>port</em>.</p><p>Both pieces of information can be retrieved from the <strong>Outputs</strong> tab that you can navigate to once you selected the my-mysql-db in the stack list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LxY7E4Gp74adGNvAUhtLsA.png" /></figure><p>Save the <strong>DatabasePort</strong> and <strong>DatabaseEndpoint</strong> as you’ll need them in the next section.</p><h3>2. Deploying a Prisma cluster to Fargate</h3><h4>2.1. Getting the CloudFormation template</h4><p>The Prisma cluster is deployed via a CloudFormation template as well. So, just like before you first need to get access to that template.</p><p>You can find it in the <a href="https://github.com/graphcool/prisma-templates">prisma-templates</a> GitHub repository. Clone or download that repo so you can use provide the template in the next step.</p><h4>2.2. Creating your CloudFormation stack</h4><p>Like in the previous section, navigate your browser to <a href="https://console.aws.amazon.com/cloudformation/">https://console.aws.amazon.com/cloudformation</a> and click the <strong>Create Stack</strong> button (be sure to have the <strong>US East (N. Virginia)</strong> region selected in the top-right corner).</p><p>Next, select the fargate.yml template you downloaded in the previous step. Then move on to the following screen by clicking <strong>Next</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j1xcd7SDYdIZIDqZe40vdg.png" /></figure><p>Here you need to provide some additional information about your Prisma cluster, e.g. which database it should connect to. Again, for the <strong>Stack name </strong>you can choose anything you like, for this tutorial we’re using my-prisma-cluster.</p><p><strong>2.2.1. Generate key-pair and set the ClusterPublicKey parameter</strong></p><p>The <strong>ClusterPublicKey</strong> is used to ensure only authorized people can deploy services to your Prisma cluster. The private counterpart of this key will be used by the CLI when running prisma deploy (and any other commands that are targeting your cluster).</p><p>There are several ways to generate a public/private-key-pair. In this tutorial, you’re going to use the Prisma Cloud API. This is<em> purely for convenience</em>, feel free to choose any other tool to generate your key-pair (such as <a href="https://www.openssl.org/">openssl</a>).</p><p>Navigate your browser to <a href="https://api.cloud.prisma.sh/">https://api.cloud.prisma.sh</a> and run the following query in the Playground:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5818eafd5a0591c06ce333ccafc9ff8/href">https://medium.com/media/e5818eafd5a0591c06ce333ccafc9ff8/href</a></iframe><p>The response to this query will be a JSON object that carries a private as well as a public key. Store the private key securely, you’ll need it in a bit.</p><p>Then copy the entire public key and paste it into the <strong>ClusterPublicKey</strong> field in the AWS Console.</p><p><strong>2.2.2. Provide database connection details</strong></p><p>Next, you need to ensure that the Prisma cluster knows which database it can use to store the data belonging to the Prisma services. This will of course be the database that you deployed in the previous section.</p><p>Now it’s time to reuse the data from step <strong>1.4.:</strong> Copy the <strong>DatabaseEndpoint</strong> from before and paste it into the <strong>DbHost</strong> field. It should look somewhat similar to this: prisma.cvg9pd7kwxj1.us-east-1.rds.amazonaws.com.</p><p>The <strong>DbPassword</strong> is the <strong>Database Password </strong>you specified in step <strong>1.2.</strong>. Assuming you haven’t changed anything, that’s Prisma123.</p><p>If you previously haven’t changed anything about the port, the <strong>DbPort</strong> is already correctly set to 3306.</p><p>Finally, the <strong>DbUser </strong>is just called prisma (unless you specified a different value for <strong>Database Username </strong>value in step <strong>1.2.)</strong>.</p><p><strong>2.2.3. Specifying the PrismaVersion parameter (must be at least 1.6.)</strong></p><p>Lastly, you need to provide the <strong>PrismaVersion </strong>which refers to the version of the Prisma Docker image you want to use.</p><blockquote><strong>Attention:</strong> Fargate deploys only work with Prisma 1.6. and greater. At the time of writing this tutorial Prisma 1.6. is still in beta — this doesn’t affect the deploy.</blockquote><p>Select the 1.6-beta (or some later) version from the dropdown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BRHpAa4gOxZRDmDJueGYLw.png" /></figure><p>With all that information in place, go ahead and click <strong>Next</strong> to move on.</p><p><strong>2.2.4. Launch the stack</strong></p><p>You don’t need any configurations on the next screen, so you can just skip it by clicking <strong>Next</strong> again.</p><p>On the last screen, you only need to check the checkbox at the bottom of the screen, confirming the following statement: <strong>I acknowledge that AWS CloudFormation might create IAM resources.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-pvVk6ypmWhRVfF5h8pyPw.png" /></figure><p>Finally, click the <strong>Create</strong> button to launch the stack.</p><h4>2.3. Wait ☕️</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F88EvfARM1YaCQ%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Flittle-rascals-our-gang-spanky-88EvfARM1YaCQ&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F88EvfARM1YaCQ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="287" frameborder="0" scrolling="no"><a href="https://medium.com/media/23bf55136a3dd2a9eeeeb28b17e703d6/href">https://medium.com/media/23bf55136a3dd2a9eeeeb28b17e703d6/href</a></iframe><h4>2.4. Save the cluster endpoint</h4><p>Once the stack has been launched, you need to save the endpoint of the cluster.</p><p>Similar to what you did with the database stack before, first select the my-prisma-cluster stack from the list and then open the <strong>Outputs</strong> tab for it. Then, go ahead and save the value for the <strong>ExternalUrl</strong> field, you will need it in the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q7rGd5dyfC5cQYHmdnDQVA.png" /></figure><h3>3. Configuring the deployed cluster in the Prisma CLI</h3><p>To be able to deploy Prisma services to your new cluster, the Prisma CLI that’s installed on your machine needs to be aware of it.</p><p>You can use the <a href="https://www.prisma.io/docs/reference/cli-command-reference/clusters/prisma-cluster-add-quungoogh8">prisma cluster add</a> command to provide the information the CLI needs to work with the new cluster in the future.</p><blockquote><strong>ATTENTION:</strong> This tutorial is based on Prisma 1.6.3. Note that the Prisma <a href="https://www.prisma.io/docs/reference/upgrade-guides/upgrading-prisma/upgrade-to-1.7-iquaecuj6b">1.7release</a> introduced a few major changes which are not yet applied in this tutorial. Thus, please make sure to install the 1.6.3 version of the Prisma CLI with the following command: npm install -g prisma<strong>@1.6.3</strong></blockquote><p>Run the prisma cluster add to tell the CLI about your new cluster! The CLI will prompt you to provide three pieces of information:</p><ul><li><strong>Cluster endpoint:</strong> This is the <strong>ExternalURL </strong>from the step <strong>2.4.</strong> It looks similar to this: <a href="http://my-pr-Publi-1GXX8QUZU3T89-433349553.us-east-1.elb.amazonaws.com">http://my-pr-Publi-1GXX8QUZU3T89-433349553.us-east-1.elb.amazonaws.com</a>.</li><li><strong>Cluster secret: </strong>This is the private key from step <strong>2.2.1</strong>.</li><li><strong>Cluster name</strong>: This can be anything that describes this cluster. For this tutorial, we’re using fargate-cluster.</li></ul><p>The CLI now added a new entry with that information to your <a href="https://www.prisma.io/docs/reference/clusters/overview-eu2ood0she#cluster-registry">cluster registry</a> (.prisma/config.yml). This file is located in your home directory and contains a list of clusters you can deploy your services to.</p><p>With the new cluster entry, it looks similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f318a913521f0fac14152ac554aecfc/href">https://medium.com/media/8f318a913521f0fac14152ac554aecfc/href</a></iframe><h3>4. Deploy a Prisma service to the new cluster</h3><p>It’s time to put your new cluster in use and deploy a service to it. In a location of your choice, run the following command in the terminal:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23403510beff2dc68679b4c3d8e36e68/href">https://medium.com/media/23403510beff2dc68679b4c3d8e36e68/href</a></iframe><p>This prompts you to select a template for the Prisma service. You’re only interested in deploying a Prisma service right now (as opposed to an entire GraphQL server or a fullstack app), so just choose <strong>Minimal setup: database-only</strong>.</p><p>After that selection, the CLI created a new directory called my-prisma-service with your project files. Navigate into it and run prisma deploy:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08a6c6ea2f71e732bdf374d7a7abcc52/href">https://medium.com/media/08a6c6ea2f71e732bdf374d7a7abcc52/href</a></iframe><p>The CLI now prompts you with a list of available clusters, among them the new fargate-cluster. Select it from the list and hit <strong>Enter</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DeHMfzjdM8DRZP66LIHC-A.png" /></figure><p>After you selected the cluster, the Prisma service is deployed to it. Notice also that the CLI added the cluster: fargate-cluster entry to your prisma.yml file. For all future deploys, the CLI will target this cluster by default. To bring up the prompt again, simply remove the entry from prisma.yml.</p><p>After the command has finished running, the CLI outputs the URL of your Prisma service. You can now test the Prisma services which is running on your very own Prisma cluster on AWS Fargate 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iRGtCZwDjkogYFTA6leOLg.png" /></figure><h3>Summary</h3><p>In this tutorial, you learned how to deploy a Prisma cluster with a backing database to AWS Fargate. In both cases, you were using preconfigured CloudFormation templates for the deployment process.</p><blockquote>If you don’t want to fiddle with CloudFormation and the AWS GUI yourself, you can also use <a href="https://blog.graph.cool/introducing-prisma-cloud-a-graphql-database-platform-ed591baa8737">Prisma Cloud</a> to easily spin up your own Prisma Clusters.</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F38bbbd%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F38bbbd%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F38bbbd%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href">https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=293aa8727b89" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/how-to-deploy-a-prisma-cluster-to-aws-fargate-using-docker-cloudformation-293aa8727b89">How to deploy a Prisma cluster to AWS Fargate using Docker &amp; CloudFormation</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tutorial: Render Props in React Apollo 2.1]]></title>
            <link>https://medium.com/graphcool/tutorial-render-props-in-react-apollo-2-1-199e9e2bd01e?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/199e9e2bd01e</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Tue, 27 Mar 2018 17:19:55 GMT</pubDate>
            <atom:updated>2018-03-27T18:07:24.054Z</atom:updated>
            <content:encoded><![CDATA[<h4>A new version of the React Apollo client has been released. Learn how to use the new API in this fullstack tutorial.</h4><p><a href="https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce">Render props</a> have seen a big hype in the React community lately. Last week, Apollo introduced a new version of their React client, now making use of this new pattern that allows to reuse code among React components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*01zrjJOgHLdycQn7OO2QgA.png" /></figure><p>In this fullstack tutorial, you will rebuild the <a href="https://github.com/graphql-boilerplates/react-fullstack-graphql/tree/master/basic">React &amp; GraphQL fullstack boilerplate</a> project and learn how to use Apollo’s new API. You can find the final version of the code on <a href="https://github.com/nikolasburk/react-apollo-tutorial">GitHub</a>.</p><p>The app you are going to build is a simple blogging application with the following features:</p><ul><li>Viewing a feed of published posts</li><li>Viewing unpublished drafts</li><li>Creating new drafts</li><li>Publishing drafts so they become visible in the feed</li><li>Deleting unpublished drafts and published posts</li></ul><h3>What are render props?</h3><h4>Overview</h4><p>Render props are a pattern to share code between React components using <em>a prop whose value is a function</em>.</p><p>From the <a href="https://reactjs.org/docs/render-props.html">official documentation</a>: “A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render logic.” Here is a simple example of what the usage of a render prop function might look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/898d3e68409bd3e26b82e911a99f6f05/href">https://medium.com/media/898d3e68409bd3e26b82e911a99f6f05/href</a></iframe><h4>Render props vs Higher-order components</h4><p>In general, render props enable code reuse and therefore are often used as an alternative to React’s <a href="https://reactjs.org/docs/higher-order-components.html">higher-order components</a> (HOCs). It should also be noted that while React Apollo 2.1. introduces this new render props API, you don’t <em>have</em> to use it. You might very well keep on using the good ol’ <a href="https://www.apollographql.com/docs/react/api/react-apollo.html#graphql">graphql</a> HOC the same way you did before. So, when to use which?</p><h3>MJ on Twitter</h3><p>@reactjs I can do anything you&#39;re doing with your HOC using a regular component with a render prop. Come fight me.</p><p>Generally, the new render prop components Query, Mutation and Subscription tend to be simpler and more straightforward to use than the HOC counterpart. This is mostly because they are being used like any other React component and can just be included in your JSX code with corresponding tags (e.g. &lt;Query&gt;). Higher-order components always require another level of indirection in that your React components need to be wrapped with the HOC function. This can be less intuitive, especially for newcomers.</p><p>Because of that, they also lend themselves for simple use cases, e.g. where one React component depends on a single query and mutation and therefore easily can be wrapped inside a Query or Mutation render prop component.</p><p>Another great use case where the render props components might come in handy is when a component uses multiple queries that depend on each other. This can easily be implemented by nesting the &lt;Query&gt; components inside each other.</p><p>If you’re already comfortable with using HOCs you might not feel the need to use the new render props API. In the end, it very much depends on your personal preference as both the graphql HOC and the new render props component provide the same functionality. Consider the new API as another tool in your toolbox to help structure your application in the way you like.</p><blockquote>This article is not about render props per se. If you want to learn more about them and why many developer prefer using render props over HOCs, make sure to read <a href="https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce">this</a> article by <a href="https://medium.com/u/7f9567e0d71c">Michael Jackson</a>.</blockquote><h3>1. Getting started</h3><p>In this section, you’ll prepare everything to get started with the new render props API of React Apollo 2.1. If you don’t want to actually follow the tutorial but only want to <em>read</em> about the new render props API, feel free to <a href="https://blog.graph.cool/tutorial-render-props-in-react-apollo-2-1-199e9e2bd01e#5025">skip ahead</a>.</p><h4>1.1. Download the starter code</h4><p>To kick this tutorial off, you first need to download the starter code for it. Open your terminal and run the following command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cef628910e0e486feab6a692b3a0225/href">https://medium.com/media/0cef628910e0e486feab6a692b3a0225/href</a></iframe><p>This downloads the code from the starter branch of <a href="http://github.com/nikolasburk/react-apollo-tutorial">this</a> GitHub repository and puts it into a new directory called react-apollo-tutorial-starter.</p><h4>1.2. Exploring the frontend app</h4><p>Feel free to make yourself familiar with the codebase. You can start the app by running yarn start inside the react-apollo-tutorial-starter directory. Don’t forget to install the dependencies before by running yarn install.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*_EjTITNbwXGCVc7f5MA7tQ.png" /></figure><p>The directory you downloaded already contains the entire UI for the app, but there’s no actual <em>functionality</em> because that all depends on the backend.</p><h4>1.3. Exploring the GraphQL server</h4><p>Speaking of the backend, the code for the GraphQL server is located inside the server directory. Check out server/src/schema.graphql to see the <a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e">GraphQL schema</a> of the app and learn what API operations are supported.</p><p>You can start the server by running yarn start inside the server directory (again, don’t forget running yarn install before). Once the server is running, you can open a <a href="https://github.com/graphcool/graphql-playground">GraphQL Playground</a> under the URL <a href="http://localhost:4000">http://localhost:4000</a> (this also is the endpoint your frontend will connect to) and use it send to queries and mutations to your server.</p><blockquote>To learn how you can deploy the GraphQL server to the web, check out the previous tutorials on our blog. There are tutorials for the one-click deployment tools <a href="https://blog.graph.cool/deploying-graphql-servers-with-zeit-now-85f4757b79a7">Zeit Now</a> and <a href="https://blog.graph.cool/deploying-graphql-servers-with-apex-up-522f2b75a2ac">Apex Up</a>.</blockquote><p>If you’ve opened a Playground right now, you’ll see an error though:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wBWEMQXeDUu6JZBN44ZGUg.png" /></figure><p>This is because the GraphQL server depends on a Prisma service as its database layer, but you haven’t deployed that Prisma service yet. So, that’s what you’ll do next.</p><h4>1.4. Setting up Prisma as the database layer for your GraphQL server</h4><p>To deploy the Prisma service, all you need to is navigate into the server directory and use the Prisma CLI to deploy the service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51a37b6682c63c6a5bd4780b3a68837e/href">https://medium.com/media/51a37b6682c63c6a5bd4780b3a68837e/href</a></iframe><p>If you have the Prisma CLI installed globally on your machine (which you can do with npm install -g prisma), you can omit the yarn prefix which invokes a script from package.json and simply run prisma deploy instead.</p><p>After you ran this command, the CLI prompts you to select a <em>cluster</em> to which the Prisma service can be deployed. For the purpose of this tutorial, the easiest is to select a <em>development cluster</em> that is free and doesn’t require to create a <a href="https://blog.graph.cool/introducing-prisma-cloud-a-graphql-database-platform-ed591baa8737">Prisma cloud</a> account. When prompted by the CLI, simply select prisma-eu1 or prisma-us1 as the target cluster. (If you have Docker installed, you can also deploy to a local cluster.)</p><p>After the command has finished, it outputs the HTTP endpoint of your Prisma service. It will look somewhat similar to this: https://eu1.prisma.sh/public-warpcrow-598/blogr/dev where public-warpcrow-598 is a randomly generated ID that will look different for you.</p><p>Note that this command also seeded some initial data in your database, based on the mutation defined in <a href="https://github.com/nikolasburk/react-apollo-tutorial/blob/starter/server/database/seed.graphql">server/database/seed.graphql</a>.</p><p>The last step is to take this endpoint and paste it into src/server/index.js where the Prisma binding instance is created, replacing the current placeholder __PRISMA_ENDPOINT__:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daeeb92d028ee45c4ae12bfcabab15e9/href">https://medium.com/media/daeeb92d028ee45c4ae12bfcabab15e9/href</a></iframe><p>That’s it! Your GraphQL server is now backed by a database and fully functional, so you can start sending queries and mutations in the Playground.</p><p>Here is a quick overview of the architecture that’s used for this app:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rzI7PLKuJuRjjBIxoSiboQ.png" /></figure><blockquote>If you want to learn how to build the GraphQL server that’s used as a backend in this tutorial, you can read <a href="https://blog.graph.cool/tutorial-how-to-build-a-graphql-server-with-graphql-yoga-6da86f346e68">this</a> tutorial.</blockquote><h4>1.5. Install React Apollo dependencies in the frontend app</h4><p>With the starter project and Prisma service in place, the next step is to install the dependencies that are required for Apollo. In your terminal, navigate back into the project’s root directory and install the dependencies there:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a80d63af4a5976982c7c24cf9d3927f/href">https://medium.com/media/4a80d63af4a5976982c7c24cf9d3927f/href</a></iframe><p>From the <a href="https://www.apollographql.com/docs/react/essentials/get-started.html#installation">Apollo documentation</a>, here is what each of the dependencies are being used for:</p><ul><li>apollo-boost: Contains everything you need to set up Apollo Client</li><li>react-apollo: View layer integration for React</li><li>graphql-tag: Necessary for parsing your GraphQL queries</li><li>graphql: Also parses your GraphQL queries</li></ul><p>Note that <a href="https://github.com/apollographql/apollo-client/tree/master/packages/apollo-boost">apollo-boost</a> is a wrapper package that lets you get started quickly with Apollo Client without much configuration overhead.</p><h4>1.6. Initialize `ApolloClient` and wrap the app with `ApolloProvider`</h4><p>At this point, you can start writing actual code! 🙌 The first thing you need to do is connecting your frontend with the backend by creating an ApolloClient instance with the endpoint of your GraphQL server.</p><p>Open src/index.js and type the following line right after the import statements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3932acfcf6140b8f185e090108d4180/href">https://medium.com/media/f3932acfcf6140b8f185e090108d4180/href</a></iframe><p>As mentioned before, ApolloClient will connect to the GraphQL server that’s running locally on port 4000.</p><p>Next, wrap the entire JSX code inside ReactDOM.render with an ApolloProvider component which receives the client instance you just created as a prop:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d9ddd00da820f4530b659a865d75026/href">https://medium.com/media/5d9ddd00da820f4530b659a865d75026/href</a></iframe><p>Thanks to the ApolloProvider, you’ll now be able to use Apollo Client’s functionality inside your app.</p><p>The last thing to do here is import the ApolloClient and ApolloProvider classes from their respective packages. Add the following two lines to the other import statements to the top of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c6d178aa1cb874aec4b15b692275e55/href">https://medium.com/media/7c6d178aa1cb874aec4b15b692275e55/href</a></iframe><h3>2. Loading and displaying drafts with render props and the new `Query` component</h3><p>You’ll start by implementing the functionality for the /drafts route that can be found in the DraftsPage component.</p><h4>2.1. Specifying the `drafts` query</h4><p>To load the drafts from the backend, you need to use the drafts query <a href="https://github.com/nikolasburk/react-apollo-tutorial/blob/master/server/src/schema.graphql#L5">defined in the server’s GraphQL schema</a>.</p><p>Open /src/components/DraftsPage.js and add the following code to the bottom of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/619a3a3d404117ad8c032d2fb56bf9c0/href">https://medium.com/media/619a3a3d404117ad8c032d2fb56bf9c0/href</a></iframe><p>Note that you’re only exporting the query because you’ll need it later in a different file when you’re updating the cache after a mutation.</p><h4>2.2. Loading data with `Query`</h4><p>Next, you will make use of React Apollo new <a href="https://www.apollographql.com/docs/react/essentials/queries.html#basic">Query</a> component to load the data and render it to the screen. Still in DraftsPage.js, wrap everything that’s currently returned by render in this new component. While you’re at it, you can also remove the dummy post data that’s currently used. Here is what the ready component will look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e07deb93a629fde5ee4006b83653d565/href">https://medium.com/media/e07deb93a629fde5ee4006b83653d565/href</a></iframe><p>Let’s quickly understand what’s going on here. By wrapping the component with Query and passing the DRAFTS_QUERY as a prop, it gets access to the result of the network call that’s initiated and managed by Apollo Client. This result contains the data object which again carries the result data for the query. The next thing you need to do is actually use the received data and display it. The code for that is already in place 🙌</p><p>Finally, you need to import the Query component, add the following import statement to the top of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b905ecf9a9c1c17442e27a1d56ba14d/href">https://medium.com/media/0b905ecf9a9c1c17442e27a1d56ba14d/href</a></iframe><p>If you run the app now, the DraftsPage will already load the data from the server. Don’t forget to have the server running (by calling yarn start inside src/server) whenever you want to test the app — otherwise the app misses its backend and won’t work!</p><p>Right now, the drafts page only displays the draft that was seeded initially:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*dgGQ6f1SZcJH7nddNM0Kug.png" /></figure><h4>2.3. Accounting for loading and error states</h4><p>All right, so the now the app load and displays the data. But what happens if the network request fails for some reason? Also, you probably want to display a loading state to your users while the request is ongoing.</p><p>Thanks to Apollo, this functionality is super straightforward. The render prop function not only receives the response data as input arguments, but at the same time also an error object as well as a boolean value loading that is true as long as the server’s response hasn’t been received.</p><p>Simply update the code inside of the Query component to account for these two additional component states:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f2db38575c4d7dd4563c70e5af9ded3/href">https://medium.com/media/9f2db38575c4d7dd4563c70e5af9ded3/href</a></iframe><p>If you’ve worked with earlier versions of Apollo, this API will feel familiar to you. It basically is the same as the one that’s used for the <a href="https://www.apollographql.com/docs/react/api/react-apollo.html#graphql">graphql</a> HOC (where Apollo injects data, loading and error into the props of the component that’s wrapped with graphql). That’s it already for the drafts page!</p><h3>3. Loading the feed</h3><p>The implementation of the <em>feed</em> is analogous to the one for the <em>drafts</em>, except that it uses the <a href="https://github.com/nikolasburk/react-apollo-tutorial/blob/master/server/src/schema.graphql#L4">feed</a> instead of the drafts query. Here is what the implementation looks like (this code needs to be put into FeedPage.js, replacing the entire content in there):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43a09128c2bf984f831ddde33b4ca8ac/href">https://medium.com/media/43a09128c2bf984f831ddde33b4ca8ac/href</a></iframe><h3>4. Creating new drafts with render props and the new `Mutation` component</h3><p>New drafts are created under the /create route which renders the CreatePage component. It shows a simple form with two inputs where the user can provide the title and the text for their new drafts.</p><h4>4.1. Specifying the `createDraft` mutation</h4><p>You’ll start by adding the <a href="https://github.com/nikolasburk/react-apollo-tutorial/blob/master/server/src/schema.graphql#L10">createDraft</a> mutation to the bottom of CreatePage.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b11f4786aee5cf2b5aa4450b8fe8a15f/href">https://medium.com/media/b11f4786aee5cf2b5aa4450b8fe8a15f/href</a></iframe><p>This mutation takes two variables which you’ll pass to it from the component’s state before it is sent to the server.</p><h4>4.2. Writing data with `Mutation`</h4><p>The Query and Mutation components in React Apollo 2.1. are very similar. The core difference is that when wrapping another component with Mutation the render prop function also receives a <em>function</em> which you use the send the mutation to the server.</p><p>Like with the Query component, go ahead and wrap everything that’s returned in render with Mutation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b5579d8fef10e39bd80b0f71ad0312c/href">https://medium.com/media/9b5579d8fef10e39bd80b0f71ad0312c/href</a></iframe><p>The data, loading and error arguments that are being passed into the render prop function have the same semantics as the ones you just saw with the Query component. The very first argument of the function, createDraft, is used to send the CREATE_DRAFT_MUTATION to the server. It is being called in the onSubmit callback of the form element.</p><p>To make this work, import Mutation and gql at the top of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdc091a4f0c778d9c0fadb00c0587ea5/href">https://medium.com/media/bdc091a4f0c778d9c0fadb00c0587ea5/href</a></iframe><p>Now, when running the app and navigating to the /create route, you can submit new drafts that are stored in the database on the server-side:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*iNK392MZ7rLIemoigPtvGA.png" /></figure><p>However, after you clicked the <strong>Create</strong>-button and the app automatically navigated back to the /drafts route, you’ll notice that the page actually hasn’t updated. Only after refreshing the page, the newly created draft will appear 🤔</p><p>The reason for this is that the drafts page only displays already cached data. To fix this, you manually need to update the cache after the mutation was performed.</p><h4>4.3. Updating the cache with the imperative store API</h4><p>Apollo’s <a href="https://www.apollographql.com/docs/react/advanced/caching.html#direct">imperative store API</a> allows to read and write directly from/to the Apollo cache. Again, if you’ve already used the imperative store API in an earlier version of Apollo, the following will feel very familiar to you. The big difference in the new version is that <a href="https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-mutation-options-update">update</a> is not passed as an argument to the function that performs the mutation, but instead passed as a prop to the Mutation component.</p><p>The API of the update function remains the same: It receives an object that serves as an interface to the cache and allows to update it as well as the server’s response.</p><p>Here is how you’ll implement it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c120e0a9d2bc9f612f9fca3884ca7c36/href">https://medium.com/media/c120e0a9d2bc9f612f9fca3884ca7c36/href</a></iframe><p>Inside update, you first extract the previous results of the DRAFTS_QUERY from the cache (here’s also the reason why you previously needed to export it from DraftsPage). Then, you’re using writeQuery to update the the contents of the cache by manually adding the new draft object that was returned by the server (which is stored in data.createDraft).</p><p>Great, when you’re testing the app again, you’ll see that the /drafts page is now updated directly after the mutation was performed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*idRITgkP04XGflQExvz61A.png" /></figure><p>Note that we’re omitting accounting for error and loading states here for the sake of brevity.</p><h3>5. Publishing posts</h3><p>Once a draft is published, it will appear in the app’s feed. The functionality for that is implemented in DetailPage component which displays two buttons for the post that it displays: <strong>Publish</strong> and <strong>Delete</strong></p><h4>5.1. Loading the selected post</h4><p>Whenever a post is clicked (either from the <em>drafts</em> or from the <em>feed</em> page), it will be displayed using the DetailPage component. This also loads the data for the post from the network.</p><p>Just like before, start by adding the query that’s required in this case:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/670787e338829daa81a935f9e76d8e36/href">https://medium.com/media/670787e338829daa81a935f9e76d8e36/href</a></iframe><p>The id variable is passed to the Query component as a prop. It is read from the current URL which contain’s the id of the selected post.</p><p>Here is what the updated render function looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c0d6d66cb971bd0ad09c0aa77b88ef5/href">https://medium.com/media/1c0d6d66cb971bd0ad09c0aa77b88ef5/href</a></iframe><p>To finish this up, you need to import Query and gql again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53f175b2fae1239cda5b6ffbb33b8c05/href">https://medium.com/media/53f175b2fae1239cda5b6ffbb33b8c05/href</a></iframe><p>Now, you can select a draft from the /drafts route and the app will display the corresponding DetailPage for it, including the <strong>Publish</strong>- and <strong>Delete</strong>-buttons:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*ifNy0EWA5EdrqkU0X9LT3Q.png" /></figure><p>These currently don’t work, so let’s implement for them next!</p><h4>5.2. Publishing a draft</h4><p>To publish a draft, you’ll use the <a href="https://github.com/nikolasburk/react-apollo-tutorial/blob/master/server/src/schema.graphql#L12">publish</a> mutation from the server’s GraphQL API. First, add the mutation to DetailPage.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ba23d93903a72936ddf45c46ae753a6/href">https://medium.com/media/2ba23d93903a72936ddf45c46ae753a6/href</a></iframe><p>The buttons are created inside _renderAction, so that’s where you need to add the Mutation component this time. Go ahead and replace the definition of the publishButton variable with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/339d8abe2d106480a3c5645725ac8946/href">https://medium.com/media/339d8abe2d106480a3c5645725ac8946/href</a></iframe><p>This code isn’t using any new concepts. The actual button is wrapped inside a Mutation component which receives the PUBLISH_MUTATION as well as an update function as its props.</p><p>Inside update, the published post first is manually removed from the previously cached results of the DRAFTS_QUERY and then added to the FEED_QUERY.</p><p>Also note that the publish function that’s passed into the render prop function is invoked whenever the button <strong>Publish</strong>-button gets clicked.</p><p>Next, you need to update what’s returned from the _renderAction function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0dad1f6c1cb3399b342721943d69bfb6/href">https://medium.com/media/0dad1f6c1cb3399b342721943d69bfb6/href</a></iframe><p>Finally, you need to ensure the Mutation component and the referenced queries are imported:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7bfb3bedf11bda2a3bb538529ec9ee0/href">https://medium.com/media/d7bfb3bedf11bda2a3bb538529ec9ee0/href</a></iframe><p>Go ahead and test the new functionality! You’ll see that a draft that is published through the UI will now indeed appear in the <em>feed</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*3fi7Zud_T3VeocM7CN_FIQ.png" /></figure><h4>5.3. Deleting posts</h4><p>The implementation of the delete functionality is very similar to the one of publishing posts. To keep this tutorial short, we’ll leave the implementation of that feature as an exercise to the attentive reader. If you find yourself lost, just check the <a href="https://github.com/nikolasburk/react-apollo-tutorial">final version</a> of the project on GitHub.</p><h3>Summary</h3><p>In this tutorial, you learned how to use the new API of React Apollo 2.1. This API is based on the new Query and Mutation components that are making use of the <a href="https://reactjs.org/docs/render-props.html">render props</a> pattern for sharing code among React components.</p><p>For the purpose of this tutorial, you rebuilt the basic boilerplate from the <a href="https://github.com/graphql-boilerplates/react-fullstack-graphql/">React &amp; GraphQL fullstack boilerplate</a> repository.</p><p>In a future tutorial, you’ll learn how to implement realtime updates inside your app using the new Subscription component of React Apollo 2.1 ⚡️</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F38bbbd%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F38bbbd%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F38bbbd%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href">https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=199e9e2bd01e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/tutorial-render-props-in-react-apollo-2-1-199e9e2bd01e">Tutorial: Render Props in React Apollo 2.1</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Top 5 Reasons to Use GraphQL]]></title>
            <link>https://medium.com/graphcool/top-5-reasons-to-use-graphql-b60cfa683511?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/b60cfa683511</guid>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[articles]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Tue, 20 Mar 2018 17:58:44 GMT</pubDate>
            <atom:updated>2018-03-20T17:58:44.249Z</atom:updated>
            <content:encoded><![CDATA[<h4>GraphQL is becoming the new standard for API development — learn the top reasons why in this article</h4><p>After only two and a half years of existence, GraphQL has made its way to the forefront of API development. In this article, we explain why developers love GraphQL and unveil the major reasons for its rapid adoption.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PNpd4M46k3b5OsA_ANITkQ.png" /></figure><h3>1) GraphQL APIs have a strongly typed schema</h3><p>One of the biggest problems with most APIs is that they’re lacking strong contracts for what their operations look like. Many developers have found themselves in situations where they needed to work with deprecated API documentation, lacking proper ways of knowing <em>what operations are supported </em>by an API and <em>how to use them</em>.</p><p>A <a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e">GraphQL schema</a> is the backbone of every GraphQL API. It clearly defines the operations (<em>queries, mutations </em>and <em>subscriptions</em>) supported by the API, including input arguments and possible responses. The schema is an unfailing contract that specifies the capabilities of an API.</p><blockquote>The GraphQL schema is an unfailing contract that specifies the capabilities of an API.</blockquote><p>GraphQL schemas are strongly-typed and can be written in the simple and expressive GraphQL <a href="https://blog.graph.cool/graphql-sdl-schema-definition-language-6755bcb9ce51">Schema Definition Language</a> (SDL). Thanks to the strong type-system, developers are getting many benefits that are unconceivable with schemaless APIs. As an example, build tooling can be leveraged to validate API requests and check for any errors that might occur in the communication with the API at compile-time. You might even get auto-completion for API operations in your editor!</p><p>Another benefit of the schema is that developers don’t have to manually write API documentation any more — instead it can be <em>auto-generated</em> based on the schema that defines the API. That’s a game-changer for API development!</p><p><a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e">GraphQL Server Basics: GraphQL Schemas, TypeDefs &amp; Resolvers Explained</a></p><h3>2) No more overfetching and underfetching</h3><p>Developers often describe the major benefit of GraphQL with the fact that clients can retrieve exactly the data they need from the API. They don’t have to rely on REST endpoints that return predefined and fixed data structures. Instead, the client can dictate the shape of the response objects returned by the API.</p><p>This solves two problems commonly encounter with REST APIs: <em>overfetching </em>and <em>underfetching</em>.</p><blockquote>With GraphQL, the client can dictate the shape of the response objects returned by the API.</blockquote><p><strong>Overfetching</strong> means the client is retrieving data that is actually not needed at the moment when it’s being fetched. It thus drains performance (more data needs longer to be downloaded and parsed) of the app and also exhausts the user’s data plan.</p><p>A simple example for overfetching would be the following scenario: An app renders a <em>profile screen </em>for a user which displays the user’s <em>name</em> and <em>birthday</em>. The corresponding API endpoint that provides the information about specific users (.e.g /users/&lt;id&gt;) is designed in a way that it also returns the <em>address </em>and <em>billing information</em> about each user. Both are useless for the profile screen and therefore fetching them is unnecessary.</p><p><strong>Underfetching</strong> is the opposite of overfetching and means that not enough data is included in an API response. This means the client needs to make additional API requests to satisfy its current data requirements.</p><p>In the worst-case, underfetching results in the infamous N+1-requests problem. This describes a situation in which a client requires information about a <em>list</em> with n items. However, there is no endpoint that would satisfy the data requirements by itself. Instead, the client needs to make one request per element to gather the required information.</p><p>As an example, consider a blogging application where users can publish articles. The app now displays a list of users, where each user element should also show the <em>title</em> of the last article published by the respective user. However, that piece of information is not included when hitting the /usersendpoint to get the list data. The app now needs to make one additional request <em>per user </em>to the /users/&lt;id&gt;/articles endpoint, only to fetch the title of the latest article.</p><blockquote><strong>Note: </strong>With REST APIs, the issues of underfetching is often tackled by tailoring the payloads of the API endpoints to the client’s needs. In the example, this would mean that the title of the last article of each users is now also returned by the <em>/users</em> endpoint. This approach might seem like a good solution at first, but it hinders fast product development and iterations cycles because any redesigns of the app will often require changes of the backend which are a lot more time-consuming. Learn more in the next section.</blockquote><p><a href="https://blog.graph.cool/how-to-wrap-a-rest-api-with-graphql-8bf3fb17547d">How to wrap a REST API with GraphQL</a></p><h3>3) GraphQL enables rapid product development</h3><p>GraphQL makes frontend developers’ lives easy. Thanks to GraphQL client libraries (like <a href="https://www.apollographql.com/client">Apollo</a>, <a href="https://facebook.github.io/relay/">Relay</a> or <a href="https://github.com/FormidableLabs/urql">Urql</a>) frontend developers are getting features like <em>caching</em>, <em>realtime</em> or<em> optimistic UI updates</em> basically for free — areas that could have <em>entire</em> <em>teams </em>dedicated to work on them if it wasn’t for GraphQL.</p><p>Increased productivity among frontend developers leads to a speedup in product development. With GraphQL, it is possible to completely redesign the UI of an app without needing to touch the backend.</p><blockquote>“We are product people — and we designed the API that we wanted to use to build products.” Lessons from 4 Years of GraphQL, <a href="https://www.twitter.com/leeb">Lee Byron</a></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FzVNrqo9XGOs%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DzVNrqo9XGOs&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FzVNrqo9XGOs%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d18e215e4156b126468af0dbd873c4dc/href">https://medium.com/media/d18e215e4156b126468af0dbd873c4dc/href</a></iframe><p>The process of building a GraphQL API is vastly centered around the GraphQL schema. Hence, you’ll often hear the term <em>schema-driven development</em> in the context of GraphQL. It simply refers to a process where a feature is first <em>defined</em> in the schema, then <em>implemented </em>with resolver functions.</p><p>Following this process and thanks to tools like <a href="https://github.com/APIs-guru/graphql-faker">GraphQL Faker</a>, the frontend developers can be productive already once the schema was defined. GraphQL Faker mocks the entire GraphQL API (based on its schema definition), so frontend and backend teams can work completely independently.</p><blockquote>To learn more about the difference between <strong>schema</strong> <strong>definition</strong> and <strong>implementation</strong>, be sure to check out <a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e">this</a> article.</blockquote><h3>4) Composing GraphQL APIs</h3><p>The idea of <a href="https://blog.graph.cool/graphql-schema-stitching-explained-schema-delegation-4c6caf468405">schema stitching</a> is one of the newer ones in the GraphQL space. In short, schema stitching allows to <em>combine and connect multiple GraphQL APIs</em> and and merge them into a single one. Similar to how a React components can be composed out of existing ones, a GraphQL API can also be composed out of existing GraphQL APIs!</p><blockquote>Similar to how a React components can be composed out of existing ones, a GraphQL API can also be composed out of existing GraphQL APIs!</blockquote><p>This is extremely beneficial for client applications that would otherwise need to talk to multiple GraphQL endpoints (which can often happen with a microservice architecture or when integrating with 3rd-party APIs like GitHub, Yelp or Shopify). Thanks to schema stitching, clients only deal with a single API endpoint and all complexity of orchestrating the communication with the various services is hidden from the client.</p><p><a href="https://blog.graph.cool/reusing-composing-graphql-apis-with-graphql-bindings-80a4aa37cff5">GraphQL bindings</a> take the idea of schema stitching to the next level by enabling a simple approach to reusing and sharing GraphQL APIs.</p><p><a href="https://blog.graph.cool/reusing-composing-graphql-apis-with-graphql-bindings-80a4aa37cff5">Reusing &amp; Composing GraphQL APIs with GraphQL Bindings</a></p><h3>5) Rich open-source ecosystem and an amazing community</h3><p>It is only a two and a half years since GraphQL was officially released by Facebook and it is incredible how much the entire GraphQL ecosystem has matured since then.</p><p>When it came out, the only tooling available for developers to use GraphQL was the <a href="https://github.com/graphql/graphql-js">graphql-js</a> reference implementation, a piece of middleware for Express.js and the GraphQL client Relay (Classic).</p><p>Today, reference implementations of the GraphQL specification are available in <a href="http://graphql.org/code/#server-libraries">various languages</a> and there’s a <a href="https://itnext.io/exploring-different-graphql-clients-d1bc69de305f">plethora of GraphQL clients</a>. In addition, lots of tooling (like <a href="https://www.prisma.io/">Prisma</a>, <a href="https://github.com/APIs-guru/graphql-faker">GraphQL Faker</a>, <a href="https://github.com/graphcool/graphql-playground">GraphQL Playground</a>, <a href="https://github.com/graphcool/graphql-config">graphql-config</a>,…) provide seamless workflows and make for an amazing developer experience when building GraphQL APIs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OPs5D2XrAcFI1hDay-_nEQ.png" /><figcaption>GraphQL is used in production by many big tech companies</figcaption></figure><p>The GraphQL community is also growing rapidly. <a href="http://graphql.org/users/">Many small and big companies have started using it in production</a> and more and more <a href="http://graphql.org/community/upcoming-events/#meetups">GraphQL Meetups</a> are being founded all over the world. There even are entire conferences that are exclusively dedicated to GraphQL:</p><ul><li><a href="https://www.graphql-europe.org">GraphQL Europe</a> (Berlin)</li><li><a href="https://www.graphqlday.org/">GraphQL Day</a> (changing locations, <a href="https://medium.com/graphql-europe/graphql-day-in-amsterdam-on-april-14-dee87bd9fc21">first edition in Amsterdam</a>)</li><li><a href="https://summit.graphql.com">GraphQL Summit</a> (San Francisco)</li></ul><h3>Get started with GraphQL today</h3><p>In this article, you have learned why GraphQL is the API technology of the future. The advantages it brings to the table and the various ways how it benefits developers and improves workflows are a game-changer for how APIs are <em>built</em> and <em>consumed</em>.</p><p>If you want to get started with GraphQL, here a few resources to help you get your feet off the ground quickly:</p><ul><li><a href="https://blog.graph.cool/tutorial-how-to-build-a-graphql-server-with-graphql-yoga-6da86f346e68">How to build a GraphQL Server with `graphql-yoga`</a></li><li><a href="https://www.howtographql.com/">How to GraphQL</a>: The fullstack GraphQL tutorial</li><li><a href="https://github.com/graphql-boilerplates">GraphQL boilerplates</a>: Starter kits for GraphQL projects with Node, TypeScript, React, Vue,…</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b60cfa683511" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/top-5-reasons-to-use-graphql-b60cfa683511">Top 5 Reasons to Use GraphQL</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying GraphQL Servers with Apex Up]]></title>
            <link>https://medium.com/graphcool/deploying-graphql-servers-with-apex-up-522f2b75a2ac?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/522f2b75a2ac</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Wed, 14 Mar 2018 16:21:02 GMT</pubDate>
            <atom:updated>2018-04-05T13:58:28.650Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to deploy your GraphQL server to the web (Tutorial)</h4><p>The <em>lifecycle</em> of an application consists of several phases. Depending on your process, you’ll find <em>at least </em>two of them: A <em>development</em> and a <em>production</em> phase. In this tutorial, you will learn how to take an app from development to production by <em>deploying</em> it with the one-click deployment tool <a href="https://github.com/apex/up">Apex Up</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VhC4wGAaTpTP82EQSVpSWg.png" /><figcaption>Apex Up makes it easy to deploy your GraphQL servers to <a href="https://aws.amazon.com/lambda">AWS Lambda</a></figcaption></figure><p>This tutorial has two parts:</p><ol><li><strong>Basic</strong>: Learn how to do a <em>simple and straightforward deployment </em>with Up</li><li><strong>Advanced</strong>: Learn how to deploy with Up <em>with environment variables</em></li></ol><h3>From development to production</h3><p>During development, you’ll usually test your software <em>locally</em>. When you’re working on a web app, this means you’re running a local server and access your application on http://localhost:4000 (or some other port).</p><p><em>http://localhost</em> however is not a domain that people on different machines than yours will be able to access over the web. That’s why you need to <em>deploy</em> the application.</p><p>Just few years ago, deployment was an extremely cumbersome process where you needed to get access to some physical server and do an awful lot of manual configuration work. Today, all this nitty-gritty is abstracted away by great services like Apex Up or Zeit Now.</p><h3>One-click deployment with Apex Up</h3><h4>Up is a simple interface to AWS Lambda</h4><p>Apex Up is an awesome one-click deployment tool for <a href="https://aws.amazon.com/lambda/">AWS Lambda</a>. Lambda itself is a great service for deploying serverless functions and even entire web servers — but it doesn’t have the best developer experience around it. Up provides a simple and intuitive interface to Lambda so you don’t have to deal with the tedious AWS tooling.</p><h4>Installing Up</h4><p>The easiest way to get started with Up is by following the quickstart instructions in the <a href="https://github.com/apex/up#quick-start">README</a>. You can install it by running the following command in the terminal:</p><pre>curl -sf https://up.apex.sh/install | sh</pre><h4>Deploying with Up</h4><p>When deploying an application with Up, all you need to do is invoke the terminal command up inside the root directory of your project. This assumes the presence of a file called up.json inside the same directory — if there is none, it will prompt you a few questions right inside the terminal and create it for you.</p><h3>Prerequisite: AWS Credentials</h3><p>Because Up uses AWS Lambda under the hood, you need to be able to authenticate with AWS when using it. This authentication is done by storing the AWS credentials on your machine.</p><p>If you have used the AWS CLI before, you likely already have credentials being stored on your machine. Otherwise, you need to install the <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html">AWS CLI</a> and create your credentials with it (note that in order to install it, you need to have Python and <a href="https://pypi.python.org/pypi/pip">pip</a> installed on your machine):</p><pre>pip install awscli --upgrade --user</pre><blockquote>The --upgrade option tells pip to upgrade any requirements that are already installed. The --user option tells pipto install the program to a subdirectory of your user directory to avoid modifying libraries used by your operating system. You can find more info <a href="https://docs.aws.amazon.com/cli/latest/userguide/installing.html">here</a>.</blockquote><p>Once the AWS CLI is installed, you can go ahead and create your credentials. This requires you to have an AWS account and provide an <strong><em>AWS Access Key ID</em></strong> as well as the belonging <strong><em>AWS Secret Access Key</em></strong>. From the <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html">AWS documentation</a>, here is how you get access to these:</p><ol><li>Open the <a href="https://console.aws.amazon.com/iam/home?#home">IAM console</a>.</li><li>In the navigation pane of the console, choose <strong>Users</strong>.</li><li>Choose your IAM user name (not the check box).</li><li>Choose the <strong>Security credentials</strong> tab and then choose <strong>Create access key</strong>.</li><li>To see the new access key, choose <strong>Show</strong>. Your credentials will look something like this: AKIAIOSFODNN7EXAMPLE (<em>Access Key ID</em>) and wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY (<em>Secret Access Key</em>)</li><li>To download the key pair, choose <strong>Download .csv file</strong>. Store the keys in a secure location.</li><li>Keep the keys confidential in order to protect your AWS account, and never email them. Do not share them outside your organization, even if an inquiry appears to come from AWS or Amazon.com. No one who legitimately represents Amazon will ever ask you for your secret key.</li></ol><p>Once you have the <em>Access Key ID</em> and the <em>Secret Access Key </em>available, run the following command and provide them when prompted:</p><pre>aws configure</pre><blockquote>If you need more help with setting up the credentials, you can check the corresponding section in the <a href="https://up.docs.apex.sh/#aws_credentials.aws_credential_profiles">Up documentation</a> or <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html">AWS</a> directly.</blockquote><p>The end result will be that you now have a hidden .aws directory inside your home folder. In there, you’ll find a file called credentials looking similar to this:</p><pre>[default]<br>aws_access_key_id = xxxxxxxx<br>aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxx</pre><h3>Part 1: Basic Deployment with Up</h3><h4>Bootstrapping the GraphQL server</h4><p>For the purpose of this tutorial, you’ll use the <a href="https://github.com/graphql-boilerplates/node-graphql-server/tree/master/basic">node-basic</a> GraphQL boilerplate project as a <em>sample server</em> to be deployed. The easiest way to get access to this boilerplate is by using the graphql create command from the <a href="https://github.com/graphql-cli/graphql-cli">GraphQL CLI</a>.</p><p>If you don’t have the GraphQL CLI installed on your machine yet, you can do so with the following command:</p><pre>npm install -g graphql-cli</pre><p>The boilerplate project uses <a href="https://github.com/graphcool/graphql-yoga/">graphql-yoga</a>, a lightweight GraphQL server based on Express.js, apollo-server and graphql-tools.</p><p>Now go ahead and bootstrap your GraphQL server with graphql create:</p><pre>graphql create hello-basic --boilerplate node-basic</pre><blockquote>This command will create a new directory called <em>hello-basic</em> and download the <em>node-basic</em> boilerplate into it.</blockquote><p>When prompted <em>where</em> (i.e. to which <a href="https://www.prismagraphql.com/docs/faq/prisma-cloud-fae2ooth2u#what-are-clusters-how-do-they-relate-to-services"><em>cluster</em></a>) to deploy your Prisma service, you need to choose one of the <em>development cluster</em> options: prisma-eu1 or prisma-us1.</p><p>It’s important to understand that your Prisma service needs to be deployed <em>before </em>the GraphQL server. This is because the GraphQL server is “powered” by the Prisma service. If the Prisma service was not available, the GraphQL server wouldn’t work because it would effectively miss the database layer!</p><h4>Deploying the server with `up`</h4><p>When deploying a web server with Up, all you need to do is navigate into the root directory of that server and invoke the up command. As mentioned above, this command either expects the presence of an already created up.json file or will create one for you if it doesn’t exist yet.</p><p>Navigate into the newly created hello-basic directory and deploy the server:</p><pre>cd hello-basic<br>up</pre><p>Provide the required information when prompted by up:</p><ul><li><strong>Project name</strong>: You can use the default, hello-basic for this</li><li><strong>AWS Profile</strong>: If you have setup your credentials correctly, up will provide a list of profiles to choose from. If this doesn’t work, go back to the <a href="https://blog.graph.cool/one-click-graphql-server-deployment-with-apex-up-522f2b75a2ac#c5cf">previous section</a> and properly configure your credentials.</li><li><strong>AWS Region</strong>: In order to get the best possible performance for your server, be sure to deploy the GraphQL server to the same AWS region as the Prisma service. If you chose prisma-eu1 as the target cluster for the Prisma, select the EU (Ireland) region — in case you chose prisma-us1, select US West (Oregon).</li></ul><p>After you’ve provided the required information, up will take the files inside the project directory and deploy them to Lambda (note that this can take a few minutes).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*SyGokC5RADoRaBjoqFUhsA.png" /></figure><p>On Lambda, the server will be started by invoking the start script inside package.json (basically it will just use yarn start or npm run start). The start script looks as follows: node src/index.js .</p><p>All right, congratulations! The GraphQL server is deployed — so how can you access it? Just run the following command in the terminal:</p><pre>up url --open</pre><p>This opens a browser and points it to the URL where the GraphQL server was deployed (when omitting the --open flag, the URL just gets printed).</p><p>But wait! What’s that? An <strong>internal server error</strong>? 😡 Where is the GraphQL server that was promised?</p><blockquote><strong>UPDATE: </strong><a href="https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/"><strong>AWS Lambda now supports Node 8</strong></a><strong>, so you’re probably not going to see this error any more. If you’re already seeing a GraphQL Playground, feel free to </strong><a href="https://blog.graph.cool/deploying-graphql-servers-with-apex-up-522f2b75a2ac#3f60"><strong>skip ahead</strong></a><strong>.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HLgNDjkvJ_gtYJZPxpDtXA.png" /></figure><p>Well, <a href="https://up.docs.apex.sh/#runtimes.node_js">Lambda and Up by currently only support Node 6</a>. The GraphQL server however is using newer JavaScript features that are not yet implemented in Node 6.</p><blockquote>To learn what exactly went wrong, you can use the up logs command to get more insights into the problem that occurred during the deployment process.</blockquote><p>To work around this, you can download the Node 8 binary and upload it to the Lambda environment. To do so, you’re going to introduce an additional build step using a <a href="https://en.wikipedia.org/wiki/Makefile">Makefile</a>.</p><blockquote>For more details regarding deploying Node 8 apps with Up, check out the corresponding <a href="https://github.com/apex/up-examples/tree/master/oss/node-8">example</a> project.</blockquote><p>Inside the hello-basic directory, create a new file called Makefile and paste the following contents into it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11b4e9d7576cdb69c4406e98a0f8223c/href">https://medium.com/media/11b4e9d7576cdb69c4406e98a0f8223c/href</a></iframe><p>Next, you need need to add a <a href="https://up.docs.apex.sh/#configuration.hook_scripts"><em>hook</em></a><em> </em>to the up.json where the build step is performed. Adjust up.json so it looks similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24bebd44b152b177b7a902058224449b/href">https://medium.com/media/24bebd44b152b177b7a902058224449b/href</a></iframe><p>What up will do the next time it’s invoked is download the Node 8 binary as specified in the Makefile and then upload it to Lambda as well. So now the last thing you need to ensure is that this binary is used to <em>start</em> the server.</p><p>You could do so by adjusting the yarn start command, but because that’s commonly reserved for your local development process, in this case it’s more handy to introduce a <a href="https://up.docs.apex.sh/#commands.start"><em>proxy command</em></a>. Again, adjust up.json so it now starts the server using the Node 8 binary:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf2d67bc5dc58a6b73ad5ceedf911297/href">https://medium.com/media/bf2d67bc5dc58a6b73ad5ceedf911297/href</a></iframe><p>With that setup in place, you can go ahead and deploy the server once more:</p><pre>up</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*PepQji46Heq3ssyRErpSkA.png" /></figure><p>Opening the application with up url --open now will finally render the <a href="https://github.com/graphcool/graphql-playground">GraphQL Playground</a> you can use to interact with your server! There’s still a minor problem however: The schema is not yet loaded and you won’t be able to send any actual queries to the API.</p><p>This is simply due to a small <a href="https://github.com/graphcool/graphql-playground/issues/598">bug</a> in the Playground. As you might notice, the URLs in the browser’s address bar and the one from the Playground’s address bar are different — the latter one lacks the URL’s <em>path </em>(“/staging”). Thus, all you need to do is manually append /staging to the Playground’s URL and hit the little <em>reload</em>-button on the right of the bar:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MEtPJM2ns2iKnZ0YjGEYIA.png" /></figure><p>Once you did that, you will be able to send queries and mutation to the API. For example, try the following query:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dad3f7b865ebb96ea4dc78064f657d3c/href">https://medium.com/media/dad3f7b865ebb96ea4dc78064f657d3c/href</a></iframe><h4>Multi-stage deployment with Up</h4><p>The /staging path that’s appended to the URL indicates the <em>stage</em> of the deployment. Up supports two stages: /staging (default) and /production.</p><p>To deploy to the /production stage, you can simply run up production.</p><h3>Part 2: Advanced Deployment with Up</h3><h4>Using environment variables to configure deployment stages</h4><p>In this part, you’ll learn how to configure <em>environment variables</em> when deploying with Up. Environment variables are a best practice to quickly switch between different <em>deployment stages</em> of your server.</p><p>For example, during development you’ll commonly want to run your server in a <em>development </em>environment. The development environment uses a different database than your <em>production </em>environment. This ensures you’re not accidentally messing with actual user data when developing a new feature.</p><p>Only once you’re confident the new feature works as expected, you’ll deploy it to <em>production</em>. Environment variables are a great way to distinguish between environments by pointing to a specific database.</p><p>Another common use case for environment variables is to provision <em>secrets</em> to the build environment. It’s likely that your database requires some sort of authentication, or maybe you’re using 3rd party services that require to provide API keys so you can access them. This kind of secret information is also commonly provided through environment variables.Bootstrapping the GraphQL server</p><h4>Bootstrapping the GraphQL server</h4><p>Similar to before, you’ll start by bootstrapping a GraphQL server. This time, you’re going to use the <a href="https://github.com/graphql-boilerplates/node-graphql-server/tree/master/advanced">node-advanced</a> GraphQL boilerplate project:</p><pre>graphql create hello-advanced --boilerplate node-advanced</pre><p>Just like before, deploy the Prisma service to any development cluster, e.g.prisma-eu1 or prisma-us1.</p><p>The three core differences to the node-basic boilerplate are the following:</p><ul><li>node-advanced comes with built-in user authentication</li><li>node-advanced uses environment variables (via .env files)</li><li>node-advanced has realtime functionality (GraphQL Subscriptions)</li></ul><p><strong>Attention: </strong>One thing that is really important to realize when using Up is that realtime functionality based on GraphQL subscriptions do not work!</p><p>The reason for that is that subscriptions are implemented with WebSockets and the server needs to maintain a pool of connections to the subscribed clients — the server is hence <em>stateful</em>. However, AWS Lambda does not support stateful apps! Lambda works in the way that it provisions infrastructure for the servers on-the-go and tears it down when not needed any more. The teardown breaks any connections from previously subscribed clients!</p><h4>Accessing environment variables</h4><p>Before moving on, consider these following two files:</p><p><strong>src/index.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93eabcf3c2a7dd8153107edc64cd87f8/href">https://medium.com/media/93eabcf3c2a7dd8153107edc64cd87f8/href</a></iframe><p><strong>database/prisma.yml</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f24802222ac999da22f8278b6ddbcbdf/href">https://medium.com/media/f24802222ac999da22f8278b6ddbcbdf/href</a></iframe><p>In index.js, two environment variables are referenced when instantiating the Prisma binding object. With process.env.PRISMA_ENDPOINT and process.env.PRISMA_SECRET, the code expects that two environment variables called PRISMA_ENDPOINT and PRISMA_SECRET are set.</p><p>Similarly, prisma.yml provides the values for three of its five properties using environment variables. In addition to PRISMA_SECRET, it also references a PRISMA_CLUSTER and PRISMA_STAGE.</p><p>The most obvious and straightforward way to provide these values now would be to set them directly in your shell,. e.g. with the following commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68383a3eba638881e8a2248f190b340b/href">https://medium.com/media/68383a3eba638881e8a2248f190b340b/href</a></iframe><p>However, this approach is often not practical (or at all <em>feasible</em>) when working with state-of-the-art deployment and <a href="https://en.wikipedia.org/wiki/Continuous_integration">continuous integration</a> tools, such as <a href="https://circleci.com">CircleCI</a>, <a href="https://buildkite.com/">Buildkite</a> or Now. This is because you don’t have direct access to the build environment — you cannot just <a href="https://en.wikipedia.org/wiki/Secure_Shell">ssh</a> into the machine that’s going to build and start your server in order to set environment variables. Instead these tools provide you with custom mechanisms for setting environment variables, or follow best practices for doing so.</p><p>In the case of Up, environment variables can be set by adding an environment object to up.json. Note that <a href="https://github.com/apex/up#pro-features">Up Pro</a> also offers an additional <a href="https://up.docs.apex.sh/#commands.env">up env</a> command to encrypt and manage environment variables.</p><p>The node-advanced boilerplate is configured to use .env files in combination with <a href="https://github.com/motdotla/dotenv">dotenv</a> by default. Since .env files are not supported by Up, you’ll have to manually copy over the values into the mentioned environment object inside up.json. While you’re at it, you can also already add the <em>hook</em> and custom <em>proxy command</em> to it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd8365e922033385b4307f237edf73e7/href">https://medium.com/media/bd8365e922033385b4307f237edf73e7/href</a></iframe><p>Notice that the PRISMA_ENDPOINT and PRISMA_CLUSTER will look slightly different for you!</p><p>Now, the last thing to do is create the Makefile that downloads the Node 8 binary. Just like before, add a Makefile to the project’s root directory and fill it with the following contents:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11b4e9d7576cdb69c4406e98a0f8223c/href">https://medium.com/media/11b4e9d7576cdb69c4406e98a0f8223c/href</a></iframe><p>That’s it — you can now go ahead and deploy the project:</p><pre>up</pre><p>Then open it in the browser with:</p><pre>up url --open</pre><p>To test the app, you can send the same query from above again. To see what other operations are available, check out the interactive API documentation right inside the Playground.</p><h3>Summary</h3><p>In this tutorial, you learned how to deploy a GraphQL server with <a href="https://github.com/apex/up">Apex Up</a>, an awesome one-click deployment tool. As sample GraphQL servers to deploy, you used the node-basic and node-advanced <a href="https://github.com/graphql-boilerplates">GraphQL boilerplate</a> projects.</p><p>Deploying node-basic was straightforward since all required configuration was hardcoded in the project. For the node-advanced project, you used environment variables that you configured by using the custom mechanism provided by Up.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=522f2b75a2ac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/deploying-graphql-servers-with-apex-up-522f2b75a2ac">Deploying GraphQL Servers with Apex Up</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Prisma Cloud — A GraphQL Database Platform]]></title>
            <link>https://medium.com/graphcool/introducing-prisma-cloud-a-graphql-database-platform-ed591baa8737?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/ed591baa8737</guid>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[announcements]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Graphcool]]></dc:creator>
            <pubDate>Tue, 06 Mar 2018 10:13:02 GMT</pubDate>
            <atom:updated>2018-04-04T15:39:06.990Z</atom:updated>
            <content:encoded><![CDATA[<h4>The easiest way to run Prisma in production — on any cloud</h4><p>After the <a href="https://blog.graph.cool/prisma-cloud-preview-invite-only-c251bebf5670">invite-only preview</a>, we are very excited to officially introduce <a href="https://www.prismagraphql.com/cloud/">Prisma Cloud</a>, a serverless hosting platform for Prisma.</p><p><strong>To get started, you can </strong><a href="https://app.prisma.sh"><strong>sign up here</strong></a>. 🎉</p><p>It offers powerful <em>data workflows </em>like exploring and editing data in an intuitive <em>data browser </em>as well as <em>automatic rollbacks</em>. Further features include <em>team collaboration</em>,<em> performance metrics</em>,<em> health checks </em>and more. Simply connect your database and you’re ready to go!</p><blockquote>This article is about <strong>Prisma Cloud</strong>, a hosting platform for Prisma services. <strong>Prisma </strong>is an open-source tool that helps you build GraphQL servers — learn more about Prisma in the <a href="https://www.prismagraphql.com/docs">documentation</a> or with this 4-min <a href="https://www.youtube.com/watch?v=20zGexpEitc">demo video</a>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8hue3n4IyMf3d6iX2hL-tQ.png" /></figure><h3>The evolution of database technologies &amp; hosting</h3><p>Before diving into the features of Prisma Cloud, let’s take a look at the history of databases and how they are hosted. (Feel free to skip ahead if you want.)</p><h4>From SQL to NoSQL: A growing jungle of specialized databases</h4><p>The field of databases has a long history. Many different kinds of databases have been developed since the first relational DB was invented in 1970.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YRTutyyz56U7IaT3EzpQJw.png" /></figure><p>For the longest time, <strong><em>relational databases</em></strong>, such as MySQL and Oracle, were state-of-the-art for any type of application that was developed. At that time, <em>monoliths </em>were the standard architecture for application development.</p><p>At some point,<strong> <em>NoSQL databases</em></strong> entered the game. MongoDB, Redis and Neo4j, to name a few, fall into that category. The goal of NoSQL was to either provide a <em>simpler interface</em> to working with data or cater to <em>specific use cases</em> and choose different optimization strategies.</p><p>Nowadays, databases are becoming available as <strong><em>globally distributed</em></strong> systems such as Google Spanner or CockroachDB. These kinds of databases are horizontally scalable and come with automatic replication features as well as guarantees for consistency, availability and scalability.</p><h4>Manual, Managed &amp; Serverless: Database hosting is getting simpler</h4><p>Similar to the historic development of database <em>technologies</em>, the ways how databases are <em>hosted</em> also has evolved quite a bit over the past decades.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*el8WJKzpZYR2r9k0TvaJag.png" /></figure><p>In the early days of the web everybody had to run databases on their own physical servers. EC2 and Digital Ocean made this easier, but a deep technical understanding is still required to <strong><em>manually</em></strong> operate database.</p><p><strong><em>Managed services</em></strong> such as Heroku’s Postgres Service, AWS RDS and Mongo Atlas abstracted away a lot of the complex details. Database management has become simpler, but the underlying model is still the same, requiring developers to provision compute capacity upfront.</p><p>The most recent development, <strong><em>serverless</em></strong> databases, frees developers from worrying about infrastructure, as their database simply scales up and down to match the current load while paying based on actual usage. Aurora Serverless and CosmosDB are prominent examples.</p><h4>The future of database management: Prisma &amp; Prisma Cloud</h4><p>Prisma is a GraphQL database proxy turning a database into a GraphQL API. Instead of reading and writing from/to the database using SQL or another database API, you’re using expressive GraphQL queries and mutations.</p><p>As described above, we now have a plethora of databases available, all bringing different strengths and tradeoffs to the table. Prisma’s coherent GraphQL API makes it easy for developers to work with data from multiple existing databases or even incorporating new types of databases into their application.</p><blockquote>Today, Prisma works with MySQL. In the future, <a href="https://github.com/graphcool/prisma/issues/1751">other databases</a> and services will be supported (MongoDB, Elasticsearch, Neo4J, …) as well.</blockquote><p>When using Prisma Cloud, you can choose any of the aforementioned technologies and hosting options for your database(s). Just as tools like Netlify and Zeit focus on a narrow slice of the tech stack (static hosting for Netlify, dynamic hosting for Zeit), so does Prisma Cloud by focusing on <em>the connection between databases and your applications</em>.</p><p>In that sense, Prisma Cloud doesn’t provide any database hosting and makes no decisions about the database technology to be used — it is used <em>on top </em>of existing providers and can be <em>combined</em> with any database technology.</p><h3>Prisma Cloud — Feature overview</h3><h4>Serverless cluster hosting</h4><p>Prisma Cloud exposes a serverless deployment model. This means you simply pay for the resources you consume (<em>pay-as-you-go</em>) — Prisma Cloud will scale the underlying resources dynamically to meet your demand.</p><p>In other words, you don’t have to predict how many servers you need or over-provision in anticipation of a big launch — Prisma handles all of that for you.</p><h4>A beautiful data browser</h4><p>At the heart of the Prisma Cloud Console is a beautiful data browser. With an intuitive UI and data editing capabilities similar to Excel and Airtable, it opens up your application data to domain experts who normally wouldn’t be able to query the database directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6bh-ex332D9jWUTAh6v7Xw.png" /></figure><p>Every Prisma Cloud service also features an integrated <a href="https://github.com/graphcool/graphql-playground">GraphQL Playground</a> –a powerful GraphQL IDE to send queries and mutations to your database. It also includes auto-completion features and intuitive API documentation.</p><h4>Team collaboration features</h4><p>With Prisma Cloud you have one central access point to all your data. Instead of creating custom admin tools for content writers and the support team, simply invite them to your Prisma Cloud team and set up appropriate data access rights.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iM5zUYKzrncDVBWWXdUxuw.png" /></figure><h4>Metrics, health checks &amp; database insights</h4><p>Monitoring is critical for operating your demanding applications at scale. Prisma Cloud can scale to tens of thousands of requests per second while providing insights into current request patterns as well as the state of the underlying databases.</p><p>The Prisma Console includes a dashboard that displays various performance metrics. In the future, there will be more detailed monitoring tools to help you analyse the impact of specific queries and optimize the data model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tr__aN3Whln_RVd4K2voyA.png" /></figure><h4>Deployment history &amp; automatic rollbacks</h4><p>A Prisma service is defined by a mapping that describes the structure of one or more databases. Whenever the service is updated, the change is recorded in the <em>deployment history</em> with detailed information about <em>what</em> changed and <em>who</em> performed the change.<br>In the future we will introduce point-in-time restore, allowing you to roll back the state of your database to just before a bad change was introduced</p><h3>Get started with Prisma Cloud today</h3><p>Prisma Cloud is a serverless GraphQL database platform that provides a simple abstraction and handy tools to manage your databases. It comes with a set of awesome features that make your data-related workflows a breeze.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FjELE4KXJPn4%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DjELE4KXJPn4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FjELE4KXJPn4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdaaa7272a72e142130aeead84a1ff40/href">https://medium.com/media/bdaaa7272a72e142130aeead84a1ff40/href</a></iframe><p><strong>Get started with Prisma Cloud today by simply </strong><a href="https://app.prisma.sh/"><strong>logging in with your GitHub account</strong></a><strong>. </strong>To learn more about how Prisma Cloud works, you can check out the <a href="https://www.prismagraphql.com/docs/tutorials/cluster-deployment/prisma-cloud-ua9gai4kie">documentation</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F38bbbd%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F38bbbd%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F38bbbd%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href">https://medium.com/media/d7f8f14658f9309b52b6c86c195ecb01/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed591baa8737" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/introducing-prisma-cloud-a-graphql-database-platform-ed591baa8737">Introducing Prisma Cloud — A GraphQL Database Platform</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL Directive Permissions — Authorization Made Easy]]></title>
            <link>https://medium.com/graphcool/graphql-directive-permissions-authorization-made-easy-54c076b5368e?source=rss----3691a1eb1fee---4</link>
            <guid isPermaLink="false">https://medium.com/p/54c076b5368e</guid>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Dennis Walsh]]></dc:creator>
            <pubDate>Thu, 01 Mar 2018 16:41:41 GMT</pubDate>
            <atom:updated>2018-03-01T16:41:40.537Z</atom:updated>
            <content:encoded><![CDATA[<h4>Directive permissions are a declarative way of implementing authorization rules in GraphQL servers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZHjkqTJJr_A1dPas8U-npg.png" /></figure><p>GraphQL servers send your app into the world wearing only its birthday suit — everything is exposed. A quick introspection query reveals all possible API operations and data structures. That <a href="https://github.com/graphql/graphql-js/issues/113">could change</a>, but for now, all of your operations and data are laid bare.</p><p>Therefore, one might anticipate authentication and authorization are GraphQL first class citizens. But, neither of them are part of the official <a href="http://facebook.github.io/graphql/draft/">spec</a>. That lack of direction created a lot of sleepless nights for my GraphQL server development, and it wasn’t until I watched <a href="https://twitter.com/ryanchenkie">Ryan Chenkie</a>’s talk about <em>directive permissions</em> that I found a solution.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F4_Bcw7BULC8%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D4_Bcw7BULC8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F4_Bcw7BULC8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a7c3d9c15ed577cb7524820317d5002/href">https://medium.com/media/6a7c3d9c15ed577cb7524820317d5002/href</a></iframe><p>In this post, we will first talk through a naive approach to GraphQL permissions and find out about its drawbacks. Then, we’ll discover directive permissions and learn how they provide a declarative and reusable alternative.</p><blockquote>To play around with directive permissions, check out this Launchpad <a href="https://launchpad.graphql.com/nxp1870jl7">demo</a>.</blockquote><h3>Permissions in GraphQL: A Naive Approach</h3><h4>A Simple Example</h4><p>When naively implemented, the code for permissions in your GraphQL server quickly becomes repetitive. Let’s start by quickly reviewing how most GraphQL servers implement permissions in a naive and simple fashion.</p><p>As an example project to anchor the discussion, here’s our <a href="https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e">GraphQL schema definition</a>. The schema defines the API for car dealer software.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb548a07d34aac0dc36bf6fc9d4f9623/href">https://medium.com/media/fb548a07d34aac0dc36bf6fc9d4f9623/href</a></iframe><p>Several fields should scream <em>“malicious user fun”.</em> To protect the API, let’s make up a few permissions rules for it:</p><ul><li>updateVehicleAskingPrice should be restricted to <em>managers</em></li><li>costBasis should be restricted to <em>managers</em></li><li>numberOfOffers should be restricted to <em>authenticated users</em></li></ul><h4>Naively Implementing Permissions in GraphQL Resolvers Creates Duplication and Mixes Concerns</h4><p>Using <a href="https://www.prismagraphql.com">Prisma</a> in combination with <a href="https://github.com/graphcool/prisma-binding">Prisma bindings</a> as a data access layer, here’s how the restriction for the updateVehicleAskingPrice mutation can be implemented:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ea10b4a638c63e2af6f6697d041a262/href">https://medium.com/media/1ea10b4a638c63e2af6f6697d041a262/href</a></iframe><p>We’re using the <a href="https://github.com/graphcool/prisma-binding#exists">exists</a> function on the Prisma binding instance<em> (line 4)</em> to ensure the requesting user has proper access rights for this mutation. If this is not the case, the mutation is not performed and will fail with an “Invalid permissions”-error instead.</p><p>Field-level constraints get even more interesting. Here is how we can protect the numberOfOffers and costBasis fields on Vehicle:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ca5aa7ceacc7d817dc7e1f2e9e6d43f/href">https://medium.com/media/7ca5aa7ceacc7d817dc7e1f2e9e6d43f/href</a></iframe><p>With this approach, our resolvers become cluttered with redundant permission logic. As a first improvement, we can write a <em>wrapping function </em>that abstracts away some of the redundant authorization logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a28ac0170049d8a449d6fb63bed07e63/href">https://medium.com/media/a28ac0170049d8a449d6fb63bed07e63/href</a></iframe><p>That looks a lot cleaner already. However, there is no straightforward way to figure out which fields are protected. This will always require digging into the resolver implementations and actually read the code.</p><h3>A Declarative Approach to GraphQL Permissions Based on GraphQL Directives</h3><h4>Embedding Permission Directives in the Schema Definition</h4><p>Contrast the above schema and resolver implementation with this schema:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44585f74d078ce87ff657ee57a0b9410/href">https://medium.com/media/44585f74d078ce87ff657ee57a0b9410/href</a></iframe><p>At a glance, we know what is protected and what is not. In my book, that’s a win! 🍻 How do we get to such magic? You guessed it — <a href="https://www.apollographql.com/docs/graphql-tools/schema-directives.html#directiveResolvers-option">directive resolvers</a>.</p><h4>Realizing Directive Resolvers Into Permissions is Straightforward</h4><p>You can think of directive resolvers as <em>resolver middleware</em>. An incoming request first hits the directive resolver. If it passes (i.e. next() is invoked), the request continues to the actual field resolver. Here’s how we check for an authorized role inside a directive resolver:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d7352241f8116d3b241a3968a2c6856/href">https://medium.com/media/8d7352241f8116d3b241a3968a2c6856/href</a></iframe><p>Enforcing permission rules with GraphQL directives allows to remove any authorization logic from the field resolvers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6b8dc457af233383d6ff9590d366764/href">https://medium.com/media/f6b8dc457af233383d6ff9590d366764/href</a></iframe><p>Note that setting up directiveResolvers with graphql-tools is straightforward:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3ce03ed305a76f4fd0253e6df6ddfb8/href">https://medium.com/media/d3ce03ed305a76f4fd0253e6df6ddfb8/href</a></iframe><h4>Changing Directive Permissions Takes Little Work</h4><p>Now, let’s change the requirements:</p><ul><li>numberOfOffers should be restricted to <em>managers</em> (formerly just <em>authenticated</em>)</li><li>askingPrice should be restricted to <em>authenticated users </em>(formerly <em>unauthenticated</em>)</li></ul><p>Here, hold my beer…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a4fc3272ce3f199656479c64acb62ba/href">https://medium.com/media/6a4fc3272ce3f199656479c64acb62ba/href</a></iframe><p>…Done! We can update our permissions simply by adjusting the directives in the schema definition, without touching the actual implementation. The generic directive resolvers take care of enforcing the rules.</p><h4>Demo: Experimenting With Directive Permissions</h4><p>The best way to learn is experimentation, so I made a <a href="https://launchpad.graphql.com/nxp1870jl7">Launchpad demo</a> to play around with permissions, queries, and users.</p><p>For instance, try running this mutation as different users:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80aa772dbf572f3333b0b5f862ef3c4d/href">https://medium.com/media/80aa772dbf572f3333b0b5f862ef3c4d/href</a></iframe><p>Change the directives on the schema to see how the permissions change. Note that Launchpad’s GraphiQL shows errors first, scroll down to see data.</p><blockquote>I also made a <a href="https://github.com/LawJolla/prisma-auth0-example">demo repo</a> combining Prisma, <a href="http://www.auth0.com">Auth0</a>, and directive permissions.</blockquote><h3>Potential Drawbacks &amp; Alternatives</h3><p>Some people argue against “polluting” the schema with information that goes beyond the actual GraphQL schema definition. I appreciate that argument. For a different approach that doesn’t touch your schema definition, check out <a href="https://github.com/maticzav/graphql-shield">GraphQL Shield</a> by <a href="https://medium.com/u/9058c3a48f07">Matic Zavadlal</a>. It even has a few ingenious tricks like caching permission functions per request.</p><h3>Directive Permissions Provide Clear and Easy Authorization</h3><p>Permissions in GraphQL can be difficult at first. With RESTful APIs, authorization is implemented by protecting the individual API endpoints. This approach does not work for GraphQL because there’s only a single endpoint exposed by the server.</p><p>Therefore, authorization in GraphQL requires a major shift in thinking. The official GraphQL spec doesn’t provide any guidance and best practices for implementing permissions are still emerging. One of them being <em>directive permissions </em>which we covered in this article.</p><p>To understand directive permissions, we first took at look at the cumbersome resolver implementation when going for a naive approach. We then learned about directive permissions and how they provide a declarative alternative by extending the GraphQL schema definition with dedicated GraphQL directives.</p><p>I believe this emerging directive permissions pattern finally gives GraphQL a clear and declarative path to securing data. It does so by separating permissions and data into their respective layers with a concise and expressive syntax.</p><blockquote>To get some practical experience with the examples explained in this article, check out the <a href="https://launchpad.graphql.com/nxp1870jl7">live demo</a> on Launchpad or the code in <a href="https://github.com/LawJolla/prisma-auth0-example">this</a> GitHub repository.</blockquote><p>I like blogging about new software patterns and intellectual property law. If we share those interests, please consider following me <a href="https://medium.com/@dwalsh.sdlr">here</a> and on Twitter <a href="https://twitter.com/lawjolla">@LawJolla</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54c076b5368e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/graphcool/graphql-directive-permissions-authorization-made-easy-54c076b5368e">GraphQL Directive Permissions — Authorization Made Easy</a> was originally published in <a href="https://medium.com/graphcool">Graphcool Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>