Using the simple "Movies API" example from the documentation. I added a ttl
to the getMovie
function, so that the result is cached for 10 minutes. How can I invalidate the cache in the updateMovie
function?
const { RESTDataSource } = require('apollo-datasource-rest');
class MoviesAPI extends RESTDataSource {
async getMovie(id) {
return this.get(`movies/${id}`, {}, { cacheOptions: { ttl: 600 } });
}
async updateMovie(id, data) {
const movie = await this.put(`movies/${id}`, data);
// invalidate cache here?!
return movie;
}
}
I know that the KeyValueCache
interface that is passed to ApolloServer provides a delete
function. However, this object doesn't seem to be exposed in data sources. It's wrapped inside HTTPCache
, which only exposes a fetch
function. The KeyValueCache
is also wrapped inside a PrefixingKeyValueCache
, so it seems almost impossible to invelidate something in the cache, without some nasty hacks, assuming the internal implementation of RESTDataSource
.