What is GraphQL?

GraphQL is a middleware that allows the user to have 1 endpoint to handle most requests on your express server. The benefits of using this:

  • Avoids having to create many routes to handle everything.
  • Avoids over-fetching and under-fetching data
  • This works concurrently with API routes so the server can still be RESTful.

Install devDependencies

When this was written keywords import and export were not supported fully so this step is recommended.

npm install @babel/core @babel/node @babel/preset-env nodemon --save-dev

Setting Up The Express Server

First thing to do is install express and make a index.js file.

npm install express

This server will be using the Hello world example from the express documentation but with some small changes to incorporate import.

import express from 'express'
const app = express()
const port = 3000
app.get('/', (req, res) => {
  res.send('Hello World!')
})
app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

The simple server is up and running here and needs sequelize models.


Setting Up Models with Seqeulize

Create a postgreSQL database using the below command, this server’s database will be called medium.

createdb medium

Install sequelize and its drivers. Sequelize will convert the javascript code into SQL but pg and pg-hstore communicate with postgreSQL and make the actual changes.

npm install sequelize pg pg-hstore.

Create a new folder called _database _ and a _models _ folder inside that and a _models.js _ file in that. Here the actual models will be made. Import sequelize and create a new instance of Sequelize passing in a string with the name of the database “postgres://localhost:5432/medium”

#postgresql #express #graphql #import-export #sequelize

How to Setup a GraphQL, Express and postgreSQL Server
1.60 GEEK