plusteam
Published in

plusteam

Hasura GraphQL Practical Guide

Hasura console

Technology Stack

  1. git: Code version management
  2. docker: Tool to build and manage containers
  3. docker-compose: Tool to manage several containers
  4. NodeJS and npm: Node is used only to set up Hasura actions and triggers.

Quick Start

Steps to set up the project

  1. Clone the repository:
    git clone https://github.com/jsilversun/hasura_tutorial
  2. Install the dependencies:
    npm install
  3. Clone the Pagila SQL scripts to the database folder
    npm run database:clone
  4. Run npx hasura init hasura-tutorial --admin-secret secret afterward, you will see a new folder named hasura-tutorial this will contain all the settings you need for your particular Hasura instance like metadata, actions, and migrations
  5. Run docker-compose up -d to start the docker containers specified in the docker-compose.yml file

Set up the GraphQL API

  1. Go to the Data tab
  2. Click on Track all tables
  3. Click on Track all relations

Basics

Query

query getActors {
actor {
actor_id
first_name
last_name
}
}
query getActorById {
actor_by_pk(actor_id: 1) {
actor_id
first_name
last_name
}
}
query getActors {
actor_by_pk(actor_id: 1) {
actor_id
first_name
last_name
film_actors {
film {
film_id
description
}
}
}
}
query getFilmsFrom {
film(where: {replacement_cost: {_lt: 10}}) {
film_id
description
replacement_cost
}
}

Aggregation queries

query {
film_aggregate {
aggregate {
avg {
rental_rate
}
}
}
}

Subscription

subscription getActorById {
actor_by_pk(actor_id: 1) {
actor_id
first_name
last_name
}
}
mutation updateActorById {
update_actor_by_pk(
pk_columns: {
actor_id: 1
},
_set: {
first_name: "PENELOPE"
}
) {
actor_id
first_name
last_name
}
}

Authentication

Authentication

  1. To get a JWT for the staff role visit the URL https://hasura-guide.herokuapp.com/staff and copy the token
  2. To get a JWT for the customer role visit the URL https://hasura-guide.herokuapp.com/customer and copy the token
  3. With each one of the tokens, go to the Hasura console
  4. Add anAuthorization header and paste the token like this: Bearer <pasted_token>
  5. Press the Decode JWT button shown in the screenshot below
Extract from the docker-compose.yml file to see how the HASURA_GRAPHQL_JWT_SECRET is passed to the GraphQL Engine

Authorization

{
"_and": [
{
"active": {
"_eq": 1
}
},
{
"email": {
"_eq": "X-Hasura-User-Email"
}
}
]
}
{
"customer": {
"email": {
"_eq": "X-Hasura-User-Email"
}
}
}
{
customer {
customer_id
email
first_name
last_name
rentals {
rental_date
}
}
}
{
rental {
customer_id
rental_id
rental_date
return_date
}
}

Migrations

ALTER TABLE "public"."customer" ADD COLUMN "activebool" bool DEFAULT true NOT NULL;

Metadata

Actions

Triggers

Conclusion

--

--

Stories about development, technology and our experience creating wonderful apps.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store