javascript - How to invalidate Relay's cache after mutation -
i'm rebuilding small, internal web app react/relay/graphql familiar stack. basically, monitors analytics of list of "active" videos. mutation replace list of active video ids new list. the problem after replacing ids, relay continues deliver old list of ids instead of new.
i haven't been able figure out how manipulate store that's passed commitmutation()
's updater
, optimisticupdater
callbacks. need either clear out stored list of active videos knows call fresh 1 or have re-run graphql query refresh cache.
specifically, need clear results query:
const activevideosquery = graphql` query app_activevideos_query { activevideos { ...setactivevideospage_activevideos ...videolist_activevideos } } `
the mutation (typescript):
const { commitmutation, graphql } = require('react-relay') const mutation = graphql` mutation setactivevideosmutation($input: setactivevideosinput!) { setactivevideos(input: $input) { clientmutationid } } ` let nextclientmutationid = 0 function commit(environment, ids: string[]) { const clientmutationid = nextclientmutationid++ return commitmutation(environment, { mutation, variables: { input: { ids, clientmutationid } }, }) } export default { commit }
and schema:
type channel { id: id! name: string! } type mutation { setactivevideos(input: setactivevideosinput!): setactivevideospayload } type query { activevideos: [video]! } input setactivevideosinput { ids: [id]! clientmutationid: string! } type setactivevideospayload { clientmutationid: string! } type video { id: id! active: boolean! details: videodetails statsbyage(seconds: int!): [videostats]! } type videodetails { title: string! description: string! thumbnailurl: string! publishedat: string! channel: channel! } type videostats { videoid: id! recordedat: string! views: string! likes: string! dislikes: string! favorites: string! comments: string! }
you're going want use the relay "environment".
the relay "environment" bundles configuration, cache storage, , network-handling relay needs in order operate.
question: why isn't there command relay.reset() wipes app?
answer: because it's cleaner instantiate new relay.environment() instead of trying make sure you've cleaned on global singleton relay.store.
edit: in response comment;
relay offers coarse-grained control of when data refetched: primecache defaults fulfilling queries using in-memory data, while forcefetch bypasses cache , refetches data in-full server. again, in practice has worked of our use cases.
refetching & cache eviction control view issue
regarding cache eviction, it's import understand relay fundamentally different many typical caches. whereas typical caches store independent key/value pairs, relay caches graph of interconnected objects. cover ramifications of extensively in thinking in graphql. in practice, means simple approaches cache eviction such ttl or lru may have unintuitive consequences. example, products typically care queries while cache stores normalized records. if single record evicted cache, cause entire query "missing" , need refetched. further, data dependencies of discrete parts of application may overlap, such disagree allowable staleness of data.
Comments
Post a Comment