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.
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.
Read more on GraphQL here.
Similarities and Differences
REST | GraphQL |
Operations supported: Create, Read, Update, Delete | Operations 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
- https://www.howtographql.com
- https://graphql.org
- https://www.apollographql.com
Good luck!