DEV Community

Ayrton
Ayrton

Posted on

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 !

Top comments (6)

Collapse
 
simerca profile image
Ayrton

Hi ! Don't forget to discuss with me because I love coffee, like you ☕️ !

Collapse
 
gevera profile image
Denis Donici • Edited

Would love to see a more in-depth tutorial with maybe authentication and roles or subscriptions

Collapse
 
simerca profile image
Ayrton

Good idea ! I can work on it

Thread Thread
 
gevera profile image
Denis Donici • Edited

Please do, because there are no proper tutorials online that will describe how to implement subscriptions. I have tried to implement subscriptions myself with no luck so far.

Collapse
 
sametweb profile image
Samet Mutevelli

I will take a look at this, it looks interesting. Thanks for sharing!

Collapse
 
simerca profile image
Ayrton

Yes very helpful !