Skip to content

Instantly share code, notes, and snippets.

@GavinRay97
Last active September 30, 2020 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GavinRay97/28c94f9f23d318542fb310bbffff8d04 to your computer and use it in GitHub Desktop.
Save GavinRay97/28c94f9f23d318542fb310bbffff8d04 to your computer and use it in GitHub Desktop.
Hasura MySQL Test Setup

Put the docker-compose.yaml into a new folder, along with a folder called /migrations. Put the sample SQL in there at /migrations/any-name.sql. This will seed the MySQL DB on startup.

Run docker-compose up -d, and visit Hasura at http://localhost:8080

Note: If you would like to a connect to an existing MySQL DB running outside of Docker on localhost, you will need to edit the configuration for the Hasura service so that it uses host networking. Then use localhost for the --mysql-host argument. Also, in order for Hasura on localhost to connect to the Docker Postgres instance, you'll need to add 5432:5432 to the service port configurations for postgres so that it's exposed, then change the DB URL to postgres://postgres:postgrespassword@localhost:5432/postgres.

services:
  graphql-engine:
    # ...
    network_mode: "host"

Query Result

version: "3.6"
services:
mysql:
image: mysql:8.0
ports:
- "3306:3306"
expose:
- 3306
volumes:
- /var/lib/mysql
- ./migrations:/docker-entrypoint-initdb.d
environment:
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: root_password
postgres:
image: postgres:12
restart: always
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgrespassword
graphql-engine:
image: hasura/graphql-engine:pull5655-633f084f
ports:
- "8080:8080"
depends_on:
- "postgres"
command:
- graphql-engine
- --mysql-host
- mysql
- --mysql-port
- "3306"
- --mysql-user
- user
- --mysql-password
- password
- --mysql-dbname
- database
- serve
restart: always
environment:
HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
HASURA_GRAPHQL_DEV_MODE: "true"
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
volumes:
db_data:
CREATE TABLE IF NOT EXISTS todos (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=INNODB;
INSERT INTO todos (title)
VALUES ('Take out the trash'), ('Do the dishes');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment