DEV Community

Joseph Sutton
Joseph Sutton

Posted on

Polyglot Microservices: Federated Subscriptions in Golang, Rust, and Node.js, Final

Series Navigation

  1. Golang Microservices (spells)
  2. Rust Microservice (messages)
  3. Node.js Microservice (players)
  4. Gateway

Hive Gateway (Node.js)

Now, let's tie it all together with the Hive Gateway, made by the awesome team, "TheGuild" over on GitHub.

First, let's create the node project with Nx:

nx g @nx/node:application
Enter fullscreen mode Exit fullscreen mode

Install a few dependencies:

pnpm i @graphql-hive/gateway graphql-ws ws
Enter fullscreen mode Exit fullscreen mode

Add a config in src/gateway.config.ts:

import { defineConfig, LogLevel } from '@graphql-hive/gateway';

export const gatewayConfig = defineConfig({
  port: 8000,
  supergraph: 'apps/gateway/supergraph.graphql',
  logging: LogLevel.debug,
  transportEntries: {
    '*.http': {
      options: {
        subscriptions: {
          kind: 'ws',
        },
      },
    },
  },
});
Enter fullscreen mode Exit fullscreen mode

Let's add a few commands to this project, too:

...
    "hive-gateway": {
      "dependsOn": ["supergraph-compose"],
      "executor": "nx:run-commands",
      "options": {
        "command": "node_modules/.bin/hive-gateway supergraph -c apps/gateway/src/gateway.config.ts"
      }
    },
    "supergraph-compose": {
      "executor": "nx:run-commands",
      "options": {
        "command": "rover supergraph compose --elv2-license=accept --config apps/gateway/supergraph.json --output apps/gateway/supergraph.graphql"
      }
    },
...
Enter fullscreen mode Exit fullscreen mode

Add a supergraph config for supergraph.json:

{
  "federation_version": "=2.9.0",
  "subgraphs": {
    "spells": {
      "schema": {
        "subgraph_url": "http://localhost:8080/query"
      }
    },
    "messages": {
      "schema": {
        "subgraph_url": "http://localhost:8081"
      }
    },
    "players": {
      "schema": {
        "subgraph_url": "http://localhost:8082/graphql"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Okay, let's test this out and run the subgraphs first:

nx run-many -t=serve -p player-service message-service spell-service
Enter fullscreen mode Exit fullscreen mode

Now, let's run the gateway:

NOTE: rover CLI is required for this step.

nx run gateway:hive-gateway
Enter fullscreen mode Exit fullscreen mode

You should be able to run and execute Subscriptions through the gateway! Nice! You've now got a polyglot federated supergraph with subscriptions!

If you'd like to see the code, check it out here: https://github.com/sutt0n/polyglot-fed-gql

Top comments (0)