Setting up GraphQl with nodejs, sequelize, and postgres

Dikshant Rajput
4 min readSep 11, 2022

--

GraphQl is a powerful query language for writing APIs, where front end can ask for the data they need rather than backend telling them the queries, frontend decide itself what they need and just call for it.

Maintaining documentation and all in REST is quite difficult as compared to GraphQl and also the point of flexibility plays a major role why one should choose GraphQl over REST. The front end can ask for more data as and when required while hitting GraphQl but the same is not the case for REST, whether the backend has to create completely new APIs or they have to edit the same API and there is always an overhead attached to both.

Using sequelize is not necessary, you can use any ORM or can completely neglect using ORM and just write your own queries. We will be using Postgres as a DB but again we can also use MySql, it’s completely up to you. So let’s get started.

The list of libraries we will be using throughout the tutorial are:

express
apollo-server-express
cors
sequelize
sequelize-cli
dotenv
pg

Let’s get started by creating a GraphQl server using express. Create a server.js in the root directory of your project and write this set of code in it.

What’s in the file?

  • Created an express app.
  • Setup cors
  • Created new apolloserver passing on the typeDefs, resolvers, and context (will be explained in the rest part of the blog).
  • Started the apolloserver.
  • started the express application.

typeDefs: These are the schemas of GraphQl.

resolvers: These are the logic to fetch those schemas data.

context: This is the common thing that we want to share with every resolver.

Crete directory named graphQl and create three more directories inside it naming schemas, resolvers, and context.

Inside the schemas directory create an index.js and write these set of code inside it:

Define the User and UserProfile schema we want to have fields in DB and expose to the front end.

Inside the resolvers directory create an index.js and write these set of code inside it:

Ignore the User model file, we will be discussing it later.

In the above file, we have written the resolvers for all the schema that we mentioned earlier while defining the schema.

Inside the context directory create an index.js file and write these set of code inside it:

Fetch the user via any token or any header that you can receive from the req parameter passed to context and return the user. This user will be available to every resolver method as a third parameter.

After all these, we have to create models, migrations, and seeders.

For these, we will be using sequelize-cli.

After installing sequelize-cli, configure the DB username, DB name, passwords, host, and all other required settings.

Run these commands:

npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string

User models and migrations will be created.

npx sequelize-cli model:generate --name User --attributes bio:string,phone:number

UserProfile model and migrations will be created.

Write your desired code in the migrations and models.

npx sequelize-cli db:migrate

This will create tables with the field mentioned in the migrations file.

Run the server file with node server.js and go to http://localhost:3000/graphql

Run this query and check the result.

If you like the blog, hit that clap icon and if you have any doubt or wants the complete code with tests written, post a comment below.

--

--

Dikshant Rajput
Dikshant Rajput

No responses yet