question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApolloClient3: TypePolicy merge readField function always requires a foreignObjOrRef

See original GitHub issue

https://spectrum.chat/apollo/apollo-client/apolloclient3-typepolicies-contexts~0ed2649c-ad4c-4e8a-9a53-9ae52f865e7b

Intended outcome: Be able to use readField in merge functions just like when using it in a read function

Actual outcome: When trying to use it without foreignObjOrRef parameter, the function will always throw

- Network error: Must provide an object or Reference when calling Policies#readField

How to reproduce the issue: Just use the readField function in a merge TypePolicy field

Versions ^3.0.0-beta.43

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bennypowerscommented, Sep 12, 2020

What’s the blessed path for persisting fields in some storage scheme, then?

consider

export const Analysis: TypePolicy = {
  fields: {
    sectionsOpen: {
      merge(prev: SectionsOpen, next: SectionsOpen, { readField }: FieldFunctionOptions): SectionsOpen {
        const id = readField('id') // womp womp, `undefined`
        localStorage.setItem(`sections-open-${id}`, JSON.stringify(next ?? null));
        return next;
      },
      read(prev: SectionsOpen, { readField }: FieldFunctionOptions): SectionsOpen {
        const id = readField('id'); // parent id! yay!
        return (
          prev ??
          JSON.parse(localStorage.getItem(`sections-open-${id}`)) ??
          { /* ...some defaults */ }
        );
      },
    },
  },
};
1reaction
ashubhamcommented, May 8, 2020

@Banou26 You may want to look at this comment in the code though:

https://github.com/apollographql/apollo-client/blob/master/src/cache/inmemory/policies.ts#L549

// Unlike options.readField for read functions, we do not fall
        // back to the current object if no foreignObjOrRef is provided,
        // because it's not clear what the current object should be for
        // merge functions: the (possibly undefined) existing object, or
        // the incoming object? If you think your merge function needs
        // to read sibling fields in order to produce a new value for
        // the current field, you might want to rethink your strategy,
        // because that's a recipe for making merge behavior sensitive
        // to the order in which fields are written into the cache.
        // However, readField(name, ref) is useful for merge functions
        // that need to deduplicate child objects and references.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing the behavior of cached fields - Apollo GraphQL
A merge function that specifies what happens when field's cached value is written; An array of key arguments that help the cache avoid...
Read more >
Customizing the behavior of cached fields - Client (React)
A merge function that specifies what happens when field's cached value is written; An array of key arguments that help the cache avoid...
Read more >
Type Policies in Apollo Client 3: An Introduction - YouTube
What are type policies in Apollo Client 3 and how can we leverage them in our applications? This video explores some of the...
Read more >
Apollo GraphQL: use case for merge function in type policy?
If you have mutations that add/remove a single item and others that overwrite the entire set, then you need some merge magic to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found