DEV Community

Joseph Sutton
Joseph Sutton

Posted on • Edited on

1

Polyglot Microservices: Federated GraphQL Subscriptions in Golang, Rust, and Nest.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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay