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.

addTypePolicies does not work as expected for local fields

See original GitHub issue

Intended outcome:

Type Policies for local state fields added through addTypePolicies at render/mount of component should allow for querying of those fields from that point on.

Actual outcome:

Type Policies for local state fields added through addTypePolicies at render/mount of component return undefined data and no errors. loading state indicates that the query executed.

Note: I dug into the addTypePolicies call and for both scenarios it seems like the type is added. The image shows the same resulting state for adding policy via initialization or adding policy via component calling addTypePolicies.

How to reproduce the issue:

See sandbox: https://codesandbox.io/s/local-query-typepolicies-bug-s2ssq?file=/src/index.js

Instructions within, but at a high level - it seems to be the difference between applying type policies to the cache at creation vs applying type policies at a later time. I have tried to narrow down the sandbox as much as I could.

// This works and `message` is queryable.
const cache = new InMemoryCache({
    typePolicies: {
        Query: {
            fields: {
                message: {
                    read() {
                        return 'Hello World';
                    }
                }
            }
        }
    }
});

vs

// This doesn't work and `message` returns undefined.
const MyComponent = () => {
  const apolloClient = useApolloClient();
  useEffect(() => {
    apolloClient.cache.policies.addTypePolicies({
        Query: {
            fields: {
                message: {
                    read() {
                        return 'Hello World';
                    }
                }
            }
        }
    });
  }, [apolloClient, typePolicies]);
}

Versions Image from Gyazo

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
benjamncommented, Mar 25, 2021

It’s not currently guaranteed to fail, but I’m in favor of making addTypePolicies fail in that case.

1reaction
coler-jcommented, Mar 25, 2021

Thanks @sirugh I just found this PR that adds docs, but it is out of date. https://github.com/apollographql/apollo-client/pull/6766

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypePolicies: mutations are not propagated when local field ...
TypePolicies: mutations are not propagated when local field typePolicy is present without a local resolver #6247. Open. ambientlight opened this ...
Read more >
Customizing the behavior of cached fields - Apollo GraphQL
You can customize how a particular field in your Apollo Client cache is read and written. To do so, you define a field...
Read more >
@apollo/client | Yarn - Package Manager
Apollo Client 3 enforces that @client fields are local only, meaning they are no longer passed into the link chain, under any circumstances....
Read more >
GraphQL gql syntax error: <selection> expected, got '}' and ...
I thought this error is occurring because of any closed curly brackets or when some fields are not properly defined while calling the...
Read more >
Defining type policies during runtime in Apollo
Apollo InMemoryCache type policies are used to define field policies for local state, to customize identifier generation by type or 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