January 4, 2024

GraphQL in a Platform Engineering World

Michael Watson

Michael Watson

In today’s digital world, optimizing APIs for efficiency and real-time performance is crucial. Your applications need to keep evolving by introducing new features or capabilities over time. Many organizations utilize platform engineering teams to create a self-service developer portal with the goal of accelerating their developers velocity building these new features.  

We’ve seen a trend of platform engineering teams leveraging GraphQL to power their self-service platform; this has been a game changer for them. Instead of shipping updates to your self-service platform in the magnitude of weeks or months, we find our customers are shipping updates daily with some making multiple changes a day. GraphQL is transforming the API landscape, but why are teams choosing GraphQL as their standard instead of other technologies like REST? I’ll give you a hint, it’s about the developer experience but let’s explore that more.

What is GraphQL?

Before we explore the benefits of GraphQL, let’s briefly understand what it is. GraphQL is a specification and query language that defines how to query, mutate, and subscribe to data within a graph. In simpler terms, it allows you to fetch data (queries), make changes or create data (mutations), and receive real-time updates (subscriptions) by specifying the exact fields you need. Unlike traditional REST APIs, GraphQL empowers the client developers to build features faster.

Does GraphQL replace REST?

One common question that arises is whether GraphQL replaces REST APIs entirely. The answer is no; GraphQL and REST can coexist harmoniously. In fact, many organizations leverage GraphQL to enhance their existing REST APIs. GraphQL can be implemented as a layer on top of REST, providing flexibility and improved developer experience. It also uses POST and GET requests, making it adaptable to various scenarios like CDN caching.

How GraphQL Elevates API Efficiency

Many platform engineering teams have chosen GraphQL to power their self-service developer portal because of the benefits they immediately get:

  • Clear Observability: Implementing GraphQL over your APIs offers a unified view of usage and performance, providing a single source of truth to evaluate the effectiveness of your API investments.
  • Accelerated Application Development: With GraphQL, developers are no longer hindered by the need to create new API endpoints. Anyone who can write a GraphQL query can add or modify fields to support new features and functionalities.
  • Expanded Capabilities: GraphQL provides access to advanced features like real-time data with subscriptions and upcoming enhancements such as the @defer directive, which optimizes query execution.
  • Improved User Experience: Ultimately, GraphQL’s goal is to create delightful experiences for users. By simplifying data access for client developers, it streamlines development and leads to better user interfaces.

The Evolution of API Architectures

API architectures have been evolving for decades and we’ve seen a shift towards architectures that empower development teams to build in a “self-service” way. They have a portal to discover the data they need and resources that help them deploy newly created capabilities based on the architecture they decided. Platform engineering is a more recent trend with the goal of automating the infrastructure and providing an internal delightful developer experience. And platform engineering is not going away, here is what Garnter said in their recent post  What is Platform Engineering?:

By 2026, 80% of large software engineering organizations will establish platform engineering teams as internal providers of reusable services, components and tools for application delivery. Platform engineering will ultimately solve the central problem of cooperation between software developers and operators.

Gartner, What is Platform Engineering?

When you only have a single web application, it’s hard to fully grasp how that might grow. As your project gains users and success, you start to get feature requests from end-users or internally. As you build your application, the day comes where you embark on your journey to build a mobile application to bring some of those web capabilities to your users phones. What do you do? Does the new mobile application use the web application’s API? Do you create a new API for the mobile app that uses the web application’s API in a backend-for-frontend (BFF) pattern? What happens when we need to build that next thing on top of large language models (LLM) or for that paradigm like voice (like Amazon Alexa)? When does it stop?

As organizations grow, they face challenges in managing and connecting these diverse APIs efficiently. Developers become responsible for figuring out how to access the data they need to build that new feature. Adding GraphQL as a layer into your architecture can simplify the personas of your organization into consumers and contributors of your platform. A simple way to integrate new capabilities in your graph so that any of your client developers can use those new capabilities in their client applications:

To address these challenges, large enterprises like Wayfair, Intuit, T-Mobile, and US Bank have successfully adopted an architecture that includes the Apollo Router. This router sits in front of multiple GraphQL APIs and orchestrates requests across the entire business. It combines these APIs into what is called the supergraph, enabling intelligent query execution. The router provides multiple benefits:

  • Language Agnostic: Apollo Federation supports over 40 different programming languages and frameworks, allowing flexibility in developing GraphQL APIs.
  • Scalability: Adding more GraphQL servers to the infrastructure becomes straightforward, making GraphQL accessible to everyone.
  • Observability: The router offers comprehensive observability, allowing organizations to track usage down to individual fields and operations.
  • Real-time Support: GraphQL subscriptions and other real-time features are seamlessly integrated, simplifying the implementation of real-time experiences.
  • Efficient Query Execution: The router optimizes query execution by creating query plans, enabling caching, and supporting advanced features like @defer
  • Security: Security controls are built into the router, ensuring a secure GraphQL infrastructure that benefits all client developers.

Advanced GraphQL Features

By using GraphQL for your platform, you instantly get access to new features released into the GraphQL spec. @defer is a new directive being added to the spec and provides a new way to progressively load data that gives a lot of power to your application developers. The GraphQL working group is very active and the community is always proposing new ideas that benefit everyone using GraphQL.

Some items that don’t make the spec go on to become established patterns in GraphQL. A good example is Persisted Queries. GraphQL has always supported GET requests but persisted queries doesn’t just url encode the GraphQL operation. They allow organizations to give an ID for every operation and enable requests to be smaller in size. Most teams also use this persisted query list to lock down production deployments to serve only pre-registered queries, enhancing security and control.

Progressive Loading with @defer

GraphQL’s upcoming @defer directive offers a solution for progressively loading data in queries. This directive allows client developers to indicate that certain data can be incrementally loaded when needed, improving the user experience by reducing loading times. All you have to do is add the @defer directive your GraphQL query and the router will figure out the rest:

Real-time Advancements

GraphQL continues to push the boundaries of real-time capabilities. Features like `@stream` and new protocols within subscriptions, such as the HTTP callback protocol, enhance real-time experiences. The HTTP callback protocol reduces the need for long-lived connections and enables GraphQL APIs to publish updates to the router.

Conclusion

GraphQL has proven to be a game-changer in the API landscape, offering efficiency, real-time capabilities, and a better developer experience. By implementing GraphQL in platform engineering efforts, organizations can modernize their APIs and provide delightful user experiences. The adoption of the Apollo Router and the use of advanced GraphQL features further elevate the capabilities of GraphQL-based ecosystems. With GraphQL, the future of API development looks promising and user-centric. If you want to learn more, start out with docs for API implementers or you can watch my talk from API world below 👇

Written by

Michael Watson

Michael Watson

Read more by Michael Watson