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.
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.
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.
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.
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.
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 masterLet’s test this out:
This is expected because we don’t have api2 deployed yet.
Get Tanmai Gopal’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s look at the logs:
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.
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.
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:
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 masterLet’s test this out from an incognito window:
This is expected because api2‘s endpoints should not be accessible to any user (except admin).
Let’s query api at /hello again:
Everything as expected! api contacts api2 and responds successfully.
Summary:
- To create an internal service, restrict the role to
adminduring service creation on the Hasura console. - To contact one service from another use the “internal endpoint”. This is the internal DNS label. Eg:
http://api2.default/respondwill resolve to theapi2service from inside the cluster.api2in the hostname comes from the name of the server, anddefaultcomes 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









