top of page
Search
  • Writer's pictureShannon

APIs Aren't Just For Devs Pt. 9 - To REST API or Use GraphQL, That is the Question...

"To be or not to be" is one of the most famous lines from William Shakespeare's play "Hamlet." It's spoken by the title character, Prince Hamlet, in his famous soliloquy in Act 3, Scene 1.


The full line goes:

"To be, or not to be, that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune,

Or to take arms against a sea of troubles

And by opposing end them."


In this soliloquy, Hamlet is contemplating the nature of existence and the hardships of life. The phrase "To be or not to be" essentially poses the question of whether it's better to continue living and facing the challenges and sufferings that life brings ("to be"), or to end one's life and escape those challenges ("not to be"). The soliloquy delves into the philosophical and emotional complexities of life, death, and the human experience. It reflects Hamlet's internal struggle with the conflicts and dilemmas he faces throughout the play.


So, while different to today's topic at hand, the existential question doesn't quite fall on its face here! Is it better to continue using or building REST APIs or is it better to consider using GraphQL?


If you're just joining me on this post, I have 8 (yes - 8) previous blog posts that may make sense to digest. I spoke at a vBrown Bag in 2022 about how APIs aren't just for developers anymore. Gone are the days of only knowing infrastructure in this software defined world! As a point of reference, please familiarize yourself with the blog posts that help outline the bigger/broader picture for all:


First thing's first...what is GraphQL and why might you hear it referenced when folks talk about REST APIs? Well, GraphQL is a query language and runtime for APIs that enables efficient data retrieval and manipulation. Instead of making multiple requests to different endpoints for specific data (as in traditional REST APIs), GraphQL allows clients to request exactly the data they need in a single query. Sounds more like where you might want to eventually wind up if you're designing an application, right?


Here's a quick breakdown on key concepts:

  1. Schema: At the heart of GraphQL is the schema, which defines the types of data available and the relationships between them. It acts as a contract between the client and the server.

  2. Queries: Clients send queries to the server to request specific data. The query mirrors the shape of the response, allowing clients to specify exactly what they need.

  3. Mutations: Mutations are used to modify data on the server. They enable clients to perform actions like creating, updating, or deleting data.

  4. Fields: In a GraphQL query, you specify the fields you want to retrieve. This granularity means you only get the data you're interested in, reducing over-fetching.

  5. Resolvers: On the server side, resolvers determine how data is fetched or manipulated for each field. Resolvers are functions that retrieve data from various sources, such as databases or external APIs.

  6. Types: GraphQL supports custom types, allowing you to define the shape of your data. Scalar types represent simple values (strings, integers, etc.), and object types define more complex structures.

  7. Introspection: GraphQL provides introspection capabilities, allowing clients to discover the schema and understand what queries, mutations, and types are available.

  8. Pagination: GraphQL includes built-in mechanisms for handling pagination, making it easier to retrieve large sets of data in manageable chunks.

Overall, GraphQL empowers clients to efficiently request and manipulate data, while providing flexibility and avoiding over-fetching or under-fetching issues often seen in REST APIs. It's widely used in modern applications to streamline data interactions between clients and servers.


Now why would you choose GraphQL over a REST API in building your application? That brings me to my next breakdown...


The question of whether REST APIs are better than GraphQL (or vice versa) is not straightforward and largely depends on the specific use case and requirements of your application. Both REST (Representational State Transfer) and GraphQL are popular approaches for building APIs, and each has its own strengths and weaknesses. Here's a comparison of both:


REST API

  1. Simplicity: REST is well-established and follows standard principles, making it easier for developers to understand and implement.

  2. Caching: REST APIs can take advantage of HTTP caching mechanisms to improve performance and reduce server load.

  3. Endpoints: REST typically uses different endpoints for different resources, which can make it more organized and explicit.

  4. Incremental data loading: REST APIs often require multiple requests to fetch related resources, which might result in over-fetching or under-fetching data.

GraphQL

  1. Flexible Queries: With GraphQL, clients can request precisely the data they need, avoiding over-fetching or under-fetching issues.

  2. Single Endpoint: GraphQL APIs typically expose a single endpoint, simplifying the request structure and reducing the number of requests needed.

  3. Versioning: GraphQL allows for evolving APIs without versioning, as clients can request only the fields they support.

  4. Introspection: GraphQL's introspective capabilities enable tools to provide better development experiences, such as documentation and auto-completion.

Which one to choose depends on various factors such as...

  1. Application complexity: GraphQL is more suitable for applications with complex data requirements, while REST might be sufficient for simpler applications.

  2. Performance: REST might be more performant for scenarios with a lot of read-heavy and cacheable requests, while GraphQL may be better for write-heavy and real-time applications.

  3. Existing infrastructure: If you already have a REST API, migrating to GraphQL might require more effort, whereas sticking with REST could be more straightforward.

  4. Team familiarity: If your development team is already experienced in REST, adopting GraphQL might require additional training.

In conclusion, neither REST nor GraphQL is inherently "better" than the other. It's essential to assess your specific project's needs, developer familiarity, and long-term goals to make the right choice. In some cases, a hybrid approach that uses both REST and GraphQL might be the most appropriate solution.


And now, you can talk about REST APIs and GraphQL with ease to develop that high level architecture for your applications! Well at least I hope you now can!

Recent Posts

See All
bottom of page