Shared types let different parts of a GraphQL system use the same data shapes. This helps keep data consistent and easy to manage.
Shared types across subgraphs in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
extend type User @key(fields: "id") { id: ID! @external username: String } # In another subgraph extend type User @key(fields: "id") { id: ID! email: String }
@key marks the field(s) used to identify the shared type.
@external marks fields that exist in another subgraph.
upc. The price field is added in this subgraph.extend type Product @key(fields: "upc") { upc: String! @external price: Int }
id. The name field is defined in this subgraph.extend type User @key(fields: "id") { id: ID! @external name: String }
type Review @key(fields: "id") { id: ID! body: String author: User @provides(fields: "username") }
This example shows two subgraphs sharing the User type. Subgraph A knows username, Subgraph B knows email. Both use id to identify the same User.
# Subgraph A schema extend type User @key(fields: "id") { id: ID! @external username: String } # Subgraph B schema extend type User @key(fields: "id") { id: ID! email: String } type Query { me: User }
Shared types use @key to identify the unique field(s) that link the same type across subgraphs.
Fields marked @external are defined in other subgraphs and not stored locally.
Using shared types helps avoid duplication and keeps data consistent across services.
Shared types let multiple subgraphs use the same data structure identified by unique keys.
Use @key to mark the identifying fields and @external for fields from other subgraphs.
This approach helps build a unified GraphQL API from multiple services easily.
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
