DEV Community

loading...

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

Ayrton
J'aime faire des choses inutiles ๐Ÿ˜…
ใƒป1 min read

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  

(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

Init npm packages

npm init

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

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

touch index.js
nano index.js

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'))

after launch

node index.js

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 !

Discussion (6)

Collapse
simerca profile image
Ayrton Author

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 Author

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 Author

Yes very helpful !