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 !
Top comments (6)
Hi ! Don't forget to discuss with me because I love coffee, like you ☕️ !
Would love to see a more in-depth tutorial with maybe authentication and roles or subscriptions
Good idea ! I can work on it
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.
I will take a look at this, it looks interesting. Thanks for sharing!
Yes very helpful !