LibraryDefining a Supergraph Schema

Defining a Supergraph Schema

Learn about Defining a Supergraph Schema as part of GraphQL API Development and Federation

Introduction to Defining a Supergraph Schema in Apollo Federation

Apollo Federation allows you to build a unified GraphQL API from multiple independent GraphQL services. A core concept in this architecture is the supergraph schema, which represents the combined schema of all your services. This section will guide you through understanding and defining this essential component.

What is a Supergraph Schema?

The supergraph schema is the gateway to your federated GraphQL API. It's a single, unified schema that aggregates the types, queries, mutations, and subscriptions from all your individual GraphQL services (called subgraphs). This schema defines the complete data graph that clients can query.

The supergraph schema is the aggregated view of all your federated GraphQL services.

Think of the supergraph schema as a master blueprint that combines the functionalities of individual service blueprints. It ensures that clients interact with a single, coherent API, abstracting away the underlying service boundaries.

In Apollo Federation, each microservice exposes its own GraphQL schema. The Apollo Gateway, which acts as the entry point for clients, fetches these individual schemas. It then composes them into a single, unified supergraph schema. This process involves resolving type extensions, merging fields, and ensuring consistency across the entire graph. The supergraph schema is what the gateway uses to understand how to route client requests to the appropriate subgraph services.

Key Concepts in Supergraph Schema Definition

Defining a supergraph schema involves understanding how individual subgraphs contribute to the overall graph. This is achieved through specific directives and conventions.

Entity Types and Extensions

In a federated graph, certain types are designated as entities. Entities are objects that can be uniquely identified by a key. Subgraphs can extend existing entities defined by other subgraphs, adding new fields or capabilities. This is a fundamental mechanism for composing the supergraph.

What is the primary mechanism for combining functionality from different subgraphs in Apollo Federation?

Extending entity types.

Federation Directives

Apollo Federation uses specific directives to mark types as entities, define their keys, and indicate relationships. The most common ones are:

DirectivePurpose
@keyMarks a type as an entity and specifies its unique identifier (key).
@extendsIndicates that a type defined in another subgraph is being extended.
@externalMarks a field as being defined in another subgraph, meaning it's not directly resolvable by the current subgraph.
@requiresSpecifies that a field on an extended entity requires fields from the current subgraph to resolve.
@providesIndicates that a field on an entity is resolvable by the current subgraph, even if it's defined elsewhere.

Directives are the language of federation, enabling subgraphs to communicate their structure and dependencies to the gateway.

Schema Composition

The Apollo Gateway is responsible for composing the individual subgraph schemas into the final supergraph schema. This process involves:

  1. Fetching schemas from all registered subgraphs.
  2. Merging types, fields, and directives.
  3. Resolving type extensions and ensuring consistency.
  4. Validating the combined schema.
  5. Providing a single, unified schema to clients.

Imagine you have a User entity. The accounts subgraph might define the User type with id and email. The reviews subgraph might extend this User entity to add a reviews field. The supergraph schema will then contain the User type with both id, email, and reviews fields, all managed by their respective subgraphs.

📚

Text-based content

Library pages focus on text content

Practical Considerations for Defining Your Supergraph Schema

When designing your federated schema, keep these best practices in mind:

Identify Entities Early

Determine which types will serve as entities across your services. These are the objects that will be shared and potentially extended. A clear entity strategy is crucial for a well-structured federated graph.

Use `@key` and `@extends` Appropriately

Apply the

code
@key
directive to the subgraph that owns the primary definition of an entity. Use
code
@extends
in other subgraphs to add fields to that entity. Ensure the key fields are consistent across all definitions.

Leverage `@external` and `@requires` for Cross-Subgraph Dependencies

When a subgraph needs to reference a field from another subgraph (e.g., to resolve a relationship), use

code
@external
on the referenced field in the subgraph that needs it. If resolving a field requires data from another subgraph, use
code
@requires
to specify those dependencies.

Maintain Schema Consistency

Ensure that types and fields have consistent names, arguments, and return types across all subgraphs. The gateway will validate this during composition.

Summary

Defining a supergraph schema in Apollo Federation is about orchestrating multiple GraphQL services into a unified API. By understanding entities, leveraging federation directives, and adhering to best practices for schema composition, you can build a robust and scalable federated GraphQL API.

Learning Resources

Apollo Federation: Supergraph Schema(documentation)

The official Apollo documentation detailing the concepts and directives involved in defining a supergraph schema.

Apollo Federation: Entities(documentation)

Learn how to define and extend entities, a cornerstone of Apollo Federation's schema composition.

Apollo Federation: Schema Composition(documentation)

Understand the process by which Apollo Gateway combines individual subgraph schemas into a unified supergraph.

Building a Federated GraphQL API with Apollo Federation(video)

A comprehensive video tutorial that walks through setting up Apollo Federation and defining schemas.

GraphQL Federation: A Deep Dive(video)

An in-depth explanation of GraphQL Federation, covering schema design and composition.

Apollo Federation Tutorial: Getting Started(tutorial)

A step-by-step tutorial to help you set up your first Apollo Federation project and define basic schemas.

Understanding GraphQL Federation Directives(blog)

A blog post that breaks down the purpose and usage of key federation directives like @key, @extends, and @external.

Federated GraphQL: The Future of APIs?(blog)

An article discussing the benefits and architectural patterns of using GraphQL Federation.

GraphQL Federation - A Practical Guide(blog)

A practical guide to implementing GraphQL Federation, including schema definition and service setup.

Apollo Federation(wikipedia)

Wikipedia entry providing a general overview of Apollo Federation and its role in building distributed GraphQL APIs.