Avoid letting defaultDataIdFromObject
normalize objects with nullish ids
#9862
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Should fix issue #9814, by not allowing objects with
id: null
orid: void 0
fields to be normalized.This change mostly affects applications that have not configured
keyFields
or a customdataIdFromObject
function when creating theInMemoryCache
, though I suppose the same confusion could happen whenkeyFields: ["id"]
is explicitly configured and theobject.id
happens to benull
or undefined.I'm borrowing the "nullish" terminology introduced by the Nullish Coalescing Operator
??
, which treats onlynull
and undefined as falsy (not""
,0
, orfalse
).This distinction allows objects with
id: ""
andid: 0
to be normalized, even though""
and0
are falsy values. While""
and0
may be unusual ID values, it's conceivable that an incrementing counter could start at 0, or a string ID could sometimes be the empty string.You could perhaps argue that objects with
id: false
also should not be normalized, sincefalse
a constant value that's uncommon for IDs, butnull
and undefined show up due to ordinary GraphQL error behavior, soid: false
suggests some actual intention to return a constant value. My mind isn't fully made up about this, but I'm erring on the side of preserving existing behavior.