Build a Powerful GraphQL API with Postgres in Under 10 Minutes
Tags: graphql, hasura, docker
Published: 2023-04-07 18:55:59

{% embed https://youtu.be/oX51rm3FsEo %}

In this tutorial, you'll learn how to create a powerful GraphQL API using a relational database model and Postgres in under 10 minutes! We'll be setting up a Hasura engine, connecting it to a database, and exposing data through GraphQL. This is a great way to quickly develop a robust and scalable API for your applications.

## Setting up Hasura Engine and Postgres Database

To start, we'll be using a tool called Hasura, which will serve as our GraphQL server. It requires a Postgres database, which we'll create using a Docker Compose file. This file will specify the Postgres version and the Hasura engine version we'll be using, as well as the ports and configurations needed to connect our database to the GraphQL API.

First, create a docker-compose.yml file with the following content:

```
version: '3.6'
services:
  postgres:
    image: postgres:12
    restart: always
    ports:
    - "5432:5432"
    volumes:
    - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgrespassword
  graphql-engine:
    image: hasura/graphql-engine:v2.21.0
    ports:
    - "8080:8080"
    depends_on:
    - "postgres"
    restart: always
    environment:
      ## postgres database to store Hasura metadata
      HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
      ## this env var can be used to add the above postgres database to Hasura as a data source. this can be removed/updated based on your needs
      PG_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
      ## enable the console served by server
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
      ## enable debugging mode. It is recommended to disable this in production
      HASURA_GRAPHQL_DEV_MODE: "true"
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      ## uncomment next line to run console offline (i.e load console assets from server instead of CDN)
      # HASURA_GRAPHQL_CONSOLE_ASSETS_DIR: /srv/console-assets
      ## uncomment next line to set an admin secret
      # HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
volumes:
  db_data:
```

Replace `postgrespassword` with a secure password of your choice. After that, run the following command in your terminal:

```
docker-compose up -d  
```

This command will pull the required images and start the services. Once everything is running, you can access the Hasura console on `localhost:8080`.

## Setting up the Database

We'll use a tool called Postico to connect to our Postgres database. Enter your database credentials (host, user, and password) and connect to the database. Create a new database called workouts_dev and configure Hasura to connect to this database.

Now, we'll create the necessary tables in our database. In this example, we're creating tables for users, workouts, workouts_exercises, and exercises.

```
CREATE TABLE users (
  id character varying(50) PRIMARY KEY,
  full_name character varying(256)
);

CREATE TABLE workouts (
    workout_id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    user_id character varying(50) (null),
    title character varying(256),
    description text
);

CREATE TABLE workouts_exercises (
    workout_id uuid REFERENCES workouts(workout_id),
    exercise_id uuid REFERENCES exercises(exercise_id),
    repetitions smallint DEFAULT 10,
    rest_time smallint DEFAULT 30
);

CREATE TABLE exercises (
    exercise_id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    image_url character varying(256),
    title character varying(256),
    description text,
    muscles character varying(50)[]
);
```

After setting up the tables and their relationships, our database is ready to be used with the GraphQL API.

## Setting up the GraphQL API

With the database connected to Hasura, we can start tracking the tables in our GraphQL API. This will allow us to access and manipulate the data in these tables using GraphQL queries and mutations.

To track the tables, go to the Hasura console, navigate to the database settings, and click on "Reload All Databases." You should now see the tables you created earlier. Click on "Track" for each table to add them to your GraphQL API.
Once the tables are tracked, you can start writing GraphQL queries and mutations to interact with your data. In this example, we inserted a new exercise and queried the data using GraphQL.

## Wrapping Up

As you can see, it's easy to set up a powerful GraphQL API with a Postgres database using Hasura. In the next part of this tutorial series, we'll dive deeper into configuration options and cover how to integrate the