When should you consider GraphQL for your application?

To understand this, let's take an example :


We have two entities here Publisher and Game

Publisher has attributes name and established year
Game has name, publisher and genre

One publisher can have multiple games and one game has it's own publisher, games also have relation among based on similar genre.

So if we try to solve this problem via rest api we need to have number of endpoints like :

domain.com/games
domain.com/game/:id
domain.com/publishers
domain.com/publisher/:id
domain.com/game/:id/publisher
domain.com/publisher/:id/games

etc etc... so you can see to get different set of data we need to expose different endpoints , if there is solution like from request we will send the data mapping and accordingly we will get our choice of data. There will be a single endpoint to handle all such request. Well then the solution is GraphQL.

Endpoint : http://localhost:3001/graphql

Query we will pass as :

{
    game:{
             name
             genre
             publisher:{
                name
             }
    }
}

Which will return game details with it's name, genre and publisher name

Comments

Popular posts from this blog

Best coding practice and structure for redux saga with react hooks | Separate watcher and worker saga | React clean code

How to use redux with React Hooks - Creating TodoList

Setting up Redux Devtools for React applications