Bird
Raised Fist0
GraphQLquery~10 mins

Schema evolution strategies in GraphQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Schema evolution strategies
Start with initial schema
Add new fields/types?
YesAdd fields as optional
Clients ignore missing fields
Remove fields/types?
YesDeprecate fields first
Wait for clients to update
Change field types?
YesAdd new field with new type
Migrate clients to new field
Finalize removal
Update schema
Clients adapt
End
This flow shows how to safely evolve a GraphQL schema by adding optional fields, deprecating before removing, and migrating clients.
Execution Sample
GraphQL
type User {
  id: ID!
  name: String
  email: String @deprecated(reason: "Use contactEmail")
  contactEmail: String
}
This schema adds a new field contactEmail and deprecates email to evolve safely.
Execution Table
StepActionSchema ChangeClient ImpactResult
1Start with initial schemaUser with id, name, emailClients use emailSchema stable
2Add new fieldAdd contactEmail (optional)Clients can use email or contactEmailNo breaking change
3Deprecate old fieldMark email as deprecatedClients warned to switchClients start migrating
4Clients updateNo schema changeClients use contactEmailSafe transition
5Remove deprecated fieldRemove email fieldClients must use contactEmailBreaking change avoided
6EndUpdated schema with id, name, contactEmailClients fully adaptedSchema evolved safely
💡 Schema evolution completes after clients adapt and deprecated fields are removed safely.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
Schema fieldsid, name, emailid, name, email, contactEmailid, name, email(deprecated), contactEmailid, name, contactEmailid, name, contactEmail
Client usageemailemail or contactEmailemail (warned) or contactEmailcontactEmail onlycontactEmail only
Key Moments - 3 Insights
Why do we add new fields as optional instead of required?
Adding new fields as optional avoids breaking existing clients who don't expect the new field, as shown in execution_table step 2.
Why mark fields as deprecated before removing them?
Deprecation warns clients to migrate before removal, preventing sudden breakage, as seen in execution_table step 3.
What happens if clients don't update before a field is removed?
Clients will break because the field no longer exists; safe evolution requires clients to adapt first, as explained in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the new field added?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Check the 'Schema Change' column for when contactEmail is added.
According to variable_tracker, what is the client usage after step 3?
AUse email or contactEmail with warning
BUse only contactEmail
CUse only email
DNo usage allowed
💡 Hint
Look at the 'Client usage' row under 'After Step 3' column.
If we remove the deprecated field immediately without warning, what would happen?
AClients continue working fine
BSchema automatically updates clients
CClients break due to missing field
DNo impact on clients
💡 Hint
Refer to key_moments about removing fields before clients adapt.
Concept Snapshot
Schema evolution in GraphQL:
- Add new fields as optional to avoid breaks
- Deprecate fields before removal
- Clients migrate to new fields
- Remove deprecated fields after clients update
- Ensures safe, backward-compatible changes
Full Transcript
Schema evolution strategies in GraphQL involve carefully changing the schema without breaking clients. First, add new fields as optional so old clients still work. Then mark old fields as deprecated to warn clients to switch. After clients update to use new fields, remove deprecated fields. This step-by-step approach ensures smooth transitions and avoids breaking changes.

Practice

(1/5)
1. What is the main purpose of schema evolution in GraphQL APIs?
easy
A. To update the API without breaking existing client applications
B. To remove all old fields immediately from the schema
C. To prevent any changes to the API once deployed
D. To make all fields mandatory for clients

Solution

  1. Step 1: Understand schema evolution concept

    Schema evolution allows changes to the API while keeping existing clients working.
  2. Step 2: Identify the correct purpose

    Removing fields immediately or making all fields mandatory breaks clients, so those are incorrect.
  3. Final Answer:

    To update the API without breaking existing client applications -> Option A
  4. Quick Check:

    Schema evolution = safe API updates [OK]
Hint: Schema evolution means safe API changes without breaking clients [OK]
Common Mistakes:
  • Thinking schema evolution means removing fields immediately
  • Believing all fields must be mandatory
  • Assuming no changes are allowed after deployment
2. Which of the following is the correct way to mark a field as deprecated in a GraphQL schema?
easy
A. type User { name: String @remove(reason: "Use fullName instead") }
B. type User { name: String deprecated: true }
C. type User { name: String @deprecated(reason: "Use fullName instead") }
D. type User { name: String deprecated(reason: "Use fullName instead") }

Solution

  1. Step 1: Recall GraphQL deprecation syntax

    GraphQL uses the @deprecated directive with a reason argument to mark fields deprecated.
  2. 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.
  3. Final Answer:

    type User { name: String @deprecated(reason: "Use fullName instead") } -> Option C
  4. Quick Check:

    @deprecated directive syntax = type User { name: String @deprecated(reason: "Use fullName instead") } [OK]
Hint: Use @deprecated(reason: "...") to mark fields deprecated [OK]
Common Mistakes:
  • Using deprecated: true instead of @deprecated directive
  • Using @remove directive which does not exist
  • Omitting the @ symbol before deprecated
3. Given this GraphQL schema snippet:
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?
medium
A. The query succeeds but clients get a deprecation warning for email
B. The query fails because email is removed
C. The query returns null for email always
D. The query returns contactEmail value instead of email

Solution

  1. Step 1: Understand @deprecated behavior in GraphQL

    Deprecated fields still exist and return data but signal clients to avoid using them.
  2. Step 2: Analyze the query effect

    Querying email returns its value but clients should see a deprecation warning.
  3. Final Answer:

    The query succeeds but clients get a deprecation warning for email -> Option A
  4. Quick Check:

    Deprecated fields return data with warnings [OK]
Hint: Deprecated fields still return data but warn clients [OK]
Common Mistakes:
  • Assuming deprecated fields are removed immediately
  • Thinking deprecated fields return null
  • Believing deprecated fields auto-redirect to new fields
4. Consider this schema update attempt:
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?
medium
A. GraphQL does not allow adding new fields to existing types
B. Adding contactEmail without deprecating email causes syntax error
C. You must rename email to contactEmail in one step
D. Removing email field breaks existing clients still using it

Solution

  1. Step 1: Identify schema evolution best practice

    Removing fields immediately breaks clients that still query those fields.
  2. Step 2: Analyze the update

    The update removes email without deprecation, causing breaking changes.
  3. Final Answer:

    Removing email field breaks existing clients still using it -> Option D
  4. Quick Check:

    Immediate removal breaks clients [OK]
Hint: Never remove fields immediately; deprecate first [OK]
Common Mistakes:
  • Thinking adding fields causes syntax errors
  • Believing renaming must be one-step without deprecation
  • Assuming GraphQL forbids adding new fields
5. You want to evolve a GraphQL schema by replacing a field phone with mobilePhone without breaking clients. Which strategy is best?
hard
A. Remove phone immediately and add mobilePhone
B. Add mobilePhone as optional, deprecate phone with reason, keep both for now
C. Rename phone to mobilePhone directly without deprecation
D. Keep only phone and do not add mobilePhone

Solution

  1. Step 1: Apply schema evolution best practice

    Adding new fields as optional and deprecating old ones avoids breaking clients.
  2. Step 2: Evaluate options

    Add mobilePhone as optional, deprecate phone with reason, keep both for now follows best practice; others cause breaking changes or no evolution.
  3. Final Answer:

    Add mobilePhone as optional, deprecate phone with reason, keep both for now -> Option B
  4. Quick Check:

    Deprecate old, add new optional field [OK]
Hint: Add new optional field, deprecate old with reason [OK]
Common Mistakes:
  • Removing old field immediately
  • Renaming fields without deprecation
  • Not adding new field at all