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

Why is dataIdFromObject being deprecated? #9799

Closed
bohdanbirdie opened this issue Jun 8, 2022 · 5 comments
Closed

Why is dataIdFromObject being deprecated? #9799

bohdanbirdie opened this issue Jun 8, 2022 · 5 comments

Comments

@bohdanbirdie
Copy link

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?

@jpvajda jpvajda added ⁉️ question 🏓 awaiting-team-response requires input from the apollo team labels Jun 9, 2022
@jpvajda jpvajda self-assigned this Jun 9, 2022
@jpvajda jpvajda removed the 🏓 awaiting-team-response requires input from the apollo team label Jun 9, 2022
@jpvajda
Copy link
Contributor

jpvajda commented Jun 9, 2022

@bohdanbirdie This is a great question, so let me confirm with the team what the plan is to fully deprecate that API so we can give you clear direction on your migration to V3. cc @hwillson @benjamn

@jpvajda jpvajda added the 🏓 awaiting-team-response requires input from the apollo team label Jun 9, 2022
@bohdanbirdie
Copy link
Author

Thanks, @jpvajda
Looking forward to it 😉

@hwillson
Copy link
Member

👋 @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 #6808 (comment). 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 #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 #8756 (comment).

@jpvajda jpvajda added 🏓 awaiting-contributor-response requires input from a contributor and removed 🏓 awaiting-team-response requires input from the apollo team labels Jun 10, 2022
@bohdanbirdie
Copy link
Author

@hwillson thanks for the clear answer!

@jpvajda
Copy link
Contributor

jpvajda commented Jun 13, 2022

I'm going to close this and link to #9817

@jpvajda jpvajda closed this as completed Jun 13, 2022
@jpvajda jpvajda removed the 🏓 awaiting-contributor-response requires input from a contributor label Jun 13, 2022
@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.
Projects
None yet
Development

No branches or pull requests

3 participants