A First Look at PostGraphile with Railway
Published:
PostGraphile builds a GraphQL API from a PostgreSQL schema that automatically detects information such as tables, columns, indexes, and relationships.
Outline
- Introduction
- Provision a PostgreSQL database with Railway
- Introspect Database with PostGraphile
- Discover Related Articles
All of this project’s code can be found in the First Look monorepo on my GitHub.
Introduction
PostGraphile builds a GraphQL API from a PostgreSQL schema that automatically detects tables, columns, indexes, relationships, views, types, functions, and comments. It combines PostgreSQL’s role-based grant system and row-level security policies with Graphile Engine’s GraphQL look-ahead and plugin expansion technologies.
Provision a PostgreSQL database with Railway
There are two ways to setup a PostgreSQL database with Railway, through the dashboard or through the CLI.
Railway Dashboard
Click dev.new and choose “Provision PostgreSQL” After the database is setup click “PostgreSQL” on the left and then choose “Connect”. Copy and paste the PostgreSQL client command.
Railway CLI
First you need to install the Railway CLI.
Check Railway CLI version
Login with railway login
If you do not have a Railway account you will be prompted to create one.
Initialize project with railway init
Run the following command, select “Empty Project,” and give your project a name.
Provision PostgreSQL with railway add
Run the following command and select PostgreSQL to add a plugin to your Railway project.
Connect to database
Seed database
Run the following SQL commands to create a test table with seed data.
List tables in database
Describe table
Quit psql
Copy database connection string to clipboard
Introspect Database with PostGraphile
It is easy to install PostGraphile with npm, although the PostGraphile documentation does not recommend installing PostGraphile globally if you want to use plugins.
If you do not globally install you will need to add npx
the beginning of all postgraphile
commands in this tutorial.
Introspect Railway Database
Open localhost:5001/graphiql
and send the following query.
Test the endpoint
Connect to endpoint with ngrok
ngrok provides an instant, secure URL to your localhost server through any NAT or firewall where you can introspect all HTTP traffic running over your tunnels.
Send the same query with your API tool of choice.