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

Permit.io Launch week

Permit CLI Launch Week is coming πŸ”œ

Fully CLI-Based Integrated Authorization - for devs who live in their terminal. Subscribe for daily livestreams, swag giveaways, and exciting new releases.

Sign up

Top comments (0)

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay