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

(graphache) - Fix query traversal for previous null fields #772

Merged
merged 6 commits into from Apr 29, 2020

Conversation

kitten
Copy link
Member

@kitten kitten commented Apr 29, 2020

Fix #768

Summary

This issue occurs when a previous selection determined a field to be null. A future selection that doesn't result in null is then forced to retrieved prevData, but isn't covering the null case.

This can happen with split selections on a single entity, e.g.

query Item {
  ...NullItem
  ...ValidItem
}

fragment NullItem {
  item {
    id
    invalid # Suppose this causes the item to become null, due to an invalid field
  }
}

fragment ValidItem {
  item {
    id
  }
}
  • On the first traversal, the NullItem fragment hits invalid which causes Query.item to become null
  • On the second traversal, the Validitem fragment hits id and tries to write it, but Query.item is already null

This can only happen with schema awareness most of the time.

Set of changes

  • Handle prevData === null by switching most of its uses to a truthy rather than an undefined check.
  • Fix case where prevData === null leads to further traversal

Outstanding edge cases

The read edge case can be seen in this PR's test case, when a traversal fails and returns undefined, then the second traversal doesn't set the affected field to null.

@changeset-bot
Copy link

changeset-bot bot commented Apr 29, 2020

🦋 Changeset is good to go

Latest commit: 70d5e2b

We got this.

This PR includes changesets to release 1 package
Name Type
@urql/exchange-graphcache Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kitten
Copy link
Member Author

kitten commented Apr 29, 2020

We can fix the write edge case probably by introducing prevData to writeField and checking whether a previously written field was null before committing it.

We can maybe fix the read edge case by introducing a mode on SelectionIterator somehow that pre-merges overlapping selection sets.

Edit: I believe the write case is sufficiently unlikely and a fix would simply make its behaviour a little more intuitive for a very high cost.

This properly fixes the issue where during `query` a
previously resolved field that has been set to null
is still being traversed.
@kitten kitten marked this pull request as ready for review April 29, 2020 14:19
Copy link
Contributor

@wgolledge wgolledge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

Co-Authored-By: Jovi De Croock <jovi.decroock@formidable.com>
@kitten kitten changed the title (graphache) - Fix traversal failing when a previous selection wrote null (graphache) - Fix query traversal for previous null fields Apr 29, 2020
@kitten kitten merged commit 395ae73 into master Apr 29, 2020
@kitten kitten deleted the fix/graphcache-traversal branch April 29, 2020 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot Page will Nullable Nested Objects: TypeError: Cannot set property '__typename' of null
3 participants