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

Simplify API for nested cache.modify calls #266

Open
lorensr opened this issue Oct 16, 2020 · 1 comment
Open

Simplify API for nested cache.modify calls #266

lorensr opened this issue Oct 16, 2020 · 1 comment
Labels
📕 cache Feature requests related to the cache

Comments

@lorensr
Copy link

lorensr commented Oct 16, 2020

Currently, modifying a non-root, non-normalized field requires nesting calls to cache.modify(), like the below code modifying ROOT_QUERY.currentUser.favoriteReviews:

const [removeReview] = useMutation(REMOVE_REVIEW_MUTATION, {
  update: (cache) => {
    cache.modify({
      fields: {
        currentUser(currentUserRef) {
          cache.modify({
            id: currentUserRef.__ref,
            fields: {
              favoriteReviews(reviews, { readField }) {
                return reviews.filter(review => readField('id', review) !== id)
              },
            },
          })
          // Keep Query.currentUser unchanged.
          return currentUserRef
        },
      },
    })
  },
})

It would be nice if there were a simpler API, for example allowing dot notation in fields:

const [removeReview] = useMutation(REMOVE_REVIEW_MUTATION, {
  update: (cache) => {
    cache.modify({
      fields: {
        'currentUser.favoriteReviews': (reviews, { readField }) =>
          reviews.filter((review) => readField('id', review) !== id),
      },
    })
  },
})
@benjamn
Copy link
Member

benjamn commented Nov 11, 2020

@lorensr I like this idea, but I think I would prefer allowing nested fields, which should be unambiguous because objects and functions are easily distinguishable:

const [removeReview] = useMutation(REMOVE_REVIEW_MUTATION, {
  update(cache) {
    cache.modify({
      fields: {
        currentUser: {
          favoriteReviews: (reviews, { readField }) =>
            reviews.filter((review) => readField('id', review) !== id),
        },
      },
    })
  },
})

@jpvajda jpvajda added the project-apollo-client (legacy) LEGACY TAG DO NOT USE label Jun 1, 2022
@jerelmiller jerelmiller added the 📕 cache Feature requests related to the cache label Apr 6, 2023
@jerelmiller jerelmiller removed the project-apollo-client (legacy) LEGACY TAG DO NOT USE label Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📕 cache Feature requests related to the cache
Projects
None yet
Development

No branches or pull requests

4 participants