Skip to content

Conversation

stubailo
Copy link
Contributor

This is the beginning of implementing a feature where the client can detect when part of a query is already in the cache, and create a set of queries to refetch only the necessary objects/fields.

@stubailo
Copy link
Contributor Author

Interesting issue. To refetch field1 and field2 on a node, you can't just do:

{
  node(id: 'id') {
    field1
    field2
  }
}

You need to do:

{
  node(id: "id") {
    ... on NodeType {
      field1
      field2
    }
  }
}

This means that the client that sends the query needs to be aware of the type of the node it's refetching. That's pretty unfortunate, because there aren't many other situations which require type information.

Possible solutions:

  1. Define a different server-side spec than Relay which doesn't require type information (for example, a type that has all fields of all types and dynamically does stuff depending on the ID). This might be going against the grain of GraphQL itself, though, given: http://facebook.github.io/graphql/#sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types
  2. Load the types in each query using __typename, much like we need to load id anyway. That way we can have the type of every node in the Store, and use this information in the fragment when refetching.

(2) seems like the sane option here, so I'm probably going to go with that.

@jbaxleyiii
Copy link
Contributor

I agree that __typename is a better option and I imagine we will find some use for it in other places. Having that could help for a client side validation of subsequent requests by stripping fields that don't exist and providing a localized error instead of having the server do the validation and send back the error?

The only issue with that I guess is if the server updates with new fields between requests, the client side would have to make an extra trip after realizing the field now exists so maybe that is a bad idea.

@stubailo
Copy link
Contributor Author

So far, my mental model is that query validation is a development-time concern rather than something you would do in production, just because of the performance overhead (in production, my assumption would be that the schema doesn't change very often, and in the overwhelming majority of cases it will be adding new fields vs. removing them)

But regardless, I agree that having type information on the client is useful. For example, "give me all of the todo items currently in the cache" in a minimongo style could be a very useful operation to have available, even in production.

@stubailo
Copy link
Contributor Author

Going to throw away this branch and start over with typescript

@stubailo stubailo closed this Mar 21, 2016
@stubailo stubailo deleted the query-diffing branch March 22, 2016 06:53
@bgentry bgentry mentioned this pull request Mar 26, 2017
7 tasks
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 19, 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

Successfully merging this pull request may close these issues.

2 participants