What is GraphQL? How to use GraphQL?

What is GraphQL?

GraphQL is a query language for the API and a server-side runtime for executing queries. It gives clients the requested data and no extra information.

Why we need GraphQL?

GraphQL is really flexible for the following cases:

  • GraphQL is an alternative to REST
  • GraphQL is designed to make APIs fast, flexible, and developer-friendly
  • GraphQL lets developers construct requests that pull data from multiple data sources in a single API call
  • GraphQL gives API maintainers the flexibility to add or deprecate fields without impacting existing queries

Advantages of GraphQL:

  • The GraphQL schema sets a single source of truth in a GraphQL application. It offers an organization a way to federate its entire API.
  • GraphQL calls are handled in a single round trip. Clients get what they request with no overfetching.
  • Strongly defined data types reduce miscommunication between the client and the server. 
  • GraphQL is introspective. A client can request a list of data types available. This is ideal for auto-generating documentation.
  • GraphQL allows an application API to evolve without breaking existing queries.
  • Many open source GraphQL extensions are available to offer features not available with REST APIs.
  • 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.

Disadvantages of GraphQL:

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