GraphQL is a “Better Way”

 

After reading articles purporting the amazingness of GraphQL over RESTful, I decided to explore it with a “getting started” tutorial. It was a breeze to set up on the server side.

The beauty of GraphQL is that it allows the client to specify what data it requires whereas a RESTful API would deliver the entire contents of a table unless a custom endpoint were created and maintained.

It also allows for collation from multiple resources and delivers the results via a tidy JSON object.

My first reaction to hearing about it was that allowing for such specificity would involve learning a new query language. I was wrong. The format is elegant and intuitive. A request looks almost just like the JSON response without the values. Here’s an example of a request:

{
  user1: user(login: "tj") {
    name
    email
    about
    following
    followers
  },
  user2: user(login: "ekendra-nz") {
    name
    email
    about
    following
    followers
  }
}
GraphQL Request

And here is the response. See how the query and response work like a Q and A? How smooth is that?

{
  "data": {
    "user1": {
      "name": "TJ Holowaychuk",
      "email": null,
      "about": "Founder of Apex\r\nhttps://apex.sh\r\n@tjholowaychuk on Twitter & Medium.",
      "following": 48,
      "followers": 30215
    },
    "user2": {
      "name": "ekendra-nz",
      "email": null,
      "about": "PHP/JS Webdev",
      "following": 0,
      "followers": 0
    }
  }
}
GraphQL Response

 

Leave a Reply

Your email address will not be published. Required fields are marked *