What if your teams could speak the same data language without endless confusion?
Why Shared types across subgraphs in GraphQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have several teams building different parts of a big app, each with its own data. They try to keep their data types separate, but some types are actually the same across teams. Without sharing, each team copies and changes these types on their own.
This copying leads to confusion and mistakes. If one team updates a type, others might not know and keep using old versions. It becomes hard to keep data consistent, and fixing bugs takes a lot of time.
Shared types across subgraphs let teams define common data types once and use them everywhere. This keeps data consistent, reduces errors, and makes collaboration smooth because everyone agrees on the same type definitions.
type User { id: ID! name: String! }
type Product { id: ID! ownerName: String! }type User @shared { id: ID! name: String! }
extend type User @key(fields: "id") { id: ID! }It enables seamless data sharing and consistency across multiple teams and services working together.
In a large online store, the User type is shared across the shopping, payment, and review services, so all parts of the app recognize the same user data without conflicts.
Manual copying of types causes errors and confusion.
Shared types keep data consistent across subgraphs.
Teams collaborate better with a single source of truth for common types.
Practice
@key in shared types across GraphQL subgraphs?Solution
Step 1: Understand the role of
The@key@keydirective marks fields that uniquely identify an entity across subgraphs, enabling them to share the same type.Step 2: Differentiate from other directives
Other directives like@externalor@deprecatedserve different purposes, not unique identification.Final Answer:
To mark fields that uniquely identify an entity across subgraphs -> Option AQuick Check:
@keymarks unique identifiers [OK]
- Confusing @key with @external
- Thinking @key marks optional fields
- Assuming @key defines data types
Solution
Step 1: Identify the directive for external fields
The@externaldirective marks fields that are owned by another subgraph but referenced in the current one.Step 2: Differentiate from other directives
@keymarks unique identifiers,@requiresand@providesrelate to field dependencies, not external ownership.Final Answer:
Use@externaldirective on the field -> Option DQuick Check:
@externalmarks fields from other subgraphs [OK]
- Using @key instead of @external
- Confusing @requires with @external
- Not marking external fields at all
type Product @key(fields: "id") {
id: ID!
name: String
price: Float @external
}Which statement is true about the
price field?Solution
Step 1: Analyze the
The@externaldirective onprice@externaldirective meanspriceis not owned here but comes from another subgraph.Step 2: Understand the role of
The@keyonididfield is the unique identifier, sopriceis not an ID.Final Answer:
It is defined in another subgraph and referenced here -> Option CQuick Check:
@externalmeans field is from another subgraph [OK]
- Thinking @external means field is owned here
- Confusing @key with @external
- Assuming @external means deprecated
type User @key(fields: "userId") {
userId: ID!
email: String @external
name: String
}Which statement is true about the
email field?Solution
Step 1: Analyze the
The@externaldirective onemail@externaldirective meansemailis defined in another subgraph and referenced here.Step 2: Differentiate from other fields
userIdis the@keyfield provided locally,nameis owned locally (no directive).Final Answer:
It is defined in another subgraph and referenced here -> Option AQuick Check:
@externalmeans field from another subgraph [OK]
- Thinking @external means owned locally
- Believing @key must include all fields
- Assuming all fields need @external
Book type. Subgraph A defines:type Book @key(fields: "isbn") {
isbn: ID!
title: String
}Subgraph B defines:
extend type Book @key(fields: "isbn") {
isbn: ID! @external
author: String
}Which statement best describes how these shared types work together?
Solution
Step 1: Identify ownership of fields
Subgraph A definesBookwithisbnandtitle, so it owns these fields.Step 2: Understand extension in Subgraph B
Subgraph B extendsBook, markingisbnas@external(owned by A) and addsauthor.Step 3: Confirm no conflicts
Using@keywith the same fieldisbnallows both subgraphs to share the type without conflict.Final Answer:
Subgraph A ownsisbnandtitle, Subgraph B extendsBookusingisbnas key and addsauthor-> Option BQuick Check:
Extension uses @external keys to share types [OK]
- Thinking both subgraphs own the same key field
- Believing extension requires redefining all fields
- Assuming conflicts occur with shared keys
