hasura를 이용하여 graphQL 순식간에 뚝딱하고 만드는방법
Universal data access layer for next-gen apps and AI
The fastest and simplest way to build, run, govern, and evolve a high-quality API layer on all your data.
hasura.io
hasura는 내가 가지고 있는 데이터에 대한 API 레이어를 만들어주는 역할을 합니다
좀더 쉽게 설명하자면 여러분이 postgres 데이터베이스와 그 안에 데이터를 가지고 있다고 할때
데이터베이스에 담긴 데이터를 조회, 추가, 삭제하는 graphQL API를 자동으로 생성합니다
여러분이 특정 데이터베이스에 기반하여 단순한 CRUD가 필요하다면
코드단의 작업 필요 없이
hasura를 설치하고 데이터베이스와 연결하여 손쉽게 코드한줄 작성하지 않고 graphQL API 레이어를 생성할 수 있습니다
여러분이 가지고 있는 테이블에 기반하여 직관적인 graphQL API를 이용할수 있습니다
harua를 직접 설치형으로 사용하는 2.x 버전은 도커를 이용하여 쉽게 설정이 가능합니다
https://hasura.io/docs/2.0/getting-started/docker-simple/
Quickstart with Docker | Hasura GraphQL Docs
Get started with Hasura using Docker
hasura.io
graphql-engine:
image: hasura/graphql-engine:v2.20.0
ports:
- "8080:8080"
restart: always
environment:
## postgres database to store Hasura metadata
HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
## this env var can be used to add the above postgres database to Hasura as a data source. this can be removed/updated based on your needs
PG_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
## enable the console served by server
HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
## enable debugging mode. It is recommended to disable this in production
HASURA_GRAPHQL_DEV_MODE: "true"
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
## uncomment next line to run console offline (i.e load console assets from server instead of CDN)
# HASURA_GRAPHQL_CONSOLE_ASSETS_DIR: /srv/console-assets
## uncomment next line to set an admin secret
# HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
depends_on:
- postgres
그리고 지금은 data-connector-agent를 사용할수 있는 버전의 docker compose 입니다
services:
postgres:
image: postgres:15
restart: always
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgrespassword
graphql-engine:
image: hasura/graphql-engine:v2.42.0
ports:
- "8080:8080"
restart: always
environment:
## postgres database to store Hasura metadata
HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
## this env var can be used to add the above postgres database to Hasura as a data source. this can be removed/updated based on your needs
PG_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres
## enable the console served by server
HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
## enable debugging mode. It is recommended to disable this in production
HASURA_GRAPHQL_DEV_MODE: "true"
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
## uncomment next line to run console offline (i.e load console assets from server instead of CDN)
# HASURA_GRAPHQL_CONSOLE_ASSETS_DIR: /srv/console-assets
## uncomment next line to set an admin secret
# HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
HASURA_GRAPHQL_METADATA_DEFAULTS: '{"backend_configs":{"dataconnector":{"athena":{"uri":"http://data-connector-agent:8081/api/v1/athena"},"mariadb":{"uri":"http://data-connector-agent:8081/api/v1/mariadb"},"mysql8":{"uri":"http://data-connector-agent:8081/api/v1/mysql"},"oracle":{"uri":"http://data-connector-agent:8081/api/v1/oracle"},"snowflake":{"uri":"http://data-connector-agent:8081/api/v1/snowflake"}}}}'
depends_on:
data-connector-agent:
condition: service_healthy
data-connector-agent:
image: hasura/graphql-data-connector:v2.42.0
restart: always
ports:
- 8081:8081
environment:
QUARKUS_LOG_LEVEL: ERROR # FATAL, ERROR, WARN, INFO, DEBUG, TRACE
## https://quarkus.io/guides/opentelemetry#configuration-reference
QUARKUS_OPENTELEMETRY_ENABLED: "false"
## QUARKUS_OPENTELEMETRY_TRACER_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8081/api/v1/athena/health"]
interval: 5s
timeout: 10s
retries: 5
start_period: 5s
volumes:
db_data:
hasura는 기본 지원으로 postgres, mssql, bigquery를 지원하고 있는데
기본 지원이 되지 않더라도 data-connector-agent를 통해 모든 종류의 데이터 소스에서도 데이터를 가져올수 있는 기능을 제공하고 있습니다
hasura에서는 내부에서 다양한 설정들을 통해 graphQL을 좀더 풍부하게 사용할수 있습니다
1:n, n:m 관계를 설정하고 노출시키고자 하는 테이블과 컬럼을 정의할 수 있습니다
이러한 정보들을 메타데이터라고 부르는데요
hasura에서 아쉬운 부분중의 하나는 메타데이터를 docker의 환경변수 형태로 주입할수 있는 방법이 없더라구요
다만 메타데이터의 export, import 기능을 제공하고 있어서,
팀단위에서 메타데이터를 공유할때는 export, import 기능을 사용하여야 합니다
https://hasura.io/docs/2.0/migrations-metadata-seeds/manage-metadata/
Manage Metadata | Hasura GraphQL Docs
Manage Hasura Server Metadata
hasura.io
상단의 네비게이션에서 settings 메뉴를 통해 import, export 기능을 확인할수 있습니다
hasura는 매우 손쉽게 graphQL API 레이어를 생성해줍니다
가상 쉽게 접근할수 있는 케이스로
내부의 백오피스에서 사용하는 API를 개발하는 용도로 개발자의 공수를 줄여줄 수 있습니다