Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

Schema Composition

Learn how subgraph schemas combine into a supergraph schema


In , composition is the process of combining a set of subgraph schemas into a supergraph schema:

(Composition succeeds)
Subgraph
schema
A
Subgraph
schema
B
Subgraph
schema
C
🛠
Composition
Supergraph schema
(A + B + C + routing machinery)

The includes all of the type and definitions from your . It also includes metadata that enables your to intelligently route incoming across all of your different subgraphs.

Supported methods

You can perform schema with any of the following methods:

Automatically with GraphOS

With managed federation, performs composition automatically whenever one of your updates its published schema. This enables your running router to dynamically fetch an updated supergraph schema from Apollo as soon as it's available:

GraphOS
Your infrastructure
Publishes schema
Publishes schema
Updates config
Polls for config changes
Apollo Schema
Registry
Apollo
Uplink
Products
subgraph
Reviews
subgraph
Router

To learn how to perform composition with , see the quickstart.

NOTE

also provides a schema linter with composition specific rules to help you follow best practices. You can set up for your in or perform one-off linting with the . Check out the schema linting docs to learn more.

Manually with the Rover CLI

The Rover CLI supports a supergraph compose command that you can use to compose a supergraph schema from a collection of subgraph schemas:

rover supergraph compose --config ./supergraph-config.yaml

To learn how to install and use this command, see the quickstart.

Breaking composition

Sometimes, your subgraph schemas might conflict in a way that causes composition to fail. This is called breaking composition.

For example, take a look at these two subgraph schemas:

Subgraph A
type Event @shareable {
timestamp: String!
}
Subgraph B
type Event @shareable {
timestamp: Int!
}

One defines Event.timestamp as a String, and the other defines it as an Int. doesn't know which type to use, so it fails.

NOTE

For examples of valid inconsistencies in field return types, see Differing shared field return types.

Breaking composition is a helpful feature of federation! Whenever a team modifies their , those changes might conflict with another subgraph. But that conflict won't affect your router, because composition fails to generate a new supergraph schema. It's like a compiler error that prevents you from running invalid code.

Rules of composition

In Federation 2, your subgraph schemas must follow all of these rules to successfully compose into a supergraph schema:

  • Multiple subgraphs can't define the same field on an , unless that field is shareable.
  • A shared field must have both a compatible return type and compatible types across each defining subgraph.
  • If multiple subgraphs define the same type, each field of that type must be resolvable by every valid GraphQL that includes it.

Unresolvable field example

This example presents a field of a shared type that is not always resolvable (and therefore breaks composition).

Consider these subgraph schemas:

Subgraph A
type Query {
positionA: Position!
}
type Position @shareable {
x: Int!
y: Int!
}
Subgraph B
type Query {
positionB: Position!
}
type Position @shareable {
x: Int!
y: Int!
z: Int!
}

Note the following about these two subgraphs:

  • They both define a shared Position type.
  • They both define a top-level Query field that returns a Position.
  • B's Position includes a z field, whereas Subgraph A's definition only includes shared x and y .

Individually, these subgraph schemas are perfectly valid. However, if they're combined, they break composition. Why?

The composition process attempts to merge inconsistent type definitions into a single definition for the supergraph schema. In this case, the resulting definition for Position exactly matches Subgraph B's definition:

Hypothetical supergraph schema
type Query {
# From A
positionA: Position!
# From B
positionB: Position!
}
type Position {
# From A+B
x: Int!
y: Int!
# From B
z: Int!
}

Based on this hypothetical supergraph schema, the following should be valid:

query GetPosition {
positionA {
x
y
z # ⚠️ Can't be resolved! ⚠️
}
}

Here's our problem. Only Subgraph A can resolve Query.positionA, because Subgraph B doesn't define the field. But Subgraph A doesn't define Position.z!

If the router sent this query to Subgraph A, it would return an error. And without extra configuration, Subgraph B can't resolve a z value for a Position in Subgraph A. Therefore, Position.z is unresolvable for this query.

Composition recognizes this potential issue, and it fails. The hypothetical supergraph schema above would never actually be generated.

Position.z is an example of a field that is not always resolvable. So now, how do we make sure that such a field is always resolvable?

Solutions for unresolvable fields

There are multiple solutions for making sure that a field of a shared type is always resolvable. Choose a solution based on your use case:

Define the field in every subgraph that defines the type.

If every subgraph that defines a type could resolve every field of that type without introducing complexity, a straightforward solution is to define and resolve all fields in all of those subgraphs:

Subgraph A
type Position @shareable {
x: Int!
y: Int!
z: Int
}
Subgraph B
type Position @shareable {
x: Int!
y: Int!
z: Int!
}

In this case, if Subgraph A only cares about the x and y fields, its for z can always return null.

This is a useful solution for shared types that encapsulate simple data.

NOTE

You can use the @inaccessible to incrementally add a value type field to multiple subgraphs without breaking composition. Learn more.

Make the shared type an entity

Subgraph A
type User @key(fields: "id") {
id: ID!
name: String!
}
Subgraph B
type User @key(fields: "id") {
id: ID!
age: Int!
}

If you make a shared type an entity, different subgraphs can define any number of different fields for that type, as long as they all define key fields for it.

This is a useful solution when a type corresponds closely to an entry in a data store that one or more of your subgraphs has access to (e.g., a Users database).

Merging types from multiple subgraphs

If a particular GraphQL type is defined differently by different subgraphs, composition uses one of two strategies to merge those definitions: union or intersection.

  • Union: The supergraph schema includes all parts of all subgraph definitions for the type.
  • Intersection: The supergraph schema includes only the parts of the type that are present in every subgraph that defines the type.

The merging strategy that composition uses for a particular type depends on the type, as described below.

Object, union, and interface types

Composition always uses the union strategy to merge object, union, and interface types.

Consider the following subgraph schemas:

Subgraph A
type User @key(fields: "id") {
id: ID!
name: String!
email: String!
}
union Media = Book | Movie
interface BookDetails {
title: String!
author: String!
}
Subgraph B
type User @key(fields: "id") {
id: ID!
age: Int!
}
union Media = Book | Podcast
interface BookDetails {
title: String!
numPages: Int
}

When these subgraph schemas are composed, the composition process merges the three corresponding types by union. This results in the following type definitions in the supergraph schema:

Supergraph schema
type User {
id: ID!
age: Int!
name: String!
email: String!
}
union Media = Book | Movie | Podcast
interface BookDetails {
title: String!
author: String!
numPages: Int
}

Because composition uses the union strategy for these types, subgraphs can contribute distinct parts and guarantee that those parts will appear in the composed supergraph schema.

NOTE

If different subgraphs contribute different fields to an interface type, any that implement that interface must define all contributed fields from all subgraphs. Otherwise, composition fails.

Input types and field arguments

Composition always uses the intersection strategy to merge input types and field . This ensures that the router never passes an argument to a subgraph that doesn't define that argument.

Consider the following subgraph schemas:

Subgraph A
input UserInput {
name: String!
age: Int
}
type Library @shareable {
book(title: String, author: String): Book
}
Subgraph B
input UserInput {
name: String!
email: String
}
type Library @shareable {
book(title: String, section: String): Book
}

These subgraphs define different fields for the UserInput input type, and they define different arguments for the Library.book field. After composition merges using intersection, the supergraph schema definitions look like this:

Supergraph schema
input UserInput {
name: String!
}
type Library {
book(title: String): Book
}

As you can see, the supergraph schema includes only the input fields and arguments that both subgraphs define.

NOTE

If the intersection strategy would omit an input field or argument that is non-nullable, composition fails. This is because at least one subgraph requires that field or argument, and the router can't provide it if it's omitted from the supergraph schema.

When defining input types and field arguments in multiple subgraphs, make sure that every non-nullable field and argument is consistent in every subgraph. For examples, see Arguments.

Enums

If an enum definition differs between subgraphs, the composition strategy depends on how the enum is used:

ScenarioStrategy
The enum is used as the return type for at least one object or interface field.Union
The enum is used as the type for at least one field argument or input type field.Intersection
Both of the above are true.All definitions must match exactly

Examples of these scenarios are provided below.

Enum composition examples

Union

Consider these subgraph schemas:

Subgraph A
enum Color {
RED
GREEN
BLUE
}
type Query {
favoriteColor: Color
}
Subgraph B
enum Color {
RED
GREEN
YELLOW
}
type Query {
currentColor: Color
}

In this case, the Color enum is used as the return type of at least one object field. Therefore, composition merges the Color enum by union, so that all possible subgraph return values are valid.

This results in the following type definition in the supergraph schema:

Supergraph schema
enum Color {
RED
GREEN
BLUE
YELLOW
}
Intersection

Consider these subgraph schemas:

Subgraph A
enum Color {
RED
GREEN
BLUE
}
type Query {
products(color: Color): [Product]
}
Subgraph B
enum Color {
RED
GREEN
YELLOW
}
type Query {
images(color: Color): [Image]
}

In this case, the Color enum is used as the type of at least one field argument (or input type field). Therefore, composition merges the Color enum by intersection, so that subgraphs never receive a client-provided enum value that they don't support.

This results in the following type definition in the supergraph schema:

Supergraph schema
# BLUE and YELLOW are removed via intersection
enum Color {
RED
GREEN
}
Exact match

Consider these subgraph schemas:

Subgraph A
enum Color {
RED
GREEN
BLUE
}
type Query {
favoriteColor: Color
}
Subgraph B
enum Color {
RED
GREEN
YELLOW
}
type Query {
images(color: Color): [Image]
}

In this case, the Color enum is used as both:

  • The return type of at least one object field
  • The type of at least one field argument (or input type field)

Therefore, the definition of the Color enum must match exactly in every subgraph that defines it. An exact match is the only scenario that enables union and intersection to produce the same result.

The subgraph schemas above do not compose, because their definitions of the Color enum differ.

Directives

Composition handles a directive differently depending on whether it's an "executable" directive or a "" directive.

Executable directives

Executable are intended to be used by clients in their queries. They are applied to one or more of the executable directive locations. For example, you might have a directive definition of directive @lowercase on FIELD, which a client could use in their query like so:

query {
getSomeData {
someField @lowercase
}
}

An executable directive is composed into the supergraph schema only if all of the following conditions are met:

  • The directive is defined in all subgraphs.
  • The directive is defined identically in all subgraphs.
  • The directive is not included in any @composeDirective directives.

Type system directives

directives help define the structure of the schema and are not intended for use by clients. They are applied to one or more of the type system directive locations.

These directives are not composed into the supergraph schema, but they can still provide information to the router via the @composeDirective directive.

Previous
Overview
Next
Introduction to Entities
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company