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.

Why is dataIdFromObject being deprecated?

See original GitHub issue

In the Apollo client v3 the dataIdFromObject is marked as deprecated, but still available for the backward compatibility.

image

In our GraphQL server, every entity could have one of the specific ID keys, eg id, guid, _id, whateverKey, etc. Previously we were using dataIdFromObject with the logic to select one of the known types of keys from the entity.

Example:

{
  "user": { "guid": "dasd-ed2-d", "name": "User1" },
  "comment": { "id": 123, "text": "My comment" },
}
  dataIdFromObject: (obj) => {
                const { __typename } = obj;
                const idPart = obj.id || obj.guid || obj._id || ....;

                if (!idPart) {
                    return false;
                }
                return `${__typename}:${idPart}`;
            },

So the cache keys would be User:dasd-ed2-d and Comment:123.

At the moment we are migrating to the V3, but since the function is marked as deprecated, eventually, we will have to replace it with the keyFields. And adding the keyFields literally to every type is error-prone and time-consuming.

So, my question - is there actually plans to remove it, and what could be the easy replacement for it in my use case?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
hwillsoncommented, Jun 10, 2022

👋 @bohdanbirdie - the short answer is that we aren’t deprecating dataIdFromObject any time soon.

The long answer is that we think type policies are a safer way to customize cache keys, as outlined in the last few bullet points of our Customizing identifier generation globally docs. The common dataIdFromObject approach is:

  • Sensitive to aliasing mistakes
  • Usually fails to protect against undefined object properties
  • Makes it easy to accidentally use different key fields at different times leading to cache inconsistencies

This being said, there are situations where dataIdFromObject can be easier to use (like yours), which is why we’ve explored the idea of allowing catch all / dynamic field policies like the policyForField option mentioned in https://github.com/apollographql/apollo-client/issues/6808#issuecomment-671388857. As we iterated on that approach, we realized similar functionality could be included in the type policy inheritance features we were working on, so we landed on https://github.com/apollographql/apollo-client/pull/7065. With type policy inheritance, you can create a client only super type that your other field policies inherit from, and define things like default key behaviour. If you have a lot of types though this means you’ll likely want to either generate your possibleTypes from your schema at build time, or consider using regular expressions to match subtype strings. You can find an example of this in https://github.com/apollographql/apollo-client/issues/8756#issuecomment-916182650.

1reaction
jpvajdacommented, Jun 13, 2022

I’m going to close this and link to https://github.com/apollographql/apollo-client/issues/9817

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring the Apollo Client cache - Apollo GraphQL Docs
dataIdFromObject. Function. A function that takes a response object and returns a unique identifier to be used when normalizing the data in the...
Read more >
Configuring the cache - Client (React) - Apollo GraphQL Docs
dataIdFromObject (deprecated), function. A function that takes a response object and returns a unique identifier to be used when normalizing the data in...
Read more >
graphql_flutter | Flutter Package - Pub.dev
client: hotfix dataIdFromObject passthrough in cache.readQuery (e3e04f8) ... graphql: keep deprecated QueryResult api and mark it as such (2b447a0) ...
Read more >
Elusive Bugs with GraphQL Object Caching in Apollo Client
Change the schema; Use dataIdFromObject option in the caching configuration; Alias the SKU to be id; Alias SKU as _id but still keep...
Read more >
Manual data normalization in Apollo Client v3 - Stack Overflow
The problem is dataIdFromObject is deprecated in Apollo Client v3 but I can't seem to find the appropriate replacement.
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