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

Do smarter root query diffing #113

Closed
stubailo opened this issue Apr 15, 2016 · 3 comments
Closed

Do smarter root query diffing #113

stubailo opened this issue Apr 15, 2016 · 3 comments

Comments

@stubailo
Copy link
Contributor

stubailo commented Apr 15, 2016

Right now, if you do the following queries in succession:

// First
{
  todoList(id: 5) {
    title
    createdAt
    tasks {
      name
      completed
    }
  }
}

// Second
{
  todoList(id: 5) {
    tasks {
      name
      completed
      author
    }
  }
}

The query diffing algorithm will refetch:

// Current implementation, not 100% efficient
{
  todoList(id: 5) {
    tasks {
      name
      completed
      author
    }
  }
}

Instead of:

// Desired, completely efficient based on given info
{
  todoList(id: 5) {
    tasks {
      author
    }
  }
}

This was a decision to not implement a new query diffing/merging algorithm before alpha. So it still works now, but will refetch some data.

The fix

In the XXX sections in diffAgainstStore: https://github.com/apollostack/apollo-client/blob/8df914c21980fb323699fc795171b2aedf3f98e1/src/data/diffAgainstStore.ts#L186

Instead of bailing out the first time we find a missing field, we should instead traverse all of the subtrees, and make a union of all of the actual missing fields. This requires an algorithm for recursively merging GraphQL SelectionSet ASTs, which isn't hard to build but is non-trivial.

Ideally, this merging algorithm would be a separate, well-tested NPM package that we can bring to the wider community, since it's totally agnostic to Apollo Client.

@mquandalle
Copy link
Contributor

mquandalle commented Apr 20, 2016

Have you reversed the current and desired implementation examples?

@stubailo
Copy link
Contributor Author

Oh, indeed I have! Thanks for the heads up.

@stubailo
Copy link
Contributor Author

After talking to @martijnwalraven: Another option is to only do this for single fields, but not for arrays. That way the algorithm is much simpler, and we don't end up with some odd edge cases.

@stubailo stubailo closed this as completed Sep 2, 2016
jbaxleyiii pushed a commit that referenced this issue Oct 17, 2017
* Added scalars

* Added the JSON example, removed typescript, added references.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants