Overview for GraphQL
8
0

What is it GraphQL?

“GraphQL is a query language for your API, and a server-side runtime for executing queries using a type system you define for your data. GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data”.

GraphQL was developed by Facebook in 2012 because the team needed “a data-fetching API powerful enough to describe all of Facebook, yet simple enough to be easy to learn and use by our product developers” when building the mobile applications.

Actually, the real secret is that GraphQL ensures that the developer and application only load the relevant and absolute necessary data, even if it’s from multiple sources.

 

When to use GraphQL?

  • Best to use where bandwidth usage matters.(apps for mobile phone, smartwatches, IoT devices etc).
  • Applications where the nested data need to be fetched in a single call. (Social networking platform where posts need to be fetched along with nested comments and commenters details).
  • where application retrieves data from multiple, different storage APIs. (a dashboard that fetches data from multiple sources such as logging services, backends for consumption stats, third-party analytics tools to capture end-user interactions).

What are the pros and cons of GraphQL?

Pros

  1. A GraphQL schema sets a single source of truth in a GraphQL application. It offers an organization a way to federate its entire API.
  2. GraphQL calls are handled in a single round trip. Clients get what they request with no over fetching.
  3. Strongly defined data types reduce miscommunication between the client and the server.
  4. GraphQL is introspective. A client can request a list of data types available. This is ideal for auto-generating documentation.
  5. GraphQL allows an application API to evolve without breaking existing queries.
  6. Many open source GraphQL extensions are available to offer features not available with REST APIs.
  7. GraphQL does not dictate a specific application architecture. It can be introduced on top of an existing REST API and can work with existing API management tools.
  8. Autogenerating API documentation.
  9. Design for make API faster, flexible and developer-friendly.

Cons

  1. GraphQL presents a learning curve for developers familiar with REST APIs.
  2. GraphQL shifts much of the work of a data query to the server side, which adds complexity for server developers.
  3. Depending on how it is implemented, GraphQL might require different API management strategies than REST APIs, particularly when considering rate limits and pricing.
  4. Caching is more complex than with REST.
  5. API maintainers have the additional task of writing maintainable GraphQL schema.

 

More awesome features of GraphQL

  • Make multiple queries in one request – you can get multiple result set using single request.
  • Pagination queries – you can select which set of results to retrieve from a connection by using cursor-based pagination.
  • Everything is typed and part of a schema – can use the schema to build queries that return only the data that you need. This solves the problem of over-fetching data. It also means that we know which fields each app is using, making it easier for us to evolve our APIs over time.

 

 

 

 

No under fetching and over fetching

What is this? This is very common problem when it comes to the REST. REST APIs are always returning fix data set according to how it is developed in back-end. What does this mean? Imagine for an example a UI that needs to display list followers only with their names. In REST API, this page load hits usually ‘/followers’ endpoint and receive follower’s detail in JSON array. This result set might include more info about followers like mails, ages, addresses, birthdays that are useless for that particular page. In simply this is over-fetching. Instead of avoid this in REST, it must be written separate endpoint for this particular scenario. In that case, it will be increased the number of endpoints that returns same results with few additional fields. Look at the given figures, both can use for above scenario but second one cases over-fetching.

 

 

 

 

 

 

Then, what is under fetching? Under fetching means completely opposite of the over fetching. In other words, under-fetching means that REST API does not provide enough of required fields. In that case, client will have to make another request for completing data fetching.

Schema and type system

GraphQL has very strong schema and type system. Once schema and type system implemented and release from the back-end side, front end developers no need to wait for endpoints. FE developers can access what they exactly need through queries. Schema and type system allows to access those data. In GraphQL, Shema serves as contract between client and server to define how a client can access data.

Followings are the list of features that can be used with schema and type system.

  • Type System
  • Type Language
  • Object Types and Fields
  • Arguments
  • The Query and Mutation Types
  • Scalar Types
  • Enumeration Types
  • Lists and Non-Null
  • Interfaces
  • Union Types
  • Input Types

Here, I am not going to talk much deeper as this is an overview for GraphQL. We will talk these in deeper in another blog.

Who is currently using GraphQL?

  • Facebook
  • Coursera
  • GitHub
  • Pluralsight
  • PayPal
  • Shopify
  • Twitter
  • And many other worldwide companies

 

Reference

SHARE THIS POST

Leave comment

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