问题描述
我需要在不使用突变的情况下通过ID删除本地商店的"记录",因为服务器不支持突变.
我试图手动访问这样的商店:
delete this.apolloClient.store.getState().apollo.data['1112']
这会删除记录,但是当我要求阿波罗将其输入服务器的项目时,就像没有缓存一样.
顺便说一句,如果不是删除,我只是更新这样的原始属性:
this.apolloClient.store.getState().apollo.data['1112'].name = 'XXX'
一切正常,数据已更新,并且Apollo继续使用缓存
我知道我应该使用突变,但不能.
我需要仅更新本地
推荐答案
我找到了2个解决方案:
查询:
stores { products { totalCount list { } } }
**如果您知道确切的变量:**
const query = gql(query); const data = apolloClient.readQuery({ query, variables:{limit:100, offset: 0} }); let removedProduct = _.remove(data.stores.products.list, function(product: IProduct) { return product.id === productId; }) data.stores.products.totalCount = data.stores.products.list.length; apolloClient.writeQuery({ query, variables:{limit:100, offset: 0}, data: data });
如果您想删除到处
let dataStore = apolloClient.store.getState().apollo.data; let productsStore = dataStore[dataStore['ROOT_QUERY'].stores.id]; let product = dataStore[productId]; //remove product from cache index Object.keys(productsStore) .map((key: any) => dataStore[productsStore[key].id]) .forEach((products: any) => { _.remove(products.list, (product) => product.id === productId); products.totalCount = products.list.length; }); //remove product's complex fields (array/objects) from cache Object.keys(product).map((key: any) => { if (Array.isArray(product[key])) { return product[key].map((item) => item.id); } else if (typeof product[key] === 'object') { return product[key].id; } return null; }).forEach((productField) => { if (Array.isArray(productField)) { productField.forEach((key) => delete dataStore[key]); } else if (productField) { delete dataStore[productField]; } }); //remove product from cache delete dataStore[productId];
问题描述
I need to remove a "record" from the local store by id without using mutation because the server does not support mutations.
I have tried to access manually the store like that:
delete this.apolloClient.store.getState().apollo.data['1112']
this removes the record but when I ask apollo to fetch the items it goes to the server, like there is no cache.
by the way, if instead of removing I just update one of the primitive properties like that :
this.apolloClient.store.getState().apollo.data['1112'].name = 'XXX'
then everything is OK, the data is updated and apollo keep using the cache
I understand that I am supposed to use mutations but I can not.
I need to update only localy
推荐答案
I have found 2 solutions:
for a query of:
stores { products { totalCount list { } } }
**if you know the exact variables: **
const query = gql(query); const data = apolloClient.readQuery({ query, variables:{limit:100, offset: 0} }); let removedProduct = _.remove(data.stores.products.list, function(product: IProduct) { return product.id === productId; }) data.stores.products.totalCount = data.stores.products.list.length; apolloClient.writeQuery({ query, variables:{limit:100, offset: 0}, data: data });
if you want to remove everywhere
let dataStore = apolloClient.store.getState().apollo.data; let productsStore = dataStore[dataStore['ROOT_QUERY'].stores.id]; let product = dataStore[productId]; //remove product from cache index Object.keys(productsStore) .map((key: any) => dataStore[productsStore[key].id]) .forEach((products: any) => { _.remove(products.list, (product) => product.id === productId); products.totalCount = products.list.length; }); //remove product's complex fields (array/objects) from cache Object.keys(product).map((key: any) => { if (Array.isArray(product[key])) { return product[key].map((item) => item.id); } else if (typeof product[key] === 'object') { return product[key].id; } return null; }).forEach((productField) => { if (Array.isArray(productField)) { productField.forEach((key) => delete dataStore[key]); } else if (productField) { delete dataStore[productField]; } }); //remove product from cache delete dataStore[productId];