Schema evolution strategies help us change a GraphQL schema safely without breaking existing apps.
Schema evolution strategies in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
# Example of adding a new optional field type User { id: ID! name: String! email: String phone: String # new optional field added } # Example of deprecating a field type User { id: ID! name: String! email: String @deprecated(reason: "Use contactEmail instead") contactEmail: String }
Adding new optional fields is safe because old clients ignore unknown fields.
Deprecating fields with @deprecated helps clients know which fields to stop using.
discount without breaking existing queries.# Adding a new optional field type Product { id: ID! name: String! description: String price: Float discount: Float # new optional field }
oldPrice as deprecated so clients know to switch to price.# Deprecating a field
type Product {
id: ID!
name: String!
oldPrice: Float @deprecated(reason: "Use price instead")
price: Float
}# Renaming a field safely
type User {
id: ID!
fullName: String
name: String @deprecated(reason: "Use fullName instead")
}This schema shows a user type where email is deprecated and replaced by contactEmail. A new optional field phone is added safely.
type Query {
user(id: ID!): User
}
type User {
id: ID!
name: String!
email: String @deprecated(reason: "Use contactEmail instead")
contactEmail: String
phone: String # newly added optional field
}Always add new fields as optional to avoid breaking existing clients.
Use the @deprecated directive to signal fields that should not be used anymore.
Removing fields should be done carefully, usually after clients have stopped using them.
Schema evolution lets you change GraphQL APIs without breaking apps.
Add new fields as optional and deprecate old ones instead of removing immediately.
Use clear deprecation reasons to guide clients to update their queries.
Practice
Solution
Step 1: Understand schema evolution concept
Schema evolution allows changes to the API while keeping existing clients working.Step 2: Identify the correct purpose
Removing fields immediately or making all fields mandatory breaks clients, so those are incorrect.Final Answer:
To update the API without breaking existing client applications -> Option AQuick Check:
Schema evolution = safe API updates [OK]
- Thinking schema evolution means removing fields immediately
- Believing all fields must be mandatory
- Assuming no changes are allowed after deployment
Solution
Step 1: Recall GraphQL deprecation syntax
GraphQL uses the @deprecated directive with a reason argument to mark fields deprecated.Step 2: Check each option's syntax
type User { name: String @deprecated(reason: "Use fullName instead") } uses correct @deprecated directive syntax; others are invalid or incorrect.Final Answer:
type User { name: String @deprecated(reason: "Use fullName instead") } -> Option CQuick Check:
@deprecated directive syntax = type User { name: String @deprecated(reason: "Use fullName instead") } [OK]
- Using deprecated: true instead of @deprecated directive
- Using @remove directive which does not exist
- Omitting the @ symbol before deprecated
type Query {
user(id: ID!): User
}
type User {
id: ID!
name: String
email: String @deprecated(reason: "Use contactEmail instead")
contactEmail: String
}What happens if a client queries for
email field?Solution
Step 1: Understand @deprecated behavior in GraphQL
Deprecated fields still exist and return data but signal clients to avoid using them.Step 2: Analyze the query effect
Queryingemailreturns its value but clients should see a deprecation warning.Final Answer:
The query succeeds but clients get a deprecation warning foremail-> Option AQuick Check:
Deprecated fields return data with warnings [OK]
- Assuming deprecated fields are removed immediately
- Thinking deprecated fields return null
- Believing deprecated fields auto-redirect to new fields
type User {
id: ID!
name: String
email: String
}
# Update:
type User {
id: ID!
name: String
contactEmail: String
}What is the main problem with this update?
Solution
Step 1: Identify schema evolution best practice
Removing fields immediately breaks clients that still query those fields.Step 2: Analyze the update
The update removesemailwithout deprecation, causing breaking changes.Final Answer:
Removingemailfield breaks existing clients still using it -> Option DQuick Check:
Immediate removal breaks clients [OK]
- Thinking adding fields causes syntax errors
- Believing renaming must be one-step without deprecation
- Assuming GraphQL forbids adding new fields
phone with mobilePhone without breaking clients. Which strategy is best?Solution
Step 1: Apply schema evolution best practice
Adding new fields as optional and deprecating old ones avoids breaking clients.Step 2: Evaluate options
AddmobilePhoneas optional, deprecatephonewith reason, keep both for now follows best practice; others cause breaking changes or no evolution.Final Answer:
AddmobilePhoneas optional, deprecatephonewith reason, keep both for now -> Option BQuick Check:
Deprecate old, add new optional field [OK]
- Removing old field immediately
- Renaming fields without deprecation
- Not adding new field at all
