April 30, 2021
Édouard Bonlieu
@edouardb_
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.
To successfully follow and complete this tutorial, you need:
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.
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.
Fill the Docker image
field with hasura/graphql-engine
.
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.
In the Environment variables section, configure the environment variables required to properly run the Hasura GraphQL engine.
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.true
. This will expose the Hasura console and allow us to perform the configuration.HASURA_GRAPHQL_DATABASE_URL
, we strongly recommend using a secret to store this value.Give your App a name, i.e hasura-demo
, and click Create App.
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.
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 |
Once the form is properly completed, click the Add Table button.
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.
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.
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 is a developer-friendly serverless platform to deploy any apps globally.
Start for freeDeploy 2 services for free and enjoy our predictable pricing as you grow
Get up and running in 5 minutes