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

How to configure cache for Viewer Type in 3.0? #6663

Closed
JoshRobertson opened this issue Jul 21, 2020 · 5 comments
Closed

How to configure cache for Viewer Type in 3.0? #6663

JoshRobertson opened this issue Jul 21, 2020 · 5 comments

Comments

@JoshRobertson
Copy link

As of 3.0, the Viewer type in our schema throws the Cache data may be lost... warning in the console because it does not have an id field, And I'm just not sure the best way to address it. I'm trying to determine the ideal solution here (other than removing the viewer type entirely).

If I write a custom merge function on Viewer it would need need to be written for each field individually, which seems like a bad idea.

I tried writing a custom merge on Query but that didn't seem to work:

    new InMemoryCache({
      typePolicies: {
        Query: {
          fields: {
            viewer: {
              merge(existing, incoming) {
                return { ...existing, ...incoming };
              },
            },
          },
        },
      },
    });

If I utilize possibleTypes and map Viewer to the types of its fields (eg User Product AccountType), it doesn't seem to fix the issue. Do I need to remove references to the viewer in my queries to allow this to work?

The only other option I can think of is adding id to Viewer, but trying to avoid that if I can.

Any input is appreciated!

@benjamn
Copy link
Member

benjamn commented Jul 21, 2020

Is there ever more than one Viewer at a time in your application?

@JoshRobertson
Copy link
Author

@benjamn no, only one Viewer. Everything is a child of viewer, as a clunky way to handle blanket auth on all Nodes

@benjamn
Copy link
Member

benjamn commented Jul 21, 2020

Ok, since it sounds like Viewer is a singleton, you can give it a constant ID (without having to invent a real identity):

new InMemoryCache({
  typePolicies: {
    Viewer: {
      // This means the singleton Viewer's identity does not depend on any of its
      // fields (except for __typename), making the ID effectively constant.
      keyFields: [],
    },
  },
})

Alternatively, the following tweak to the merge function might make a difference, since it will do a better job of merging sub-fields that have their own merge functions:

merge(existing, incoming, { mergeObjects }) {
  return mergeObjects(existing, incoming);
}

I'll leave this issue open in case those solutions don't work, or you have any other questions.

@JoshRobertson
Copy link
Author

JoshRobertson commented Jul 22, 2020

Thanks @benjamn !

keyFields: [] worked perfectly.

interestingly keyFields: false does not.

@hwillson
Copy link
Member

hwillson commented May 4, 2021

It doesn't sound like there is an outstanding issue here, so closing. Thanks!

@hwillson hwillson closed this as completed May 4, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants