Deploy the Hasura GraphQL Engine to Expose and Create APIs from your Databases on Koyeb
Tags: docker, graphql, tutorial, webdev
Published: 2021-05-17 07:13:18


## Introduction

The Hasura GraphQL engine allows you to create and expose GraphQL APIs from your databases. Using the Hasura GraphQL engine you can focus on building your data schema structure
allowing you to build modern applications faster.

The Hasura GraphQL engine comes with native, useful primitives such as JWT authentication, authorization to restrict user access to a specific subset of data, event triggers to capture events happening on specific tables
and invoke a webhook to handle and perform a processing operation, and much more.


## Requirements

 To successfully follow and complete this tutorial, you need:
 - A Postgres database to use as the Hasura GraphQL engine backend.
 - A [Koyeb account](//app.koyeb.com) to deploy and run the Hasura GraphQL engine.

## Steps

In this guide, we will focus on how to deploy the Hasura GraphQL engine on the Koyeb serverless platform and build a simple GraphQL API using PostgreSQL as a backend.

1. [**Deploy the Hasura GraphQL engine on Koyeb**](#deploy-the-hasura-graph-ql-engine-on-koyeb)
2. [**Try out Hasura**](#try-out-hasura)
4. [**Conclusion**](#conclusion)


## Deploy the Hasura GraphQL Engine on Koyeb

To deploy the Hasura GraphQL, we use the `hasura/graphql-engine` Docker image. On the Koyeb control panel, click the **Creat App** button.
You land on the Koyeb App creation form.

1. Fill the `Docker image` field with `hasura/graphql-engine`.
2. In the *Ports* section, change the export port from `80` to `8080`, which is the port the `hasura/graphql-engine` Docker image app is listening on. This setting is required to let Koyeb know which port your application is listening to and properly route incoming HTTP requests. If you want the Hasura GraphQL engine to be available on a specific path, you can change the default one (`/`) to the path of your choice.
3. In the *Environment variables* section, configure the environment variables required to properly run the Hasura GraphQL engine.

    - HASURA_GRAPHQL_DATABASE_URL: The environment variable containing the PostgreSQL URL, i.e. `postgres://<user>:<password>@<host>:<port>/<database>`. To store this value which contains sensitive information, we strongly recommend configuring the environment variable using Koyeb secrets instead of storing it as a plaintext value. Secrets are encrypted at rest. They are ideal to store add sensitive data like authentication tokens, OAuth tokens, etc.
    - HASURA_GRAPHQL_ENABLE_CONSOLE: Set to `true`. This will expose the Hasura console and allow us to perform the configuration.
    - HASURA_GRAPHQL_ADMIN_SECRET: The secret to access the Hasura Graphql admin. As for the `HASURA_GRAPHQL_DATABASE_URL`, we strongly recommend using a secret to store this value.

4. Give your App a name, i.e `hasura-demo`, and click **Create App**.

## Try out Hasura

### Create a table

Open the Hasura console by clicking your App URL on the Koyeb control panel, in my case: `hasura-ed.koyeb.com`.
You land on the Hasura console where you need to log in using your admin secret.

Once logged in, click the **Data** tab on the top navigation bar of the Hasura console and click the **Create Table** button.
In this guide, we will create a todo table to showcase how to interact with data.

- Table name: todo
- Columns:

|Name|Type|Default value|Nullable|Unique|
|---|---|---|---|---|
|id|UUID|gen_random_uuid()|False|True|
|task|Text||False|False|
|status|Text||False|False|
|created_at|Date|now()|False|False|
|updated_at|Date|now()|False|False|

- Primary key: id

Once the form is properly completed, click the **Add Table** button.

### Insert some data into the todo table

On the Hasura control panel, click the **GraphiQL* tab.

To insert data into our table, we use GraphQL mutations. Mutations are used to create, update and delete data on the server.

In the editor paste the following snippet to create our first todo task and click the **Play** button.

```graphql
mutation toto {
  insert_todo_one(object: {task: "My first task"}) {
    id
    status
    created_at
    task
    updated_at
  }
}
```

This query inserts only one todo task *My first task* and returns the todo task id, task, created_at, update_at, and status.

### Retrieve our todo tasks

Now to retrieve our todo tasks, execute the following GraphQL query:


```graphql
query getTasks {
  todo {
    id,
    task,
    created_at,
    updated_at,
    status
  }
}
```

This query retrieves all todo tasks and returns for each its id, task, created_at, update_at, and status.

## Conclusion

In this guide, we discover how to deploy the Hasura GraphQL engine and koyeb, created a todo table, and perform basic GraphQL queries to insert and retrieve data.
Hasura provides great features not covered in this guide that we probably cover in the next tutorial.

To go deeper and learn more about how Hasura works, we recommend you to check out the Hasura official [documentation](https://hasura.io/docs/latest/graphql/core/index.html)

If you have any suggestions or feedback about this tutorial or would like to see a specific topic covered, do not hesitate to join us on [Slack](https://slack.koyeb.com).