之后,如何将react组件的道具传递给选项
apollo-client
突变
我正在使用
react
具有
阿波罗客户
. 在一个组件中,我试图运行一个删除变异,之后我想从本地存储中删除该项,而不需要执行
refetchQueries
. 为了做到这一点,我一直在使用
options.update
命令
为了更新存储,我需要尝试删除的对象的父ID。它在react组件中可用,我只需要找到一种方法将其传递给
选项。使现代化
作用
const { fundId } = this.props;
const variables = { documentId: document.id };
const options = { variables }
this.props.deleteFundDocument(options)
.then( response => console.log("Document successfully deleted", response) )
.catch( e => console.log("Document not deleted", e) )
export default graphql(FundDocumentQL.deleteFundDocument, {name: 'deleteFundDocument', options: FundDocumentQLOptions.deleteFundDocument})
)(DocumentDisplay)
下面是我从FundDocumentQLPOptions中传递给选项的内容,您可以看到我从localStorage中获取fundId,这有点像黑客。我宁愿试着把它传下去。
const deleteFundDocument = {
update: (proxy, {data: {deleteFundDocument}}) => {
try {
if (localStorage.getItem('documentViewerFundId')) {
const fundId = localStorage.getItem('documentViewerFundId');
let data = proxy.readQuery({query: FundDocumentQL.allFundDocuments, variables: {fundId: fundId}});
console.log('data.allFundDocuments 1', data.allFundDocuments);
// console.log('documentId', documentId);
console.log('variables.documentId', variables.documentId);
const newDocuments = data.allFundDocuments.filter( item => {
return item.id !== deleteFundDocument.id;
});
console.log('newDocuments', newDocuments);
data.allFundDocuments = [...newDocuments];
console.log('data.allFundDocuments 2', data.allFundDocuments);
proxy.writeQuery({query: FundDocumentQL.allFundDocuments, data, variables: {fundId: fundId}});
}
} catch (e) {
console.log(e);
}
}
};
我在apollo客户端文档中看到了这个示例:
https://www.apollographql.com/docs/react/basics/mutations.html#graphql-mutation-options-variables
export default graphql(gql`
mutation ($foo: String!, $bar: String!) {
...
}
`, {
options: (props) => ({
variables: {
foo: props.foo,
bar: props.bar,
},
}),
})(MyComponent);
我看到了这个答案:
Apollo can't access queryVariables in update: after a mutation