├── .env.example ├── .gitignore ├── LICENSE ├── README.md ├── asset ├── graphql-msa.svg └── hasura-starter.svg ├── docker-compose.yml └── hasura ├── config.yaml └── migrations └── 1573631107183_init ├── up.sql └── up.yaml /.env.example: -------------------------------------------------------------------------------- 1 | # Hasura 2 | 3 | HASURA_ENDPOINT_PROTOCOL=http 4 | ## This can be either IP or hostname(domain). 5 | HASURA_ENDPOINT_IP=localhost 6 | ## Sync port with hasura/config.yaml for local development environment 7 | HASURA_ENDPOINT_PORT=8080 8 | HASURA_ENDPOINT=${HASURA_ENDPOINT_PROTOCOL}://${HASURA_ENDPOINT_IP}:${HASURA_ENDPOINT_PORT} 9 | ## Docs for authentication: https://docs.hasura.io/1.0/graphql/manual/auth/authentication/index.html 10 | HASURA_GRAPHQL_ADMIN_SECRET=helloworld 11 | ## Docs for logs: https://docs.hasura.io/1.0/graphql/manual/deployment/logging.html 12 | HASURA_GRAPHQL_ENABLED_LOG_TYPES=startup, http-log, webhook-log, websocket-log, query-log 13 | ## Set to "false" to disable console or for manual [migration](https://docs.hasura.io/1.0/graphql/manual/migrations/existing-database.html) 14 | HASURA_GRAPHQL_ENABLE_CONSOLE=true 15 | 16 | # Database 17 | 18 | POSTGRES_ENDPOINT_IP=localhost 19 | POSTGRES_ENDPOINT_PORT=5432 20 | POSTGRES_DATABASE=test 21 | POSTGRES_USERNAME=root 22 | POSTGRES_PASSWORD=toor 23 | ## example for other timezones notation: DB_TIMEZONE=Asia/Seoul 24 | POSTGRES_TIMEZONE=utc -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env* 2 | !.env.example 3 | 4 | # Created by https://www.gitignore.io/api/intellij,visualstudiocode 5 | # Edit at https://www.gitignore.io/?templates=intellij,visualstudiocode 6 | 7 | ### Intellij ### 8 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 9 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 10 | 11 | # User-specific stuff 12 | .idea/**/workspace.xml 13 | .idea/**/tasks.xml 14 | .idea/**/usage.statistics.xml 15 | .idea/**/dictionaries 16 | .idea/**/shelf 17 | 18 | # Generated files 19 | .idea/**/contentModel.xml 20 | 21 | # Sensitive or high-churn files 22 | .idea/**/dataSources/ 23 | .idea/**/dataSources.ids 24 | .idea/**/dataSources.local.xml 25 | .idea/**/sqlDataSources.xml 26 | .idea/**/dynamic.xml 27 | .idea/**/uiDesigner.xml 28 | .idea/**/dbnavigator.xml 29 | 30 | # Gradle 31 | .idea/**/gradle.xml 32 | .idea/**/libraries 33 | 34 | # Gradle and Maven with auto-import 35 | # When using Gradle or Maven with auto-import, you should exclude module files, 36 | # since they will be recreated, and may cause churn. Uncomment if using 37 | # auto-import. 38 | # .idea/modules.xml 39 | # .idea/*.iml 40 | # .idea/modules 41 | # *.iml 42 | # *.ipr 43 | 44 | # CMake 45 | cmake-build-*/ 46 | 47 | # Mongo Explorer plugin 48 | .idea/**/mongoSettings.xml 49 | 50 | # File-based project format 51 | *.iws 52 | 53 | # IntelliJ 54 | out/ 55 | 56 | # mpeltonen/sbt-idea plugin 57 | .idea_modules/ 58 | 59 | # JIRA plugin 60 | atlassian-ide-plugin.xml 61 | 62 | # Cursive Clojure plugin 63 | .idea/replstate.xml 64 | 65 | # Crashlytics plugin (for Android Studio and IntelliJ) 66 | com_crashlytics_export_strings.xml 67 | crashlytics.properties 68 | crashlytics-build.properties 69 | fabric.properties 70 | 71 | # Editor-based Rest Client 72 | .idea/httpRequests 73 | 74 | # Android studio 3.1+ serialized cache file 75 | .idea/caches/build_file_checksums.ser 76 | 77 | ### Intellij Patch ### 78 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 79 | 80 | # *.iml 81 | # modules.xml 82 | # .idea/misc.xml 83 | # *.ipr 84 | 85 | # Sonarlint plugin 86 | .idea/**/sonarlint/ 87 | 88 | # SonarQube Plugin 89 | .idea/**/sonarIssues.xml 90 | 91 | # Markdown Navigator plugin 92 | .idea/**/markdown-navigator.xml 93 | .idea/**/markdown-navigator/ 94 | 95 | ### VisualStudioCode ### 96 | .vscode/* 97 | !.vscode/settings.json 98 | !.vscode/tasks.json 99 | !.vscode/launch.json 100 | !.vscode/extensions.json 101 | 102 | ### VisualStudioCode Patch ### 103 | # Ignore all local history of files 104 | .history 105 | 106 | # End of https://www.gitignore.io/api/intellij,visualstudiocode 107 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 jjangga0214 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![hasura-starter](./asset/hasura-starter.svg) 2 | 3 | - A boilerplate for `docker-compose` and `migrations`, setting development environment. 4 | - A cheatsheet for Hasura CLI. 5 | - A guide for beginners. 6 | 7 | [![license](https://img.shields.io/badge/license-MIT-ff4081.svg?style=flat-square&labelColor=black)](./LICENSE) 8 | [![Hasura v1.1.0](https://img.shields.io/badge/Hasura-v1.1.0-18ffff.svg?style=flat-square&labelColor=black)](./docker-compose.yml) 9 | [![pr welcome](https://img.shields.io/badge/PRs-welcome-09FF33.svg?style=flat-square&labelColor=black)](https://github.com/jjangga0214/hasura-starter/pulls) 10 | 11 | ## Before Getting Started 12 | 13 | Clone this repo. 14 | 15 | Copy and edit **.env.example** file as you want. 16 | 17 | ```bash 18 | cp .env.example .env 19 | ``` 20 | 21 | The file uses **Variable Expansion** format to reduce duplication, which [`dotenv-expand`](https://github.com/motdotla/dotenv-expand) supports. `docker-compose` doesn't officially mention if it supports the format. It simply says it expects `VAR=VAL`([docs](https://docs.docker.com/compose/env-file/)). However, it seems `docker-compose` does work with the format. If not, you can use a tool like [`dotenv-cli`](https://github.com/entropitor/dotenv-cli) for populating "expanded" values. (e.g. `dotenv -- docker-compose up -d`) 22 | 23 | Note that you should uncomment `HASURA_GRAPHQL_ADMIN_SECRET` from [docker-compose.yml](docker-compose.yml) and configure it from env file, if you want to enable authentication (Refer to [docs](https://docs.hasura.io/1.0/graphql/manual/auth/authentication/index.html) for more detail). 24 | 25 | Default ports are `8080` for Hasura, `5432` for Postgres. Before you change them, consider reading [Networking](#Networking) section. 26 | 27 | ## Getting Started 28 | 29 | Run Postgres and Hasura. 30 | 31 | ```bash 32 | docker-compose up -d 33 | # if you name the env file differently, 34 | # you can set "env_file" field for docker-compose (https://docs.docker.com/compose/environment-variables) 35 | # or use a tool like "dotenv-cli" (https://github.com/entropitor/dotenv-cli) 36 | dotenv -e .env.dev -- docker-compose up -d 37 | ``` 38 | 39 | Stop/Debug/Clear Postgres and Hasura. 40 | 41 | ```bash 42 | # stop hasura and postgres 43 | docker-compose down 44 | # print logs on the terminal. Good for debugging. 45 | docker-compose up 46 | # stop hasura and postgres, and remove docker volumes as well 47 | docker-compose down --volumes 48 | ``` 49 | 50 | ## CLI Cheatsheet 51 | 52 | Here are `hasura` CLI cheatsheets for easy copy-and-paste. If you haven't used CLI, first be familar with [migrations](https://deploy-preview-3042--hasura-docs.netlify.com/graphql/manual/migrations/existing-database.html) concept. 53 | 54 | There are more available commands. Refer to [docs](https://docs.hasura.io/1.0/graphql/manual/hasura-cli/index.html) or `hasura --help` for more detail. 55 | 56 | `--project` option specifies a directory Hasura CLI should target. If you go into the directory(`cd hasura`), you don't need to specify it. 57 | 58 | If you enabled `HASURA_GRAPHQL_ADMIN_SECRET`, then you should provide the env var (or the option `--admin-secret `) to the CLI. 59 | For convenience of development, just populate env file to your shell (maybe with a shell plugin like [`dotenv`](https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/dotenv)), or use a tool like [`dotenv-cli`](https://github.com/entropitor/dotenv-cli). 60 | 61 | (Comments are my personal shorthand notation, which are not executable.) 62 | 63 | ```bash 64 | # hasura:_ 65 | hasura --project hasura 66 | # hasura:console => open web console to generate migration files 67 | hasura --project hasura console 68 | # hasura:migrate:_ 69 | hasura --project hasura migrate 70 | # hasura:status => show migration status 71 | hasura --project hasura migrate status 72 | # hasura:pull => pull migration from Hasura. Generally only used once (as a first migration) per a project. 73 | hasura --project hasura migrate create --from-server 74 | # hasura:push => push(applies) all applicable migration files to Hasura 75 | hasura --project hasura migrate apply 76 | # hasura:push:count => push a given number of migration(s) to Hasura 77 | hasura --project hasura migrate apply --up 78 | # hasura:push:version => push a specific version of migration to Hasura 79 | hasura --project hasura migrate apply --version 80 | # => mark an migration is applied, without actual execution. Check out "Squash" section under "Migrations". 81 | hasura --project hasura migrate apply --skip-execution --version 82 | # hasura:rollback:count => roll back a given number of migration(s) from Hasura 83 | hasura --project hasura migrate apply --down 84 | # hasura:rollback:version => roll back a specific version of migration from Hasura 85 | hasura --project hasura migrate apply --type down --version 86 | ``` 87 | 88 | ### To another stage (e.g. production) 89 | 90 | Use `--endpoint` option for remote Hasura. The option overrides the default endpoint specified in [hasura/config.yaml](hasura/config.yaml) (So you don't have to specify it when dealing with local Hasura). 91 | 92 | For practice, I recommend run migration commands against both local and remote Hasura. You can deploy an instance of Hasura by one(few)-click and free on Heroku. Refer to this very simple [Heroku deployment guide](https://docs.hasura.io/1.0/graphql/manual/getting-started/heroku-simple.html). 93 | 94 | ```sh 95 | # e.g. HASURA_ENDPOINT=http://another-graphql-instance.herokuapp.com 96 | hasura --project hasura --endpoint $HASURA_ENDPOINT migrate create --from-server 97 | hasura --project hasura --endpoint $HASURA_ENDPOINT migrate apply 98 | hasura --project hasura --endpoint $HASURA_ENDPOINT migrate apply --up 99 | hasura --project hasura --endpoint $HASURA_ENDPOINT migrate apply --version 100 | hasura --project hasura --endpoint $HASURA_ENDPOINT migrate apply --down 101 | hasura --project hasura --endpoint $HASURA_ENDPOINT migrate apply --type down --version 102 | hasura --project hasura --endpoint $HASURA_ENDPOINT migrate status 103 | ``` 104 | 105 | ### Examples 106 | 107 | ```bash 108 | hasura:pull 'init' 109 | hasura:push:count 2 110 | hasura:push:version 1550925483858 111 | hasura:rollback:count 2 112 | hasura:rollback:version 1550925483858 113 | ``` 114 | 115 | ## [Migrations](https://docs.hasura.io/1.0/graphql/manual/migrations/manage-migrations.html) 116 | 117 | You don't need to execute `hasura init` by yourself, as migrations directory([/hasura/migrations](/hasura/migrations)) and the config file([/hasura/config.yaml](/hasura/config.yaml)) are already here. 118 | 119 | This projects assumes `migrations`(,which includes `metadata`) handled by Hasura. But you can choose another migration tool over Hasura. If so, you only need Hasura to handle `metadata`. For that case, refer to [docs](https://docs.hasura.io/1.0/graphql/manual/migrations/manage-metadata.html) for more detail. 120 | 121 | ### [cli-migrations](https://docs.hasura.io/1.0/graphql/manual/migrations/auto-apply-migrations.html) 122 | 123 | Under [/hasura/migrations](/hasura/migrations), there is `1573631107183_init`. It's a sample migration which creates `User` table and make Hasura [`track`](https://docs.hasura.io/1.0/graphql/manual/schema/using-existing-database.html#step-1-track-tables-views) it. By the given configuration, not plain `hasura/graphql-engine:`, but `hasura/graphql-engine:.cli-migrations` is used as docker image. The image automatically applies migrations when Hasura starts. Remove `1573631107183_init` for your migrations. 124 | 125 | ### Squash 126 | 127 | You can "squash" multiple migrations into one by taking [several steps](https://blog.hasura.io/resetting-hasura-migrations/). 128 | But there has been issues(e.g. [#2724](https://github.com/hasura/graphql-engine/issues/2724)) about inconvenience. 129 | To make it simple, community members developed a tool like [hasura-squasher](https://github.com/domasx2/hasura-squasher) and [manual workflow](https://github.com/hasura/graphql-engine/issues/2724#issuecomment-524547524). 130 | Finally, a new command `hasura migrate squash` is introduced with a new migration structure on `v1.0.0-beta.9`. 131 | As of writing(`v1.1.0`), the command is still in preview(not stable). For more detail, check out the [changelog](https://github.com/hasura/graphql-engine/releases/tag/v1.0.0-beta.9). 132 | 133 | ## Networking 134 | 135 | ### Sync Hasura port 136 | 137 | By the given configuration, `8080` port is given to Hasura. To change it, you should sync the port number specified in [hasura/config.yaml](hasura/config.yaml) and `HASURA_ENDPOINT_PORT` in your env file. 138 | 139 | ### Host networking 140 | 141 | By the given configuration, Hasura and Postgres communicates each other by docker's [host mode networking](https://docs.docker.com/network/host). 142 | No matter how you configure `DB_ENDPOINT_PORT` from env file, Postgres is exposed to Hasura by `5432` port, the default value of Postgres image. 143 | That's why `5432` is hard-coded at `HASURA_GRAPHQL_DATABASE_URL` in [docker-compose.yml](docker-compose.yml). For the same reason, service name `postgres` is used rather than `localhost` on `HASURA_GRAPHQL_DATABASE_URL`. 144 | 145 | ```yml 146 | environment: 147 | HASURA_GRAPHQL_DATABASE_URL: "postgres://${POSTGRES_USERNAME}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DATABASE}" 148 | ``` 149 | 150 | ### Postgres exposure 151 | 152 | By the given configuration, Postgres is exposed to the host(your OS) by the port of `DB_ENDPOINT_PORT` from env file. This is useful on development as you can directly access to Postgres without Hasura. However, if you want to use [docker-compose.yml](docker-compose.yml) on production (e.g. `docker swarm` for `docker-compose`), you might reconsider how you should configure it. 153 | 154 | ## Seed Data 155 | 156 | Automatically seeding sample data can be very useful on local development workflow. Currently, Hasura doesn't support the "seed" feature (or some more generic feature that could be easily used). As a workaround, you can manually put a sql file with `INSERT` statements into migrations directory. To ensure seeding is executed AFTER Postgres schema is fully migrated, you can manually name it with very high timestamp value. However, as generally seed is only for development environment, _"seeding as migration"_ approach makes migration inconvenient, or even dangerous (e.g. You should be careful to migration for production). There's [an issue and discussion](https://github.com/hasura/graphql-engine/issues/2431#issuecomment-520459657) about this, and I personally expect this problem to be officially solved soon. 157 | 158 | As an another workaround, you can write a little program that calls GraphQL mutations or executes `INSERT` sql statements. 159 | 160 | For example, if you use node.js, 161 | 162 | ```bash 163 | docker-compose up -d && node seed.js 164 | ``` 165 | 166 | Of course, the program should watches (e.g. continuous health check polling) Hasura endpoint to make sure migrations are completed before inputting data. 167 | 168 | ## Custom Business Logic 169 | 170 | Custom logics are to be integrated with Hasura-generated GraphQL, as a single endpoint. They are also useful when Hasura's authorization system can't cover your complex requirements for specific tasks. There are various ways depending on your needs. 171 | 172 | - [Postgres View](https://docs.hasura.io/1.0/graphql/manual/queries/derived-data.html): You can expose computed/derived data as GraphQL query/subscription. 173 | - [Postgres Function](https://docs.hasura.io/1.0/graphql/manual/queries/custom-functions.html): You can expose custom GraphQL query/subscription. (Currently not for mutation) 174 | - [Postgres Trigger](https://hasura.io/blog/postgres-triggers-on-graphql-mutations-682bf48db023/): You can use Postgres Trigger to add synchronous logic like custom input validation, or arbitrary tasks like insert, update, or delete to other related data. 175 | - [Hasura's Remote Schema](https://docs.hasura.io/1.0/graphql/manual/remote-schemas/index.html): (This is DIFFERENT from graphql-tools'(of Apollo) `Remote Schema`.) You can develop your own GraphQL server (or external GraphQL API), and integrate it "into" Hasura (Similar to schema stiching). 176 | - [Remote Join](https://blog.hasura.io/remote-joins-a-graphql-api-to-join-database-and-other-data-sources/)(WIP. Comming Soon): You can precisely "join" (as like SQL join) external GraphQL for constructing the schema you exactly want. (Similar to Apollo Federation) 177 | - [Actions](https://deploy-preview-3042--hasura-docs.netlify.com/graphql/manual/actions/index.html)(WIP. Comming Soon): You can define a custom mutation in SDL by using types Hasura generates, and write "Action handler", which can be one of 'HTTP handler', 'Postgres functions' or 'Postgres PLV8'. It can be async as well (subscription/query for the result are auto generated by Hasura.). 178 | - [Schema Stitching](https://www.apollographql.com/docs/graphql-tools/schema-stitching/): (Though schema-stitching is deprecated, Apollo Federation, its alternative, can't be used in this use case.) In some cases, you need to place an additional layer in front of Hasura, in order to handle special requirement. You can override Hasura-generated queries, mutation, and subscription. Here's a good practical [guide](https://hasura.io/blog/the-ultimate-guide-to-schema-stitching-in-graphql-f30178ac0072/) with examples. If needed, you can also consider graphql-tools' [`schema transforms`](https://www.apollographql.com/docs/graphql-tools/schema-transforms/) feature to Remote Schema as well. 179 | - GraphQL microservices: Hasura can be an API gateway, with its "Action" and Remote Join, etc. However, you might want to use [Apollo Federation](https://www.apollographql.com/docs/apollo-server/federation/introduction/), and treat Hasura as a microservice. Then you can transform Hasura's schema to make it federation-spec-compliant. Though you can use graphql-tools' [`schema transforms`](https://www.apollographql.com/docs/graphql-tools/schema-transforms/), there's a helping package [graphql-transform-federation](https://www.npmjs.com/package/graphql-transform-federation). This is also useful to 3rd party GraphQL API service which you can't fix directly, or some situations where GraphQL directive is not allowed like some code-first approach frameworks (e.g. [nexus](https://github.com/prisma-labs/nexus-prisma), [type-graphql](https://github.com/MichalLytek/type-graphql)). Currently, Apollo Federation does not support subscription, though it's on the official roadmap. If you need it, you can place one more gateway that uses schema stitching. If you have a question, leave an issue. 180 | 181 | ![GraphQL MSA diagram](./asset/graphql-msa.svg) 182 | 183 | ## Event Trigger 184 | 185 | When an event(e.g. `INSERT`, `UPDATE`, `DELETE`) is occurred on a Postgres table, Hasura can invoke webhooks. Refer to [docs](https://docs.hasura.io/1.0/graphql/manual/event-triggers/index.html) for more detail. 186 | 187 | ## 3Factor App 188 | 189 | It's somewhat radical event-driven, async, reactive architecture pattern (Redux-like backend?!). The concept itself is independant from Hasura, however Hasura can be used for the architecture implementation. Refer to [3factor.app](https://3factor.app/) or an [example](https://github.com/hasura/3factor-example/). 190 | 191 | ## License 192 | 193 | [MIT License](./LICENSE). Copyright © 2019, Gil B. Chan <[bnbcmindnpass@gmail.com](mailto:bnbcmindnpass@gmail.com)> ([@jjangga0214](https://github.com/jjangga0214)) 194 | -------------------------------------------------------------------------------- /asset/graphql-msa.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /asset/hasura-starter.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.7" 2 | 3 | services: 4 | postgres: 5 | image: postgres:12-alpine 6 | restart: always 7 | environment: 8 | POSTGRES_DB: ${POSTGRES_DATABASE} 9 | POSTGRES_USER: ${POSTGRES_USERNAME} 10 | POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} 11 | PGTZ: ${POSTGRES_TIMEZONE} 12 | ports: 13 | - target: 5432 14 | published: ${POSTGRES_ENDPOINT_PORT} 15 | protocol: tcp 16 | mode: host 17 | volumes: 18 | ## To executes ['initialization scripts'](https://hub.docker.com/_/postgres), 19 | ## uncomment the next line, create ./data directory, and add .sh or .sql files under ./data 20 | # - ./data:/docker-entrypoint-initdb.d 21 | - postgres_data:/var/lib/postgresql/data 22 | graphql-engine: 23 | image: hasura/graphql-engine:v1.1.0.cli-migrations 24 | ports: 25 | - target: 8080 26 | published: ${HASURA_ENDPOINT_PORT} 27 | protocol: tcp 28 | mode: host 29 | volumes: 30 | - ./hasura/migrations:/hasura-migrations 31 | depends_on: 32 | - "postgres" 33 | restart: always 34 | environment: 35 | HASURA_GRAPHQL_DATABASE_URL: "postgres://${POSTGRES_USERNAME}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DATABASE}" 36 | HASURA_GRAPHQL_ENABLE_CONSOLE: ${HASURA_GRAPHQL_ENABLE_CONSOLE} 37 | HASURA_GRAPHQL_ENABLED_LOG_TYPES: ${HASURA_GRAPHQL_ENABLED_LOG_TYPES} 38 | ## uncomment next line to set an admin secret 39 | # HASURA_GRAPHQL_ADMIN_SECRET: '${HASURA_GRAPHQL_ADMIN_SECRET}' 40 | volumes: 41 | postgres_data: 42 | -------------------------------------------------------------------------------- /hasura/config.yaml: -------------------------------------------------------------------------------- 1 | endpoint: http://localhost:8080 2 | -------------------------------------------------------------------------------- /hasura/migrations/1573631107183_init/up.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION public.set_current_timestamp_updated_at() RETURNS trigger 2 | LANGUAGE plpgsql 3 | AS $$ 4 | DECLARE 5 | _new record; 6 | BEGIN 7 | _new := NEW; 8 | _new."updated_at" = NOW(); 9 | RETURN _new; 10 | END; 11 | $$; 12 | CREATE TABLE public."user" ( 13 | id uuid DEFAULT public.gen_random_uuid() NOT NULL, 14 | created_at timestamp with time zone DEFAULT now() NOT NULL, 15 | updated_at timestamp with time zone DEFAULT now() NOT NULL, 16 | password text NOT NULL, 17 | username text NOT NULL 18 | ); 19 | ALTER TABLE ONLY public."user" 20 | ADD CONSTRAINT user_pkey PRIMARY KEY (id); 21 | ALTER TABLE ONLY public."user" 22 | ADD CONSTRAINT user_username_key UNIQUE (username); 23 | CREATE TRIGGER set_public_user_updated_at BEFORE UPDATE ON public."user" FOR EACH ROW EXECUTE PROCEDURE public.set_current_timestamp_updated_at(); 24 | COMMENT ON TRIGGER set_public_user_updated_at ON public."user" IS 'trigger to set value of column "updated_at" to current timestamp on row update'; 25 | -------------------------------------------------------------------------------- /hasura/migrations/1573631107183_init/up.yaml: -------------------------------------------------------------------------------- 1 | - args: 2 | allowlist: [] 3 | functions: [] 4 | query_collections: [] 5 | remote_schemas: [] 6 | tables: 7 | - array_relationships: [] 8 | configuration: 9 | custom_column_names: {} 10 | custom_root_fields: 11 | delete: null 12 | insert: null 13 | select: null 14 | select_aggregate: null 15 | select_by_pk: null 16 | update: null 17 | delete_permissions: [] 18 | event_triggers: [] 19 | insert_permissions: [] 20 | is_enum: false 21 | object_relationships: [] 22 | select_permissions: [] 23 | table: user 24 | update_permissions: [] 25 | type: replace_metadata 26 | --------------------------------------------------------------------------------