UP | HOME

Date: [2022-11-12 Sat]

GraphQL

Table of Contents

From What Is GraphQL? REST vs. GraphQL [ByteByteGo]​ : GraphQL is a query language for API developed by Meta.

1. Advantages

  • It provides a schema of the data in the API and gives clients the power to ask for exactly what they need.
  • It could aggregate multiple resource requests into a single query. (No N+1 queries problem)
  • It also supports mutations, and subscriptions.

2. Similarity And Differences with REST

  • REST and GraphQL both use HTTP.
  • Both make a request via a URL, and both can return a JSON response in the same shape.
  • GraphQL doesn’t use URLs to specify the resources that are available in the API. Instead, it uses a GraphQL schema. We can send a complex query that fetches additional data according to relationships defined in the schema. Doing the same in REST is more complicated. We would have to do that client side with multiple requests. This is a common problem resulting in N+1 queries.

3. Disadvantags

  • The beauty of REST is that we don’t need special libraries to consume someone else’s API. In contrast, GraphQL requires heavier tooling support, both on the client and server sides.
  • This requires a sizable upfront investment.
  • Another criticism of GraphQL is that it is more difficult to cache. REST uses HTTP GET for fetching resources, and HTTP GET has a well-defined caching behavior that is leveraged by browsers, CDNs, proxies, and web servers. GraphQL has a single point of entry and uses HTTP POST by default.
  • The final concern we have with GraphQL is that while GraphQL allows clients to query for just the data they need, this also poses a great danger. Power to client may pose security risk. Unwanted requests/attacks may cripple the system. Yes, there are ways to mitigate this risk,but it adds even more complexity to a GraphQL implementation.

You can send your feedback, queries here