GraphQL vs REST APIs

The only advantage I see of GraphQL over REST is the ability of specifying the fields returned in the response. It can save a lot of sequential calls which slowdown the frontend responsiveness.

With Ruby on Rails there is a way that can be easily used to create this advantage over a REST API. We can use the as_json serializer:

  • We can make a call with query parameter like this: /users?query=”include: { posts: { include: { comments: { only: :body } }, only: :title } }”
  • In the controller we could return: user.as_json(params[:query])

This is an idea to improve your REST API, but in Rails we can make a fully GraphQL API with graphql gem.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.