Inter-service communication on your Hasura backend

This is a quick tutorial of how 1 API service can internally contact another API service in a Hasura backend.

Clients make request to an API service that makes a request to an internal API service to respond.

The first service, titled api is accessible to clients. Clients make a request to the endpoint /hello. api then makes a request to a service titled api2 on the /respond endpoint. Once api receives a response from api2 it responds to the client.

Let’s see how we’d create these services on Hasura. For a tl;dr, head to the summary directly.

Create and deploy the 'api' service

Head to the add service page, and add a custom git push based service.

Create a new service called “api”.

Let’s write our code and git push away!

To deploy code, add your SSH public key to the advanced settings page in the console.

Adding your SSH public keys via the Hasura console

Next, create a nodejs-express git repository that can easily be git pushed. Use the https://github.com/hasura/quickstart-docker-git repo to get started quickly.

Use quickstart-docker-git to get a nodejs-express base repo

my-node-api is going to be source code behind our api service.

Let’s modify my-node-api/app/src/server.js to make an API request to api2.

server.js for the api service

To push this code, first initialise the git repo:

cd my-node-api/app/src
npm install --save isomorphic-fetch #Adds fetch to package.json
echo '.node_modules' >> .gitignore
cd ../../
git init
git add .
git commit -m 'My first commit'

Then add the hasura server as a git remote:

git remote add hasura ssh://hasura@api.epaulet34.hasura-app.io/~/git/api/
git push hasura master

Let’s test this out:

The “api” service is not able to respond successfully because there’s no “api2” service available yet

This is expected because we don’t have api2 deployed yet.

Let’s look at the logs:

Error logs of the “api” service

Everything as expected. api throws an error because it can’t reach api2.

Deploy code for the “api2” service

Head to the add service page, and add a git push service titled api2. However, since we don’t want api2 accessible to web clients, let’s restrict access to only the admin role. This will not allow any requests from users (whether logged in or not) to reach api2.

Create the “api2” service via the Hasura project console

Next, create another nodejs-express git repository that can easily be git pushed. Again, use the base repo from https://github.com/hasura/quickstart-docker-git to get started quickly.

Use the quickstart-docker-git repo to create another nodejs-express repo for “api2”

my-node-api2 is going to be the source code behind our api2 service.

Let’s modify my-node-api2/app/src/server.js to have a /respond endpoint:

server.js code for “api2”

To push this code, first initialise the git repo:

cd my-node-api2
git init
git add .
git commit -m 'My first commit'

Then add the hasura server as a git remote:

git remote add hasura ssh://hasura@api.epaulet34.hasura-app.io/~/git/api2/
git push hasura master

Let’s test this out from an incognito window:

Clients outside the cluster cannot access the “api2” service

This is expected because api2‘s endpoints should not be accessible to any user (except admin).

Let’s query api at /hello again:

Because “api2” is up, “api” now returns a successful response.

Everything as expected! api contacts api2 and responds successfully.

Summary:

  1. To create an internal service, restrict the role to admin during service creation on the Hasura console.
  2. To contact one service from another use the “internal endpoint”. This is the internal DNS label. Eg: http://api2.default/respond will resolve to the api2 service from inside the cluster. api2 in the hostname comes from the name of the server, and default comes from the kubernetes namespace that they are running in. Internal endpoints for every service are available on the Manage page.

Add backend APIs to your apps in minutes with Hasura. Check it out here: https://hasura.io

Check out more Hasura tutorials here: https://blog.hasura.io/tutorials/home

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.