Implementing Schema Versioning
1. Understanding API Versioning
| Strategy | GraphQL Equivalent |
|---|---|
| URL versioning (REST) | Avoided — single endpoint |
| Schema evolution | Add fields, deprecate old |
| Per-field deprecation | @deprecated reason |
| Namespace types | Optional v2_User for major rewrites |
2. Using Field Deprecation
Example: Deprecating with reason
type User {
fullName: String! @deprecated(reason: "Use firstName + lastName since 2026-01")
firstName: String!
lastName: String!
}
3. Implementing Graceful Deprecation
Deprecation Lifecycle
- Add new field/type alongside old
- Mark old as
@deprecatedwith migration hint - Track usage via Apollo Studio / operation logs
- Communicate timeline to clients
- Wait for usage to drop near zero
- Remove old field in next major release
4. Adding New Fields
| Rule | Detail |
|---|---|
| Always safe | Existing clients ignore unknown fields |
| Default nullable | Avoid forcing existing data to provide value |
| Document | Include description string |
5. Using Optional Arguments
| Type of Change | Safety |
|---|---|
| Add nullable arg | Safe |
| Add required arg without default | Breaking |
| Add required arg with default | Safe |
| Remove arg | Breaking |
6. Implementing Breaking Changes
| Change | Migration |
|---|---|
| Type change | Add new field with new type, deprecate old |
| Required arg added | Use new field name to opt in |
| Field removal | Long deprecation window |
| Enum value rename | Add new value, deprecate old, accept both |
7. Versioning with Namespaces
| Use | Detail |
|---|---|
| Major rewrites | Coexist with v1 fields |
| Internal vs public | internal { ... } namespace |
8. Using Schema Directives
| Directive | Use |
|---|---|
| @deprecated | Standard deprecation |
| @inaccessible | Hide from federation supergraph |
| @tag | Annotate for governance/contracts |
| @since(version) | Custom: indicate when added |
9. Communicating Changes
| Channel | Detail |
|---|---|
| Schema changelog | Auto-generate via graphql-inspector |
| Schema diff in PR | CI annotation |
| Client notifications | Email registered API consumers |
| Status page | Document deprecation timeline |
10. Migrating Clients
| Step | Tool |
|---|---|
| Detect deprecated usage | graphql-eslint no-deprecated |
| Auto-migrate | Codemods |
| Validate | graphql-codegen against new schema |
| Deploy | Update client before server removes |