简体   繁体   中英

How do you pass a react component's props down to options after apollo-client mutation?

How do you pass a react component's props down to options after apollo-client mutation?

I am using react with apollo-client . In a component I am trying to run a delete mutation after which I want to remove the item from the local store without doing a refetchQueries . In order to do so I've been using the options.update command.

In order to update the store, I need the parent ID of the object I'm trying to delete. It's available in the react component, I just need to find a way to pass it down to the options.update function.

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)

Here's what I pass in to the options from FundDocumentQLOptions as you can see I get the fundId from localStorage which is kind of hacky. I'd rather try and pass it down properly.

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);
    }
  }
};

I saw this example in the apollo-client docs: 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);

And I saw this answer: Apollo can't access queryVariables in update: after a mutation

Reading the other answer here Apollo can't access queryVariables in update: after a mutation

I realized I could pass fundId from this.props into the update function when I created the options object ahead of the mutation.

const { fundId } = this.props;    
const variables = { documentId: document.id };
const options = { 
  variables: variables,
  update:  (proxy, { data: { deleteFundDocument } }) => FundDocumentQLOptions.deleteFundDocument(proxy, deleteFundDocument, fundId)}
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'})(DocumentDisplay)

From FundDocumentQLOptions

const deleteFundDocument = (proxy, deleteFundDocument, fundId) => {
  try {
    let data = proxy.readQuery({query: FundDocumentQL.allFundDocuments, variables: {fundId: fundId}});
    // console.log('data.allFundDocuments 1', data.allFundDocuments);
    data.allFundDocuments = data.allFundDocuments.filter( item => {
      return item.id !== deleteFundDocument.id;
    });
    // console.log('data.allFundDocuments 2', data.allFundDocuments);
    proxy.writeQuery({query: FundDocumentQL.allFundDocuments, data, variables: {fundId: fundId}});
  } catch (e) {
    console.log(e);
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM