Skip to main content

Posts

Showing posts from October 23, 2022

Let's solve (N+1) problem in GraphQL

Greetings! This is the fourth article of the GraphQL series and the second part of the Query where I am going to solve (N+1) problem in GraphQL. However, I will add complete codes as well to make it usable on its own. The complete code can be found here . Code with (N+1) problem unresolved is here . (N+1) in brief When we fetch relationships like we did in our movie server where we queried movies, there will be extra N queries to fetch the relationship (director). With the first query to find movies, there are (N+1) queries altogether. Had you run our example by adding a console.log you can see the problem clearly. export function findDirector(id) { console.log(`find director for {id}`); return directors.find(director => director.id === id); } This problem does not depend on the database we used. Whether it is a fake store like we use, MySql, or MongoDB, the problem is the same. Solving N+1 Facebook provides a solution for this using the library data-loa

Let's build a GraphQL server with Node.js and Express (2)

Greetings! In this third article of the GraphQL series, I would like to discuss the steps needed to create a GraphQL server using Node.js with a somewhat more advanced query than the previous one. The complete code can be found here . What do we build? Movie API where we can query movies and directors' information. Initializing the project npm init -y npm install express graphql express-graphql --save npm install @babel/core @babel/node @babel/preset-env --save-dev npm install nodemon --save-dev Enable ES6 modules in package.json and add a start command for nodemon. { "name": "nodejs-graphql-query", "version": "1.0.0", "description": "", "main": "index.js", "type": "module", "scripts": { "start": "nodemon --exec babel-node src/index", "test": "echo \"Error: no test specified\" && exit 1"

Let's build a GraphQL starter server with Node.js and Express

Greetings! This is the second article of the GraphQL series, I would like to discuss the steps needed to create a GraphQL server using Node.js. Have a look at the first article if haven't read it yet. The complete code can be found here .  What we build As the idea is to create a basic GraphQL server, we will expose the "Hello World" query. Initializing the project npm init -y npm install express graphql express-graphql --save npm install @babel/core @babel/node @babel/preset-env --save-dev npm install nodemon --save-dev Enable ES6 modules in package.json and add a start command for nodemon. { "name": "nodejs-graphql-starter", "version": "1.0.0", "description": "", "main": "index.js", "type": "module", "scripts": { "start": "nodemon --exec babel-node index", "test": "echo \"Error: no

Introduction to GraphQL

Greetings! GraphQL is a query language for APIs to query data from the server. We are querying data from the server like we do with the database. Cool isn't it? Instead of getting what the server provides, we query what we want and it gives exactly what is asked! nothing more, nothing less. "A query language for your API" Advantages Easy to organize Allows API to evolve without breaking existing queries A single call to fetch data Query exactly what is wanted Better performance due to reduced network calls, and fewer data Common GraphQL terms Schema - GraphQL schema describes all the possible data that clients can query. It is made up of object types that define which kind of object you can request and what fields it has. Resolver - Each query is attached to a function called resolver that is used to produce data. Query - GraphQL query is used to fetch values from the