1

I’m trying to understand what the advantages are of GraphQL. I’ve read about reducing the number of endpoints and the complexity of server responses, but it seems that the same results can be achieved with JS alone.

Here’s an example of a data object that could be sent as JSON to a node server with MongoDB. This would be an example of a game app where the client is retrieving user info:

let data = {
     db: "users", 
     params: {_id: "xxxxx"}, 
     fields:  ["username", "level"],
     games: 
           {
               db: "games", 
               params: {userID: "xxxxx"},
               fields: ["opponent”]
           }
}

In this example, db, params, and fields would be standard keys, and games would be like a special key for the specific purpose of retrieving the user’s games, however, the syntax of the games object would follow the same standard format as the overall data object.

Then on the server, the Mongo query would look something like this:

db.collection(data.db).find(params)

You’d then filter out the extraneous Mongo fields in some standardized way and respond to the client.

I’m a relative beginner with JS, but I think you could also chain promises based on whether certain special keys (e.g., “games” from above) are included in the data object.

This seems like it achieves the same benefits as GraphQL with less complexity. What other benefits does GraphQL have that a plain JS equivalent does not?

SuperCodeBrah
  • 2,874
  • 2
  • 19
  • 33
  • One added advantage would be strong type checking. It's not just about aggregating the end points, it solves the problem of over-fetching(when endpoint gives you data which you don't want) and under-fetching(when one endpoint req is enough to get all the data you want). GraphQL gives the client power of choosing what it wants, a endpoint may be used in different components of an app but only that component knows what data it exactly wants and it can just ask for that data. – Pavan Bahuguni Apr 05 '18 at 17:11

0 Answers0