Make a fast Auto GraphQL Server with NodeJS and Postgres in 5 minutes !

simerca

Ayrton

Posted on October 3, 2020

Make a fast Auto GraphQL Server with NodeJS and Postgres in 5 minutes !

Hello World !
Too many loves in my last post
https://dev.to/simerca/why-you-don-t-use-ansible-4olk

Today i show you how to mount a fast Auto Schema GraphQL server with NodeJS and Postgres Database in 5 minutes.

The the first thing is mount Postgres database with Docker !

docker run --name mydb-postgres -e POSTGRES_PASSWORD=12345 -p 5432:5432 -d postgres  
Enter fullscreen mode Exit fullscreen mode

(default user is : postgres , default db is : postgres)

You can try to connect with DBeaver it's a good Postgres UI tool
https://dbeaver.io/

NOW!

create a folder for your NodeJS project

mkdir awesome-graphql-server
cd awesome-graphql-server
Enter fullscreen mode Exit fullscreen mode

Init npm packages

npm init
Enter fullscreen mode Exit fullscreen mode

Install Express and Postgraphile

Postgraphile is a very good tool to auto schema your Graphql based on your Postgres structure (relations include , very awesome)

npm install express
npm install postgraphile
Enter fullscreen mode Exit fullscreen mode

so this is the simple code to have to insert in your index.js

touch index.js
nano index.js
Enter fullscreen mode Exit fullscreen mode

insert this inside

var express = require('express');

const {
    postgraphile
} = require("postgraphile");

var app = express();
app.use(
    postgraphile(
        process.env.DATABASE_URL || "postgres://postgres:12345@0.0.0.0:5432/postgres",
        "public", {
            watchPg: true,
            graphiql: true,
            enhanceGraphiql: true,
        }
    )
);
app.listen(4000, () => console.log('go to for playground graphiql http://localhost:4000/graphiql'))
Enter fullscreen mode Exit fullscreen mode

after launch

node index.js
Enter fullscreen mode Exit fullscreen mode

And go to http://localhost:4000/graphiql
Welcome to your Graphql Auto schema playground !

The endpoint for Graphql request is
http://localhost:4000/graphql

Thanks for your feedbacks !

💖 💪 🙅 🚩
simerca
Ayrton

Posted on October 3, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related