Skip to main content

PostgreSQL 14 Database Refresher :: Working with GraphQL

Shenzhen, China

Hasura GraphQL

The Hasura GraphQL engine makes your data instantly accessible over a real-time GraphQL API, so you can build and ship modern apps and APIs faster. Hasura connects to your databases, REST servers, GraphQL servers, and third party APIs to provide a unified realtime GraphQL API across all your data sources.

This guide will help you get the Hasura GraphQL engine and a Postgres database to store its metadata running as Docker containers using Docker Compose. This is the easiest way to set up Hasura GraphQL engine on your local environment.

docker-compose

This sets up both Postgres v12 and Hasura GraphQL:

The hasura/graphql-engine/install-manifests repo contains all installation manifests required to deploy Hasura anywhere. Get the docker compose file from there:

curl https://raw.githubusercontent.com/hasura/graphql-engine/stable/install-manifests/docker-compose/docker-compose.yaml -o docker-compose.yml

The following command will run Hasura GraphQL engine along with a Postgres database to store its metadata.

docker-compose up -d

docker

This works without issues with my Postgres v14 docker installation on host 192.168.2.111:

docker run -d -p 8080:8080 \
  -e HASURA_GRAPHQL_DATABASE_URL=postgresql://postgres:secretpassword@192.168.2.111:5432/message_boards \
  -e HASURA_GRAPHQL_ENABLE_CONSOLE=true --name=hasura --rm \
  hasura/graphql-engine:latest

After the container is started visit the IP of your docker host on port 8080. Go to data and track all your data:

Postgres Hasura GraphQL

I can now query the data from my PG database using Graph queries in GraphiQL:

{
  boards(where: { board_id: { _eq: 39 } }) {
    board_id
    board_name
    board_description
  }
  comments(where: { board_id: { _eq: 39 } }) {
    board_id
    comment
    comment_id
    time
    user_id
  }
}

Postgres Hasura GraphQL