# A First Look at PostGraphile with Railway

> PostGraphile builds a GraphQL API from a PostgreSQL schema that automatically detects information such as tables, columns, indexes, and relationships

- **Collection:** Blog post
- **Published:** 2021-07-17
- **Author:** Anthony Campolo
- **Canonical URL:** https://ajcwebdev.com/first-look-postgraphile-with-railway/
- **Markdown URL:** https://ajcwebdev.com/first-look-postgraphile-with-railway/index.md
- **JSON URL:** https://ajcwebdev.com/first-look-postgraphile-with-railway/index.json

---

> ___All of this project's code can be found in the [First Look monorepo](https://github.com/ajcwebdev/a-first-look/tree/main/backend/postgraphile-railway/) 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](https://www.postgresql.org/docs/current/user-manag.html) and [row-level security policies](https://www.postgresql.org/docs/current/ddl-rowsecurity.html) with Graphile Engine's [GraphQL look-ahead](https://build.graphile.org/graphile-build/4/look-ahead/) and [plugin expansion](https://build.graphile.org/graphile-build/4/plugins/) 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](https://railway.com/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](https://docs.railway.com/cli).

### Check Railway CLI version

```bash wrap=false
railway version
```

```
railway version 0.2.40
```

### Login with railway login

If you do not have a Railway account you will be prompted to create one.

```bash wrap=false
railway login
```

### Initialize project with railway init

Run the following command, select “Empty Project,” and give your project a name.

```bash wrap=false
railway init
```

### Provision PostgreSQL with railway add

Run the following command and select PostgreSQL to add a plugin to your Railway project.

```bash wrap=false
railway add
```

### Connect to database

```bash wrap=false
railway connect postgresql
```

```
psql (13.3, server 13.2)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

railway=# 
```

### Seed database

Run the following SQL commands to create a test table with seed data.

```sql
CREATE TABLE Post (title text, body text);
INSERT INTO Post VALUES ('This is a blog post', 'Wooooooo');
INSERT INTO Post VALUES ('Another blog post', 'Even better than the other!');
```

![01 - railway-seed-data](https://ajc.pics/2021/07/17/01-railway-seed-data.webp)

### List tables in database

```bash wrap=false
\d
```

```
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | post | table | postgres
(1 row)
```

### Describe table

```bash wrap=false
\d post
```

```
              Table "public.post"
 Column | Type | Collation | Nullable | Default 
--------+------+-----------+----------+---------
 title  | text |           |          | 
 body   | text |           |          | 
```

### Quit psql

```bash wrap=false
\q
```

### Copy database connection string to clipboard

```bash wrap=false
echo `railway variables get DATABASE_URL` | pbcopy
```

## Introspect Database with PostGraphile

It is easy to install PostGraphile with [npm](https://docs.npmjs.com/getting-started/installing-node/), although the PostGraphile documentation does not recommend installing PostGraphile globally if you want to use plugins.

```bash wrap=false
npm install -g postgraphile
```

If you do not globally install you will need to add `npx` the beginning of all `postgraphile` commands in this tutorial.

### Introspect Railway Database

```bash wrap=false
railway run postgraphile --watch --enhance-graphiql --dynamic-json --port 5001
```

Open `localhost:5001/graphiql` and send the following query.

![02 - postgraphile-graphiql](https://ajc.pics/2021/07/17/02-postgraphile-graphiql.webp)

### Test the endpoint

```bash wrap=false
curl \
  --request POST \
    --url "http://localhost:5001/graphql" \
    --header "Content-Type: application/json" \
    --data '{"query":"{ query { allPosts { totalCount nodes { body title } } } }"}'
```

```json wrap=false
{
  "data":{
    "query":{
      "allPosts":{
        "totalCount":2,
        "nodes":[
          {
            "body":"Wooooooo",
            "title":"This is a blog post"
          },
          {
            "body":"Even better than the other!",
            "title":"Another blog post"
          }
        ]
      }
    }
  }
}
```

### Connect to endpoint with ngrok

[ngrok](https://ngrok.com/) 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.

```bash wrap=false
./ngrok http 5001
```

```
Session Status                online
Account                       Anthony Campolo (Plan: Free)
Version                       2.3.40
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://363ef1ef5cf3.ngrok.io -> http://localhost:5001
Forwarding                    https://363ef1ef5cf3.ngrok.io -> http://localhost:5001

Connections                   ttl     opn     rt1     rt5     p50     p90
                              2       0       0.00    0.00    5.11    5.21
```

Send the same query with your API tool of choice.

![03 - all-posts-query](https://ajc.pics/2021/07/17/03-all-posts-query.webp)

## Discover Related Content

- [Deploying Railway Applications with StepZen](https://ajcwebdev.com/videos/greg-schier-deploying-railway-applications/) (Video)
- [Querying MongoDB with Prisma and Railway](https://ajcwebdev.com/query-mongodb-with-prisma-and-railway/)
- [A First Look at GraphQL Helix](https://ajcwebdev.com/first-look-gql-helix/)
- [Deploy a GraphQL Server with Docker and Fly](https://ajcwebdev.com/deploy-gql-docker-container-with-fly/)
- [PodRocket on GraphQL 101 with Anthony Campolo](https://ajcwebdev.com/podcasts/podrocket-graphql-101/) (Podcast)
