REST vs. GraphQL APIs – how to choose?

REST APIs has been around for a while now, while GraphQL is relatively new to the game. While they both are used for data transfer – sending HTTP requests and receive HTTP responses, they both have their own unique characteristics. Let’s look at how data is fetched via REST and GraphQL.

Data Fetching with REST vs GraphQL

For a REST API, client would send HTTP GET requests to multiple endpoints to retrieve the data.

Image courtesy: www.howtographql.com

For GraphQL, however, the client sends a single query to the GraphQL server that includes the concrete data requirements. The server then responds with a JSON object where these requirements are fulfilled. In other words, GraphQL sits between the client and the backend services and aggregates multiple requests in to a single query.

Image courtesy: www.howtographql.com

Read more on GraphQL here.

Similarities and Differences

REST GraphQL
Operations supported: Create, Read, Update, DeleteOperations supported: Query, Mutation, Subscription
Organized as endpoints.Organized as Schema and type system.
Architecture is Server driven.Architecture is Client driven.
Development is relatively slow.Rapid development speed.
Full support for web caching.web caching through libraries.
As there are separate endpoints for different queries, relatively less complex.Queries can become complex because of client requests.
Can upload files.Can’t upload files.
Client requests can be sent from common tools like curl or a browser.Requires special tooling at the client and server side. Hence not suited for simple projects.

Should you use GraphQL?

As you know, software engineering is all about tradeoffs. Whether REST or GraphQL or SOAP is right for your project depends on your specific use case, but do consider GraphQL next time.

References

  1. https://www.howtographql.com
  2. https://graphql.org
  3. https://www.apollographql.com

Posts navigation

  • 1
  • 2