How to get started with Hasura and GraphQL on DigitalOcean

This is a quick blog post about how to get up and running with Hasura, GraphQL on DigitalOcean.

Hasura is probably my current favorite technology when it comes to handleing backend mechanisms for an web app. Hasura is just awesome! So, what is Hasura?

Hasura is an Instant Realtime GraphQL on Postgres

What it means is that Hasura will act as a middle layer between your database in PostgreSQL and your app. Your app will communicate with your database via GraphQL. Hasura will handle everything between your app and PostgreSQL. That includes:

  • Get, post, update and delete data.
  • Handle subscriptions so you can retreive real time data from your Postgres database.
  • Handle access control in the form of roles (ex: admin or user roles).
  • Build and manage your data in Postgres via Hasuras web interface, much like phpMyAdmin.
  • And much more. Read more on hasura.io.

Setup

I used the one One-click apps at Digital Ocean. I am running my instance of Hasura on a $5/month droplet, which is just amazing!

Install Hasura GraphQL using One-click app at Digital Ocean

This will automatically install Hasura Engine, Postgres and Caddy on a Digital Ocean droplet for you. For the rest of this tutorial we will interact with the droplet using SSH and run some command to secure Hasura.

Logging in to the server and running docker container ps we can see that all services is up an running.

Docker containers running Hasura Engine, Postgres and Caddy

SSL/TLS (https:// and wss://)

We want to secure our communication to the server and we will let Caddy and Let’s encrypt to set up a SSL/TSL certificate for us so we can use both HTTPS (Hypertext Transfer Protocol Secure) and WSS (WebSocket Secure).

We will add a subdomain pointing to our newly created droplet. In DigitalOcean this will look like this:

Add a sub domain pointing to our Hasura instance

Notice, it could take up to 48 hours for DNS changes to take affect.

Next we will go back to our droplet and edit our Caddy config file located at /etc/hasura/Caddyfile. Edit it to look like:

hasura.your-domain.com {
    proxy / graphql-engine:8080 {
        websocket
    }
}

Next, restart the container with docker restart 'container-id' (with out the ticks('). The container-id can be found using docker ps and is the left most column. In the image above my Caddy-container had the id of e0d614783237.

Access key

Open the file /etc/hasura/docker-compose.yaml to edit the HASURA_GRAPHQL_ACCESS_KEY variable. In the example below I set my access key to mysecretaccesskey.

 environment:
   HASURA_GRAPHQL_ACCESS_KEY: mysecretaccesskey
   HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:@postgres:5432/postgres
 command:

Next, browse to your newly created and secure Hasura console at https://your-domain.com. You will be prompted with a login screen. Use your HASURAGRAPHQLACCESS_KEY to login.

Up and running

You are now up and running with Hasura on DigitalOcean. Let me know in the comments below if you would like to read more in depth blog posts about Hasura, Apollo and React.