0
0
GraphQLquery~10 mins

Shared types across subgraphs in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shared types across subgraphs
Define shared type in Subgraph A
Define shared type in Subgraph B
Mark type as @key in each subgraph
Gateway composes subgraphs
Shared type resolved across subgraphs
Queries can access combined fields
Shared types are defined in multiple subgraphs with matching keys, then composed by the gateway to allow queries accessing combined fields.
Execution Sample
GraphQL
type Product @key(fields: "id") {
  id: ID!
  name: String
}

extend type Product @key(fields: "id") {
  id: ID! @external
  price: Float
}
Defines a shared Product type in two subgraphs, one with 'name', another extending it with 'price'.
Execution Table
StepActionSubgraph A Type StateSubgraph B Type StateGateway Composition Result
1Define Product type with id and name in Subgraph AProduct {id: ID!, name: String}N/AN/A
2Extend Product type with id and price in Subgraph BProduct {id: ID!, name: String}Product {id: ID! (external), price: Float}N/A
3Mark @key on id field in both subgraphsProduct @key(fields: "id")Product @key(fields: "id")N/A
4Gateway composes subgraphs, merges Product types by idN/AN/AProduct {id: ID!, name: String, price: Float}
5Query requests Product with name and priceN/AN/AReturns combined fields from both subgraphs
6Execution endsN/AN/AComposition complete, shared type resolved
💡 Composition ends after merging shared types by @key fields and resolving combined schema.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
Subgraph A Product Typeundefined{id, name}{id, name}{id, name}{id, name}
Subgraph B Product Typeundefinedundefined{id (external), price}{id (external), price}{id (external), price}
Gateway Composed Product Typeundefinedundefinedundefined{id, name, price}{id, name, price}
Key Moments - 3 Insights
Why do both subgraphs need to mark the shared type with the same @key?
Because the gateway uses the @key field to match and merge the shared types across subgraphs, as shown in execution_table step 3 and 4.
What does the @external directive mean in the extended type?
@external marks fields that exist in another subgraph, so the gateway knows this field is owned elsewhere, as seen in execution_table step 2.
How does the gateway combine fields from both subgraphs into one type?
The gateway merges fields from both subgraphs based on the shared @key, creating a combined type with all fields, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what fields does the composed Product type have after step 4?
Aid, name
Bid, name, price
Cid, price
Dname, price
💡 Hint
Check the 'Gateway Composition Result' column at step 4 in the execution_table.
At which step does the gateway merge the shared types from subgraphs?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where 'Gateway composes subgraphs' in the execution_table.
If the @key directive was missing in one subgraph, what would happen during composition?
AThe gateway would ignore the subgraph missing @key.
BThe gateway would still merge types correctly.
CThe gateway would fail to merge the shared types properly.
DThe gateway would merge only the fields from the subgraph with @key.
💡 Hint
Refer to key_moments about the importance of matching @key directives.
Concept Snapshot
Shared types across subgraphs use the same type name and @key directive.
Each subgraph defines or extends the type with @external fields.
The gateway composes these types by merging fields based on the @key.
This allows queries to access combined fields from multiple subgraphs.
Missing @key prevents proper merging.
Use @external to mark fields owned by other subgraphs.
Full Transcript
In GraphQL federation, shared types are defined in multiple subgraphs with the same name and marked with the @key directive on a unique field like 'id'. One subgraph defines the base type with some fields, and another subgraph can extend it with additional fields marked as @external if owned elsewhere. The gateway composes these subgraphs by merging the shared types using the @key field to identify matching types. This results in a combined type that includes all fields from the subgraphs. Queries can then request fields from the combined type seamlessly. If the @key directive is missing in any subgraph, the gateway cannot merge the types properly, causing errors or incomplete schemas.