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

Ergonomic improvements for field policy merge and keyArgs functions. #6714

Merged
merged 5 commits into from
Jul 27, 2020

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Jul 27, 2020

This PR introduces two minor improvements for merge and keyArgs functions in field policies.

First, if you need to write a custom keyArgs function (unusual), you can now access the variables of the current read/write operation through context.variables.

Second, you can now write

new InMemoryCache({
  typePolicies: {
    Book: {
      fields: {
        author: {
          merge: true,
        },
      },
    },
  },
})

as a shorthand for

new InMemoryCache({
  typePolicies: {
    Book: {
      fields: {
        author: {
          merge(existing, incoming, { mergeObjects }) {
            return mergeObjects(existing, incoming);
          },
        },
      },
    },
  },
})

to enable merging of author objects associated with a given Book.

You can also specify merge: false to do the opposite:

new InMemoryCache({
  typePolicies: {
    CourtCase: {
      fields: {
        verdict: {
          merge: false,
        },
      },
    },
  },
})

This ensures the verdict field of the CourtCase object always overwrites any existing verdict, without warning about clobbering data (see #6372). In other words, merge: false is a shorthand for

new InMemoryCache({
  typePolicies: {
    CourtCase: {
      fields: {
        verdict: {
          merge(_, incoming) {
            return incoming;
          },
        },
      },
    },
  },
})

Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love these enhancements @benjamn - great ideas!

Copy link
Member

@glasser glasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like what we talked about! Nice!

src/__tests__/fetchMore.ts Outdated Show resolved Hide resolved
merge(existing: any[], incoming: any[], {
isReference,
}) {
const merged = existing ? existing.slice(0) : [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't usefully review this part of the test without a lot more learning.

@benjamn benjamn merged commit 9048596 into master Jul 27, 2020
@benjamn benjamn deleted the ergonomic-improvements-for-field-policies branch July 28, 2020 14:23
@justinanastos
Copy link
Contributor

This provides features I didn't even know I wanted; thank you @benjamn !

@benjamn
Copy link
Member Author

benjamn commented Jul 28, 2020

These improvements are available now in @apollo/client@3.1.0 (just published to npm).

@JesseZomer
Copy link

First, if you need to write a custom [`keyArgs`](https://deploy-preview-6714--apollo-client-docs.netlify.app/docs/react/caching/cache-field-behavior/#specifying-key-arguments) function (unusual), you can now access the variables of the current read/write operation through `context.variables`.

@benjamn Do you think it's possible to also get access to these variables when writing your custom keyargs function? I have some types that don't have unique enough data (think weeks in a calendar). But together with the context from the query they do.

A week number is not unique enough (if you have multiple calendars) but it is in combination with the calendar id. And the only way to get the weeks is through a calendar query.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants