In the ever-evolving landscape of GraphQL development, the need for efficient orchestration and aggregation of data from distributed and independent GraphQL services has led to the emergence of GraphQL Federation.
Federated GraphQL involves combining multiple GraphQL services – each independent and autonomous, free to implement, maintain, and ship their own schema without being coupled to any other service – into a unified schema, providing a single endpoint for clients to seamlessly access data from various sources.
A Federation Gateway serves as that single entry point to this federated architecture, stitching together data from diverse services on-demand based on client queries.
In this article, we''ll explore five top GraphQL Federation V1/V2 compatible gateways in 2023. By delving into their functionalities, strengths, and nuances, we aim to empower you with the insights needed to make well-informed decisions in this dynamic GraphQL landscape whether you''re a seasoned developer or new to GraphQL Federation.
Let’s get started.
What is a Gateway?
At the heart of GraphQL Federation lies the router or federation gateway, functioning as a centralized entry point that unifies the data retrieval process.
While it shares a superficial resemblance to an API Gateway by acting as a proxy, the federation gateway distinguishes itself through its unique capabilities – it dynamically stitches together data from independent GraphQL services, providing not only a consolidated interface for clients but also delivering additional functionalities such as caching mechanisms, authentication and authorization, observability and analytics, and more.
In essence, while Federation Gateways may share the role of a proxy with API Gateways, their distinctive ability to dynamically stitch data and provide a range of advanced functionalities positions them as specialized tools tailored specifically for the unique requirements of GraphQL Federation.
But which gateway do I choose for my app?
What do you have to do if you’re building large-scale GraphQL applications right now in 2023, and decide you need Federation? Which Federation V1 (or in some cases, V2) compatible Gateway do you choose? Let’s examine a few options open to GraphQL devs.
1. Apollo Router
Apollo Router emerges as a powerful gateway solution crafted by the Apollo team to tackle the intricacies of GraphQL Federation in contemporary applications. This gateway stands out for its unique approach, providing a precompiled Rust binary that not only optimizes performance but also ensures reliability in the dynamic task of stitching data across diverse GraphQL services, handling large numbers of concurrent requests.
- Apollo Router is the first party Federation gateway from Apollo themselves.
- Out-of-the-box support for GraphQL Subscriptions, enabling developers to seamlessly integrate real-time data updates into their applications. This feature enhances the overall user experience by providing timely and reactive information.
- With built-in support for authentication via JWT (JSON Web Tokens), Apollo Router provides a secure and efficient means to manage user access to GraphQL services.
- Offers a high degree of customization by allowing developers to write Rust or Rhai scripts. This flexibility empowers developers to tailor the gateway''s behaviour to specific use cases, providing a granular level of control.
- Apollo Router prioritizes observability by incorporating built-in support for OpenTelemetry. This ensures that developers can gather comprehensive insights into the performance and behaviour of the federated GraphQL system, facilitating effective monitoring and troubleshooting.
- Apollo Router distinguishes itself by being more than just a library—it''s a precompiled Rust binary. This architecture results in significantly lower latency compared to the older Node.js Apollo Gateway.
- As a precompiled binary, Apollo Router boasts fewer dependencies than its Node.js counterpart. This reduction in dependencies not only enhances security by minimizing potential vulnerabilities, but also contributes to greater maintainability over the long run.
- Strategically built to take full advantage of Apollo''s own GraphOS platform. This integration simplifies the management of federated GraphQL in the cloud, providing developers with a seamless and optimized experience.
- Apollo Router leverages Apollo''s rover CLI to compose subgraphs into a federated graph, with minimal code required.
- Apollo Router uses the Elastic V2 license, which is not an OSI-approved open source license. It''s essential for developers to thoroughly research the Elastic V2 license to understand its implications and restrictions. The licensing model can have an impact on how the gateway can be used, distributed, and modified, making it crucial to align with project goals and compliance requirements.
- Consider your existing tooling and ecosystem before opting for Apollo Router. If your dev environment cannot readily adapt to the workflow of a precompiled binary, or if writing Rhai scripts for customization poses challenges, Apollo Router might not be the most suitable option. Compatibility with your technology stack is a critical factor in ensuring a seamless integration of the gateway into your development workflow.
- Apollo Router management is closely tied to the adoption of Apollo''s cloud platform. Developers should assess whether this alignment with cloud technology is feasible and aligns with project requirements. If there are constraints, such as compliance reasons that prevent the use of cloud platforms, this could pose challenges in getting started with Apollo Router.
2. Apollo Server
- Supports both Federation V1 and V2, providing developers with a Node.js based flexibility in adopting the latest federation spec.
- Apollo Server can perform its own supergraph schema composition on startup using introspection. This is in contrast to the Apollo Router, which requires external tools like the Rover CLI or managed federation via GraphOS for schema composition.
Again, Apollo Server shares the Elastic V2 license issue with Apollo Router. Ensure it aligns with your project''s licensing requirements.
Compared to Apollo Router, Apollo Server may introduce more latency, lower throughput, and additional dependencies. If speed, concurrency, and security are top priorities, consider Apollo Router.
3. WunderGraph Cosmo
WunderGraph Cosmo stands out as a fully open-source (Apache 2.0 license) Federation V1/V2 compatible router/gateway, meticulously crafted in Go, and fully self-hostable. This gateway offers an incredibly fast, stable, and comprehensive solution for developers adopting distributed GraphQL architectures.
Cosmo Router is powered by graphql-go-tools, recognized as the fastest and most reliable implementation of Federation V1. Building upon this foundation, Cosmo Router introduces additional optimizations, providing even more substantial performance gains.
- WunderGraph Cosmo empowers developers with the ability to self-host the stateless router completely on-prem if needed, using any Kubernetes service, ensuring data autonomy and freedom from vendor lock-in.
- WunderGraph Cosmo Router implements a custom DataLoader pattern known as DataLoader 3.0. This innovative approach utilizes Breadth-first data loading to address the N+1 problem. By optimizing data loading in this way, performance is significantly improved, up to 5 times faster. Moreover, it simplifies code complexity, especially in multithreaded languages like Go, Rust, Java, etc., where the standard DataLoader pattern might encounter challenges related to concurrency.
- Includes ''Ludicrous Mode,'' a groundbreaking feature that dramatically enhances performance, specifically designed for read-only requests that are frequently executed. This mode is tailored to deliver exceptional speed and efficiency, making it an ideal choice for scenarios where high-speed data retrieval is paramount.
- The gateway offers a suite of tools for comprehensive observability, including analytics, metrics, tracing, and logging.
Documentation: The WunderGraph Cosmo documentation provides extensive insights into implementation details and usage guidelines.
Additional Resources: Explore the WunderGraph Cosmo Plans for information on hosting options and additional features.
- Unique Aspects: Cosmo Router allows seamless extension through the creation of custom Go modules, leveraging predefined interfaces. This approach is far more intuitive and flexible means of extending functionality, and significantly simplifies the process compared to writing custom Rust/Rhai scripts, as is required with the Apollo Router.
- WunderGraph Cosmo goes beyond conventional observability by integrating with OpenTelemetry for metrics, Prometheus for service monitoring, and even RED metrics for router traffic analysis. This built-in observability suite provides real-time insights into key performance indicators such as error rates, average request/response times, and router traffic. This robust monitoring system empowers developers to proactively manage and optimize their GraphQL services.
- WunderGraph Cosmo offers unparalleled flexibility with its 100% self-hostable architecture, enabling deployment on any Kubernetes service, including AWS, Azure, Google for production environments, and Minikube for local development. Developers have the freedom to host the entire platform on-premises, ensuring data autonomy and control.
- Additionally, the option to leverage Cosmo Cloud for running stateful components while self-hosting the Cosmo Router (stateless) adds another layer of versatility if you’d rather not take on the additional complexity.
Considerations: WunderGraph Cosmo’s commitment to open-source principles, self-hostability, and freedom from vendor lock-in make it a compelling choice for large-scale GraphQL applications.
4. GraphQL Mesh
- GraphQL Mesh excels in integrating diverse data sources, including GraphQL services, OpenAPI REST, gRPC, databases, and more, allowing for a flexible and comprehensive approach. You can create type-safe GraphQL APIs from any datasource, letting you work with non-GraphQL services as if they were native GraphQL.
- GraphQL Mesh stands out for its high level of customization. Developers have the flexibility to alter, extend, and manipulate schemas, adding middleware to existing resolvers, and more.
- Features a plugin-based system that extends the gateway''s functionality. Developers can easily incorporate plugins to add features such as caching, authentication, rate limiting, mocking, live queries, and more. This modular and extensible architecture empowers developers to enhance the capabilities of their gateway based on specific use cases and requirements.
- Prioritizes observability by incorporating built-in support for monitoring tools like Prometheus, Sentry, OpenTelemetry, and more. This ensures that developers can gain insights into the performance and behaviour of the gateway, facilitating effective monitoring, debugging, and optimization.
Unique Aspects: The standout feature of GraphQL Mesh lies in its versatility. It goes beyond traditional GraphQL gateways by supporting various data sources, providing a unique "anything to GraphQL API" capability without tightly coupling services.
- GraphQL Mesh supports a vast array of services and databases, including OpenAPI REST, GraphQL, gRPC, Federation supergraph, and more. It doesn''t restrict developers to GraphQL-only services, allowing integration with databases like Postgres, MongoDB, SQLite, MySQL, Neo4J, and others. Notably, GraphQL Mesh supports services that may not be officially compliant with Apollo Federation, providing a level of compatibility that extends beyond conventional boundaries.
- GraphQL Mesh empowers developers to modify the schema effortlessly, enabling precise control over the contents of GraphQL requests and responses. This flexibility allows for common transforms such as naming convention changes, type merging, and direct extension of source schemas.
- GraphQL Mesh opens the door to extended functionality through open-source plugin packages. Developers can leverage a variety of plugins to enhance and customize the gateway''s behaviour according to their specific needs. The availability of a plugin ecosystem, documented here, further enriches the extensibility and versatility of GraphQL Mesh, making it a dynamic and adaptable solution.
Use GraphQL Mesh if you have specific needs:
- GraphQL Mesh is the ideal choice if you require broad compatibility for integrating various data sources. Whether it''s REST APIs, gRPC, SOAP, databases, OpenAPI/Swagger, or more, GraphQL Mesh excels in transforming these disparate sources into cohesive GraphQL schemas. If your project involves working with a mix of data services, GraphQL Mesh provides the flexibility needed for a unified GraphQL API.
- If speeding up your coding process is a priority, GraphQL Mesh offers automatic resolver generation. This feature can significantly streamline the development workflow by automatically creating resolvers for your schema based on the underlying data sources. This automation not only enhances productivity but also ensures consistency in resolver implementation.
- GraphQL Mesh provides developers with full control over schema stitching and transformation. This level of control is essential for projects that require fine-tuning the integration of diverse data sources into a unified GraphQL API.
- Mercurius provides a novel solution for a Fastify-based GraphQL Federation.
- Fast and efficient.
Documentation: The official Mercurius documentation offers comprehensive insights into usage, configuration, and best practices.
- Mercurius distinguishes itself with its emphasis on speed and efficiency, making it an ideal choice for projects where performance is a critical consideration. While supporting only Federation V1, it seamlessly integrates into the Fastify ecosystem, catering to developers seeking Federated GraphQL in a Fastify environment.
- Mercurius goes beyond standard functionality by allowing developers to leverage hooks throughout the GraphQL gateway lifecycle. These hooks, detailed in the documentation, empower developers to interact with any part of the GraphQL gateway process, starting from the moment an incoming GraphQL request is received. This level of flexibility enables fine-grained control and customization, facilitating the integration of specific functionalities as needed.
- Mercurius supports customizable persisted queries, providing developers with the ability to tailor query persistence according to their project requirements. This feature enhances efficiency and can contribute to optimizing the overall performance of the GraphQL gateway.
- Mercurius introduces a forward-looking approach by enabling the use of WebSockets for sending queries and mutations instead of traditional HTTP requests. This not only opens up avenues for real-time data updates but also reduces latency compared to the conventional request-response cycle.
Considerations: Developers exploring federation options within a Fastify environment will find Mercurius to be their best choice. Its lightweight nature and focus on speed align well with Fastify’s goals, proving particularly useful in scenarios where efficient Federation implementation is a priority.
In conclusion, our exploration of GraphQL Federation and Federation Gateways highlights WunderGraph Cosmo as a standout option. Its robust feature set, reasonable pricing, self-hostability, and active developer community make it an attractive choice for large-scale GraphQL applications in 2023.
However, it needs to be said that the challenges associated with the Apollo Federation spec, including its nebulous nature and Elastic V2 licensing, may hinder its widespread adoption. In response to this, the Open Federation initiative offers a promising and open alternative, developed by contributors such as WunderGraph and GraphQL Hive.