{"id":339,"date":"2021-09-18T05:24:03","date_gmt":"2021-09-18T12:24:03","guid":{"rendered":"https:\/\/vinodsr.com\/myblog\/?p=339"},"modified":"2021-09-18T07:00:17","modified_gmt":"2021-09-18T14:00:17","slug":"how-to-enable-subscription-in-apollo-federation-with-nestjs-graphql","status":"publish","type":"post","link":"https:\/\/vinodsr.com\/myblog\/2021\/09\/how-to-enable-subscription-in-apollo-federation-with-nestjs-graphql\/","title":{"rendered":"How to enable subscription in Apollo Federation with nestJS graphql"},"content":{"rendered":"\n<div class=\"wp-block-cover has-background-dim\"><img loading=\"lazy\" decoding=\"async\" width=\"991\" height=\"449\" class=\"wp-block-cover__image-background wp-image-383\" alt=\"\" src=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/apollo-federation1.jpg\" data-object-fit=\"cover\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/apollo-federation1.jpg 991w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/apollo-federation1-300x136.jpg 300w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/apollo-federation1-768x348.jpg 768w\" sizes=\"auto, (max-width: 991px) 100vw, 991px\" \/><div class=\"wp-block-cover__inner-container is-layout-flow wp-block-cover-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\"><\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">NestJS with GraphQL<\/h2>\n\n\n\n<p>Setting up a graphQL server in nestJS saves 5x-10x of the effort with vanilla nodejs. NestJS uses the standard apollo library for setting up the graphQL. It supports both normal and federation mode. In federation mode, we can setup graphql servers in every microservices, which can be aggregated using a gateway; Just like API-Gateway with REST services. It is a very good technique if you are running microservices and want to separate the responsibility for each service.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>NestJS is one of the powerful nodejs framework available as of today. One of the cool features of nestJS is that it allows easy integration with databases, message queues, authentication etc. Further more it allows developers to create application  using nodeJS with in a much stricter, robust and flexible framework like Spring Boot \/ Angular.<\/p><p>If you are new to nestJS head over to <a href=\"https:\/\/docs.nestjs.com\">https:\/\/docs.nestjs.com<\/a> for getting started. This article expects that you have a prior knowledge of setting up and running nestJS.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Contents<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"#theproblem\" title=\"#theproblem\">the problem<\/a><\/li><li><a href=\"#theproblem\" title=\"#theproblem\">solution<\/a><\/li><li><a href=\"#theproblem\" title=\"#theproblem\">conclusion<\/a><\/li><li><a href=\"#bonustip\">bonus tip<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"theproblem\">the problem<\/h2>\n\n\n\n<p> All this comes with some inherent caveats from the apollo library. While setting up the federation you cannot use graphql subscription. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"69\" src=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/Federation-error-subs-1024x69.png\" alt=\"\" class=\"wp-image-355\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/Federation-error-subs-1024x69.png 1024w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/Federation-error-subs-300x20.png 300w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/Federation-error-subs-768x52.png 768w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/Federation-error-subs.png 1152w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>You will probably see this error<\/figcaption><\/figure>\n\n\n\n<p>Apollo had come up with an <a href=\"https:\/\/www.apollographql.com\/blog\/backend\/federation\/using-subscriptions-with-your-federated-data-graph\/\" target=\"_blank\" rel=\"noreferrer noopener\">approach <\/a>to over come this problem. But it over complicates the situation. Lets see how to tackle this problem using the features in nestJS. In the example i am using the <a href=\"https:\/\/docs.nestjs.com\/graphql\/resolvers#schema-first\" target=\"_blank\" rel=\"noreferrer noopener\">schema first approach<\/a>. The same approach can be implemented in the <a href=\"https:\/\/docs.nestjs.com\/graphql\/resolvers#code-first\" target=\"_blank\" rel=\"noreferrer noopener\">code based approach<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"solution\">solution<\/h2>\n\n\n\n<p>The solution to this problem is to hide the subscription based schema for the federation server and host the subscription based graphql as a separate normal graphql server. Seems very simple huh ? Not that easy \ud83d\ude42 . We will cover the method with some simple steps. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">step 1 : Classify Schema<\/h4>\n\n\n\n<p>The primary step is to separate your graphql schema in to different files as per the below conventions. <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>*.graphq<\/strong>l : for graphql syntax supported in both federation and normal mode<\/li><li><strong>*.graphql.federation<\/strong> : for syntax supported only in federation mode (eg : extends)<\/li><li><strong>*.graphql.normal<\/strong> : for syntax supported only in normal mode (eg : subscription)<\/li><\/ol>\n\n\n\n<p>Save the subscription model in any &#8220;<em><strong>graphql.normal<\/strong><\/em>&#8221; file<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"321\" height=\"108\" src=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/vscode-formatted.png\" alt=\"\" class=\"wp-image-356\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/vscode-formatted.png 321w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/vscode-formatted-300x101.png 300w\" sizes=\"auto, (max-width: 321px) 100vw, 321px\" \/><\/figure><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>You can give any name you would like to. This tutorial follows this naming convention.<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">step 2 : configure server<\/h4>\n\n\n\n<p>Setup the app.module.ts with two graphql modules, one for the normal server and another for the federation server.  We need to configure the module in such a way that the only federation module loads the <code><strong>.graphql.federation<\/strong><\/code> file and only normal module loads the <code><strong>graphql.normal<\/strong><\/code> file. The <strong><code>.graphql<\/code><\/strong> file has to be loaded by both modules.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">&nbsp;&nbsp;imports:&nbsp;[\n&nbsp;&nbsp;&nbsp;&nbsp;GraphQLModule.forRoot({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:&nbsp;false,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playground:&nbsp;true,\n&nbsp;     path:&nbsp;'<strong>\/graphql<\/strong>',\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typePaths:&nbsp;['.\/**\/*.<em>{graphql,graphql.normal}<\/em>'],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;installSubscriptionHandlers:&nbsp;true,\n&nbsp;&nbsp;&nbsp;&nbsp;}),\n&nbsp;&nbsp;&nbsp;&nbsp;GraphQLFederationModule.forRoot({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:&nbsp;false,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playground:&nbsp;false,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path:&nbsp;'<strong>\/graphql-federated<\/strong>',\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typePaths:&nbsp;['.\/**\/*.<em>{graphql,graphql.federation}<\/em>'],\n&nbsp;&nbsp;&nbsp;&nbsp;}),\n&nbsp;&nbsp;],<\/code><\/pre>\n\n\n\n<p>Notice that type paths for the two modules are different as per our convention. The normal graphql with subscription is now available at <code>\/grapqhl<\/code> and the  server for federation gateway is available at <code>\/graphql-federated<\/code><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><span style=\"color:#6d6e70\" class=\"has-inline-color\">We are not spinning two servers here. Its the same express server with two middleware configured for different paths. So there will not be any performance issues<\/span><\/em><\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">step 3 : the illusion<\/h4>\n\n\n\n<p>This is the most important step. There are some directives in graphql that only works in the federated mode and vice versa. You will finally end up in writing the custom version of the grapqhl model in the federated and the normal files. This will add the headache of duplicate graphql models in the application.<\/p>\n\n\n\n<p>This problem can be tackled in a easy way , using dummy directives !<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Define a declarative for \u201ckey\u201d<\/li><\/ul>\n\n\n\n<pre title=\"key.directive.ts\" class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">import { SchemaDirectiveVisitor } from 'apollo-server-express';\nimport { GraphQLField } from 'graphql';\n\n\/**\n * This is a dummy implementation of the key directive for the normal mode\n *\/\nexport class FakeKeyDirective extends SchemaDirectiveVisitor {\n  \/**\n   * Fake Key definition\n   * @param _field Field of graphql\n   *\/\n  visitFieldDefinition(_field: GraphQLField&lt;any, any&gt;) {\n    _field.args;\n  }\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Include it in the module<\/li><\/ul>\n\n\n\n<pre title=\"app.module.ts\" class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">@Module({\n  imports: [\n    GraphQLModule.forRoot({\n      debug: false,\n      playground: true,\n      path: '\/graphql',\n      typePaths: ['.\/**\/*.{graphql,graphql.normal}'],\n      installSubscriptionHandlers: true,\n\n      directiveResolvers: {\n        key: FakeKeyDirective,\n      },\n    }),\n    GraphQLFederationModule.forRoot({\n      debug: false,\n      playground: false,\n      path: '\/graphql-federated',\n      typePaths: ['.\/**\/*.{graphql,graphql.federation}'],\n    }),\n  ],\n  controllers: [AppController],\n  providers: [AppService, UsersResolver, UserService],\n})\nexport class AppModule {}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Define a fake implementation. This only has to work in the normal mode. So the file name has to end with \u201cgraphql.normal\u201d <\/li><\/ul>\n\n\n\n<pre title=\"key.graphql.normal\" class=\"wp-block-code\"><code lang=\"graphql\" class=\"language-graphql\">directive @key(fields: String) on OBJECT<\/code><\/pre>\n\n\n\n<p>Now you can define the model using the federation supported @key directive and the model works both in federation and normal graphql server.<\/p>\n\n\n\n<pre title=\"departments.graphql\" class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">type Department @key(fields: \"id\") {\n  id: ID!\n  name: String\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">thats all folks<\/h2>\n\n\n\n<p> Now you can start the federation gateway which listens to the the<code> \/graphql-federated<\/code> and the federation works. <\/p>\n\n\n\n<p>For subscription you can use any Websocket enabled gateways like nginx, istio etc and connect directly to the microservices<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">conclusion<\/h2>\n\n\n\n<p>Yes it is possible to enable federation and subscription for graphql in nestJS using a simple trick. More efficient than the <a href=\"https:\/\/www.apollographql.com\/blog\/backend\/federation\/using-subscriptions-with-your-federated-data-graph\/\" target=\"_blank\" rel=\"noreferrer noopener\">Apollo <\/a>method. You can download the entire code for your reference from my <a href=\"https:\/\/github.com\/vinodsr\/nestjs-graphql-federation-with-subscription\" target=\"_blank\" rel=\"noreferrer noopener\">github repo (https:\/\/github.com\/vinodsr\/nestjs-graphql-federation-with-subscription<\/a>)<a href=\"https:\/\/github.com\/vinodsr\/nestjs-graphql-federation-with-subscription\" target=\"_blank\" rel=\"noreferrer noopener\">.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bonustip\">bonus tip<\/h2>\n\n\n\n<p>Since you are using a separate file extension other than .graphql, your IDE won\u2019t give the native graphql formatting for the graphql.normal and graphql.federation files. There is a fix for this. Just enabled file associations for those extensions. <\/p>\n\n\n\n<p>This is how it looks like in VS Code. Adding the file extensions will give you syntax highlighting and code formatting for the normal and federation graphql. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"222\" src=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/image-2.png\" alt=\"\" class=\"wp-image-361\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/image-2.png 837w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/image-2-300x80.png 300w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2021\/07\/image-2-768x204.png 768w\" sizes=\"auto, (max-width: 837px) 100vw, 837px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Step by step instructions on how to enable subscription on a federated NestJS based  graphql server (apollo graphql server). <\/p>\n","protected":false},"author":1,"featured_media":388,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[109,107],"class_list":["post-339","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","tag-graphql","tag-nestjs","has-post-thumbnail-archive"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/comments?post=339"}],"version-history":[{"count":39,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/339\/revisions\/405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media\/388"}],"wp:attachment":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media?parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/categories?post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/tags?post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}