Logo

dev-resources.site

for different kinds of informations.

Build a Powerful GraphQL API with Postgres in Under 10 Minutes

Published at
4/7/2023
Categories
graphql
hasura
docker
Author
imekinox
Categories
3 categories in total
graphql
open
hasura
open
docker
open
Author
8 person written this
imekinox
open
Build a Powerful GraphQL API with Postgres in Under 10 Minutes

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:
Enter fullscreen mode Exit fullscreen mode

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

docker-compose up -d  
Enter fullscreen mode Exit fullscreen mode

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)[]
);
Enter fullscreen mode Exit fullscreen mode

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

hasura Article's
30 articles in total
Favicon
Convert insert mutation to upsert
Favicon
refinedev - hasura (nested/multiple query_root)
Favicon
From Idea to Launch: My 30-Day MVP Journey
Favicon
How to Build a GraphQL API for MongoDB Using Hasura in Six Steps
Favicon
How to Build a Supergraph using Snowflake, Neon PostgreSQL, and Hasura in Five Steps
Favicon
Streamlining CI/CD Pipelines with Hasura GraphQL Engine
Favicon
Supabase over Hasura for 2024?
Favicon
Hasura and Keycloak integration with NestJS server
Favicon
Modern API Development (Part 1)
Favicon
Build a graphQL API with Hasura low-code platform
Favicon
Startup Starter Kit
Favicon
Hasura x MEWS
Favicon
Hasura vs Apollo: Comparing GraphQL Platforms
Favicon
Hasura and Nhost vs Supabase
Favicon
How to monitor URQL performance and link with database queries ?
Favicon
Build a Powerful GraphQL API with Postgres in Under 10 Minutes
Favicon
Guide to Side effects in Hasura
Favicon
Hasura: Building Scalable and Real-Time Applications - An Extensive Guide
Favicon
Hasura Cloud: Building Scalable and Secure GraphQL APIs Made Easy
Favicon
How to avoid messing up squash migration in Hasura
Favicon
Appwrite vs. Hasura vs. Apollo
Favicon
Auth0, Hasura, Social Media Login
Favicon
Deno & Hasura for app development
Favicon
SSR: clerk with hasura
Favicon
Hasura Custom Authentication Using JWT
Favicon
Unable to HASURA_GRAPHQL_JWT_SECRET in docker-compose file
Favicon
Hasura Storage in Go: 5x performance increase and 40% less RAM
Favicon
Using Authorizer with Hasura
Favicon
Hasura + Supertokens
Favicon
Creating a todo web app in < 112 lines of code with Hasura and Python

Featured ones: