Warning: mysqli_connect(): (HY000/1040): Too many connections in /var/www/webnuz/inc/base_connector.php on line 2 Failed to connect to MySQL: Too many connections Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /var/www/webnuz/inc/func_sysvars.php on line 5 a first look at postgraphile with railway - by Dev To
Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 17, 2021 10:04 pm GMT

a first look at postgraphile with railway

PostGraphile builds a GraphQL API from a PostgreSQL schema in seconds that automatically detects tables, columns, indexes, relationships, views, types, functions, and comments.

By combining features such as PostgreSQL's role-based grant system and row-level security policies with Graphile Engine's GraphQL look-ahead and plugin expansion technologies, PostGraphile ensures your generated schema is secure, performant and extensible.

1. 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 create a Railway account and install the Railway CLI.

Check Railway CLI version

railway version
railway version 0.2.40

Login with railway login

railway login

Initialize project with railway init

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

railway init

Provision PostgreSQL with railway add

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

railway add

Connect to database with psql

Replace xxxx with your database password.

PGPASSWORD=xxxx psql --host=containers-us-west-2.railway.app --username=postgres --port=5675 --dbname=railway
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.

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

List tables in database

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

Describe table

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

Quit psql

\q

Copy database connection string to clipboard

echo `railway variables get DATABASE_URL` | pbcopy

2. 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.

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

Enter the following command to connect to your Railway database including:

  • Username - postgres
  • Password - xxxxxxxx
  • Host - containers-us-west-10.railway.app
  • Port - 7215
  • Database name - railway
postgraphile -c 'postgresql://postgres:xxxxxxxx@containers-us-west-10.railway.app:7215/railway' --watch --enhance-graphiql --dynamic-json --port 5001

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

02-postgraphile-graphiql

Send POST request to the endpoint with curl

curl \  --request POST \    --url "http://localhost:5001/graphql" \    --header "Content-Type: application/json" \    --data '{"query":"{ query { allPosts { totalCount nodes { body title } } } }"}'
{  "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 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.

./ngrok http 5001
Session Status                onlineAccount                       Anthony Campolo (Plan: Free)Version                       2.3.40Region                        United States (us)Web Interface                 http://127.0.0.1:4040Forwarding                    http://363ef1ef5cf3.ngrok.io -> http://localhost:5001Forwarding                    https://363ef1ef5cf3.ngrok.io -> http://localhost:5001Connections                   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


Original Link: https://dev.to/ajcwebdev/a-first-look-at-postgraphile-with-railway-1k9d

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To