0
0
GraphQLquery~3 mins

Why Shared types across subgraphs in GraphQL? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your teams could speak the same data language without endless confusion?

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
type User { id: ID! name: String! }
type Product { id: ID! ownerName: String! }
After
type User @shared { id: ID! name: String! }
extend type User @key(fields: "id") { id: ID! }
What It Enables

It enables seamless data sharing and consistency across multiple teams and services working together.

Real Life Example

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.

Key Takeaways

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.