Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cacheObject with fields using customized cache IDs not updating #11

Closed
jsindos opened this issue Oct 17, 2021 · 4 comments
Closed

cacheObject with fields using customized cache IDs not updating #11

jsindos opened this issue Oct 17, 2021 · 4 comments

Comments

@jsindos
Copy link

jsindos commented Oct 17, 2021

It seems like apollo-augmented-hooks/useMutation isn't able to update a cacheObject with fields who have customized cache IDs. In this case, ProductInBag has a custom cache ID derived from selectedOption.id, it's not removed from bag when using includeIf(false).

type Bag {
    id: Int
    products: [ProductInBag]
}
...
new InMemoryCache({
    typePolicies: {
      ProductInBag: {
        keyFields: ['selectedOption', ['id']]
      }
  }
})
...
const DELETE_PRODUCT_FROM_BAG = gql`
  mutation DeleteProductFromBag($selectedOptionId: Int!) {
    deleteProductFromBag(selectedOptionId: $selectedOptionId) {
      selectedOption {
        id
      }
    }
  }
`
...
const deleteProductFromBagMutation = useMutation(DELETE_PRODUCT_FROM_BAG)

const handleOnPressDelete = async (selectedOption) => {
  await deleteProductFromBagMutation({
    variables: {
      selectedOptionId: selectedOption.id
    },
    modifiers: [{
      cacheObject: bag,
      fields: {
        products: ({ includeIf }) => includeIf(false) // this is not removing the deleted ProductInBag, even though it is returned from mutation
      }
    }]
  })
}
@jsindos
Copy link
Author

jsindos commented Oct 17, 2021

The following from Apollo works

const handleOnPressDelete = async (selectedOption) => {
  await deleteProductFromBagMutation({
    variables: {
      selectedOptionId: selectedOption.id
    },
    update: (cache, mutationResult) => {
      cache.modify({
        id: `${bag.__typename}:${bag.id}`,
        fields: {
          products: (previous, { readField }) =>
            previous.filter(
              productInBagRef => selectedOption.id !== readField('selectedOption', productInBagRef).id
            )
        }
      })
    }
  })
}

I assume the fix would involve detecting when a ref has a customised cache ID, and using

ref => idToRemove !== readField('customisedCacheId', ref).anyNestedCustomisedCacheId

instead of

ref => idToRemove !== readField('id', ref)

as the filter function.

@mindnektar
Copy link
Owner

mindnektar commented Oct 18, 2021

Hey there, thanks for the heads up! apollo-augmented-hooks already works with custom keys (I'm using them extensively in my projects as well), but what might not work is nested keys (['selectedOption', ['id']]). I don't remember having used those before. I will check that out and report back later today.

@mindnektar
Copy link
Owner

@jsindos I've fixed the issue in v2.1.1 - please see if it works for you. :)

@jsindos
Copy link
Author

jsindos commented Oct 19, 2021

@mindnektar that works, thanks for fixing it so quickly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants