Apollo Server persisted query cache invalidation on schema downgrade
0 reputation · 23 Mar 2022, 11:51 UTC
0 reputation · 23 Mar 2022, 11:51 UTC
The goal is to keep client responses consistent when a GraphQL schema is changed and later rolled back, with persisted queries and response caches in use.
Apollo Server documents cache control headers and persisted query support. It does not document automatic invalidation or rollback of cached query results when a breaking schema change is introduced and then reverted. Manual cache clearing is required and coordination across instances is a concern in distributed deployments. No schema-rollback-related configuration option is described in the configuration reference.
There is an unresolved decision about whether a configuration flag for automatic cache invalidation on schema downgrade should be exposed.
Does Apollo Server expose a configuration option to automatically invalidate persisted query caches on schema change or downgrade? What permission boundary governs cache invalidation across distributed instances? Is a documented schema versioning scheme recommended to maintain rollback safety?
34775 reputation · 23 Mar 2022, 16:45 UTC
Apollo Server does not expose a configuration option to automatically invalidate persisted query caches when a GraphQL schema is changed and later rolled back. Cache clearing must be performed manually or via a custom plugin.
cacheInvalidationOnSchemaChange exists in the Apollo Server configuration reference.redis-cli DEL "apollo:persistedQuery:*") or flush the appropriate database.Are you using an in‑memory cache or a shared store such as Redis/Memcached? Knowing this determines whether you need to restart instances or issue a store‑specific clear command.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 23 Mar 2022, 14:23 UTC
When you publish a schema to Apollo Studio each version receives a unique schemaId. Apollo Server’s ApolloServerPluginPersistedQueries can be configured to prepend this ID to the cache key, e.g.
const store = new RedisStore();
const persistedQueries = new ApolloServerPluginPersistedQueries({
store,
getCacheKey: ({ query, variables, context }) => {
const schemaId = context.schemaId; // injected by a custom plugin
return `${schemaId}:${hash(query)}`;
},
});
With this scheme, a rollback simply restores the older schemaId and you can target a purge like:
redisClient.keys('schemaId:*:persistedQuery:*').then(keys => redisClient.del(keys));
Because the cache key now includes the schema version, a shared store (Redis, Memcached) can be cleared per‑schema without touching other instances’ data. This approach keeps distributed deployments in sync and avoids the “cache stampede” risk of blanket flushes.