Skip to content

Commit a817b07

Browse files
authored
Merge pull request #372 from defang-io/hasura-rds-example
Hasura Example
2 parents 530ded4 + 792907d commit a817b07

File tree

39 files changed

+346
-0
lines changed

39 files changed

+346
-0
lines changed

samples/other/hasura/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# GraphQL API with Hasura + Postgres
2+
3+
This sample project demonstrates how to deploy Hasura with Defang and connect it to a Postgres database. We also demonstrate how to run a Postgres container during development and how to switch over to a managed postgres service like RDS, Neon, or others in production. If you want to get a compatible database ready to go really quickly for free, [Neon](https://neon.tech/) is a quick and easy way to go. The sample populates the database with some sample data so you can quickly start playing with the Hasura console. It sets wide open permissions on the tables as well so you can start querying or mutating the data right away.
4+
5+
## Prerequisites
6+
1. Download <a href="https://github.com/defang-io/defang">Defang CLI</a>
7+
2. Have a managed database service configured and have the connection string ready.
8+
3. (optional) If you are using <a href="https://docs.defang.io/docs/concepts/defang-byoc">Defang BYOC</a>, make sure you have properly <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html">authenticated your AWS account</a>.
9+
4. (optional) [Install the Hasura CLI](https://hasura.io/docs/latest/hasura-cli/install-hasura-cli/) to create migrations and update metadata for your Hasura GraphQL api.
10+
11+
## Development
12+
13+
For development, we use a Postgres container. The Postgres container is defined in the `compose.dev.yml` file. The Hasura container is defined in the `compose.yml` file, with some overrides in the `compose.dev.yml` file so it can correctly connect to the development database container.
14+
15+
To start the development environment, run `docker compose -f ./compose.yml -f ./compose.dev.yml up`. This will start the Postgres container and the Hasura container. The Hasura console will be available at `http://localhost:8080` with the password `password`.
16+
**Note:** _If you want to make changes to your database, permissions, etc. you should use the Hasura console and the Hasura CLI to make those changes. See the next section for more information._
17+
18+
### Editing the database/permissions etc.
19+
20+
If you want to edit the database, permissions, or any other Hasura settings such that you can deploy them to production, you should [install the Hasura CLI](https://hasura.io/docs/latest/hasura-cli/install-hasura-cli/). Then, after starting the development environment, you can run `hasura console` _inside the `./hasura` directory_. This will open the Hasura console in your browser. Any changes you make in the console will be saved to the `migrations` and `metadata` directories. When you run `defang compose up` these changes will be applied to the production environment.
21+
22+
## Deploying
23+
1. Open the terminal and type `defang login`
24+
2. Add your connection string as a defang config value by typing `defang config set HASURA_GRAPHQL_DATABASE_URL` and pasting your connection string (which should be in the format `postgres://username:password@host:port/dbname`)
25+
3. Setup a password for hasura by typing `defang config set HASURA_GRAPHQL_ADMIN_SECRET` and adding a password you would like to login with.
26+
2. Type `defang compose up` in the CLI.
27+
3. Your app will be running within a few minutes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
postgres:
3+
image: postgres:16
4+
environment:
5+
- POSTGRES_USER=postgres
6+
- POSTGRES_PASSWORD=postgres
7+
- POSTGRES_DB=postgres
8+
volumes:
9+
- postgres_data:/var/lib/postgresql/data
10+
hasura:
11+
depends_on:
12+
- postgres
13+
restart: on-failure
14+
environment:
15+
- HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres
16+
- HASURA_GRAPHQL_ADMIN_SECRET=password
17+
volumes:
18+
- ./hasura:/hasura
19+
20+
volumes:
21+
postgres_data:

samples/other/hasura/compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: '3.7'
2+
name: hasura-rds
3+
services:
4+
hasura:
5+
build:
6+
dockerfile: Dockerfile
7+
context: ./hasura
8+
# If you want to add your own domain name in Defang BYOC, uncomment the following line and replace the domain name with your own
9+
# domainname: hasura.mycoolapp.com
10+
ports:
11+
- target: 8080
12+
published: 8080
13+
mode: ingress
14+
environment:
15+
- HASURA_GRAPHQL_DATABASE_URL
16+
- HASURA_GRAPHQL_ADMIN_SECRET
17+
- HASURA_GRAPHQL_ENABLE_CONSOLE=true
18+
- HASURA_GRAPHQL_UNAUTHORIZED_ROLE=public
19+
- HASURA_GRAPHQL_EXPERIMENTAL_FEATURES=naming_convention
20+
- HASURA_GRAPHQL_DEFAULT_NAMING_CONVENTION=graphql-default
21+
- HASURA_GRAPHQL_MIGRATIONS_DIR=/hasura/migrations
22+
- HASURA_GRAPHQL_METADATA_DIR=/hasura/metadata
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM hasura/graphql-engine:v2.39.0.cli-migrations-v3.ubuntu
2+
3+
COPY . /hasura
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 3
2+
endpoint: http://localhost:8080
3+
metadata_directory: metadata
4+
admin_secret: password
5+
actions:
6+
kind: synchronous
7+
handler_webhook_baseurl: http://localhost:3000

samples/other/hasura/hasura/metadata/actions.graphql

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
actions: []
2+
custom_types:
3+
enums: []
4+
input_objects: []
5+
objects: []
6+
scalars: []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)