Skip to content

ncrmro/postgres-graphql

Repository files navigation

Postgres GraphQL

This is an example project using the following technologies to provide a GraphQL server.

The main benefit here is our GraphQL API types come directly from the Database, furthermore in our frontend applications we can automatically generate Typescript Types and Typed requests to interact with our API improving development agility and our application's integrity.

This README contains the following sections.

Getting Started

The only hard requirement is that you have docker and docker-compose installed, although naturally, you could install everything locally.

.env

First, let's copy the .env.example to .env. This file used by docker-compose will pass these environment variables into designated containers.

cp .env.example .env
sh ./init.sh
docker-compose up database migrations

Write some migrations in current.sql, once you're ready to commit.

Note I like to keep the minimum amount of logic in a specific commit to make your schema easier to read in the future, this also keeps your agility up in regards to developing new features.

docker-compose exec migrations yarn graphile-migrate commit -m "my commit message"

If you need to reset the database and rerun committed migrations.

docker-compose exec migrations yarn graphile-migrate reset --erase

GraphQL

Once we have our database up and running with a schema we can go ahead and start the GraphQL server.

docker-compose up database migrations gql

Or using up without and other parameters would start all available docker-compose services

docker-compose up database migrations gql

Note that because GraphQL specifies migrations as a dependency and migrations specify database as a dependency we could also start everything up with the following command, although this would only output logs from the gql service hiding other crucial logs.

docker-compose up database migrations gql

GraphiQL

Postgraphile comes with a nifty interface to aid in development called GraphiQL, this allows us to inspect our current GraphQL schema at the following URL.

http://localhost:5000/graphiql