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

Fuzzy type policies don't support type-level merge functions #11480

Closed
amannn opened this issue Jan 12, 2024 · 4 comments
Closed

Fuzzy type policies don't support type-level merge functions #11480

amannn opened this issue Jan 12, 2024 · 4 comments
Labels
🏓 awaiting-team-response requires input from the apollo team ⁉️ question

Comments

@amannn
Copy link
Contributor

amannn commented Jan 12, 2024

Issue Description

I tried out fuzzy type policies in an app of mine and noticed that type-level merge functions don't work correctly. That means, at least an error is printed, the result looks ok (however it also does when I don't specify any type policies).

Link to Reproduction

https://codesandbox.io/p/devbox/apollo-client-cache-merge-svk8yx?file=%2Fsrc%2Findex.jsx%3A7%2C1

Reproduction Steps

  1. Go to reproduction
  2. See console output (an error is printed)

See the cache configuration in index.jsx: Depending on if fuzzy type matching is used or not, the merge function will be applied.


Related: #10639

@amannn
Copy link
Contributor Author

amannn commented Jan 12, 2024

Oh, I just noticed #6901 mentions possibleTypes while my reproduction uses typePolicies. I came here after this tweet by @phryneas. Can type policies also be fuzzy?

@bignimbus bignimbus added ⁉️ question 🏓 awaiting-team-response requires input from the apollo team labels Jan 12, 2024
@bignimbus
Copy link
Contributor

Hi @amannn 👋🏻 thanks for opening this issue! This area of the library is not something the current team has dealt with a lot so give us some time to get some recommendations together for you. @benjamn did recently write this very helpful spec though, which might help move things forward for you:

it("typePolicies can be inherited from supertypes with fuzzy possibleTypes", () => {
const cache = new InMemoryCache({
possibleTypes: {
EntitySupertype: [".*Entity"],
},
typePolicies: {
Query: {
fields: {
coworkers: {
merge(existing, incoming) {
return existing ? existing.concat(incoming) : incoming;
},
},
},
},
// The point of this test is to ensure keyFields: ["uid"] can be
// registered for all __typename strings matching the RegExp /.*Entity/,
// without manually enumerating all of them.
EntitySupertype: {
keyFields: ["uid"],
},
},
});

@amannn
Copy link
Contributor Author

amannn commented Jan 12, 2024

Oh right, thank you so much!

I can confirm that this fixes my reproduction:

  new InMemoryCache({
    typePolicies: {
      Page: {
        keyFields: false,
        merge: true,
      },
    },
    possibleTypes: {
      Page: ["^.*Page$"],
    },
  }),

Working example with Page interface.

Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🏓 awaiting-team-response requires input from the apollo team ⁉️ question
Projects
None yet
Development

No branches or pull requests

2 participants