All tutorials
Tutorial

Deploy the Hasura GraphQL Engine to Expose and Create APIs from your Databases on Koyeb

4 min

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 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
  2. Try out Hasura
  3. Conclusion

Deploy the Hasura GraphQL Engine on Koyeb

On the Overview tab of the Koyeb control panel, click Create Web Service to begin:

  1. Choose Docker as the deployment method.
  2. Enter hasura/graphql-engine as the Docker image.
  3. In the Environment variables section, click Add variable to add the following variables:
    • 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. In the Exposed ports section, change the existing entry to 8080.
  5. Choose a name for your App and Service, for example hasura-demo, and click Deploy.

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:
NameTypeDefault valueNullableUnique
idUUIDgen_random_uuid()FalseTrue
taskTextFalseFalse
statusTextFalseFalse
created_atDatenow()FalseFalse
updated_atDatenow()FalseFalse
  • 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.

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:

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

Questions or suggestions to improve this guide? Join us on the community platform to chat!

Koyeb

Welcome to Koyeb

Koyeb is a developer-friendly serverless platform to deploy any apps globally.

  • Start for free, pay as you grow
  • Deploy your first app in no time
Start for free
The fastest way to deploy applications globally.