apollo-kotlin icon indicating copy to clipboard operation
apollo-kotlin copied to clipboard

[Brainstorming] make the cache work without possibleTypes

Open martinbonnin opened this issue 2 years ago • 0 comments

Right now, we generate type information in codegen through CompiledField.type and CompiledFragment.possibleTypes. When normalizing/reading from the cache, we use the same collectFields() algorithm that we use in codegen. Except that because the queries are valid, we can do a lot more assumptions in the cache code.

Instead of walking the operation from the selections, we could walk it from the Json/Map and for each Json field, lookup the first GraphQL field from the operation by walking all the fragments. Because all fields must have the same type and argument, this should be safe.

The only edge case I can think about is disjoint types:

{
  animal {
    __typename
    ... on Dog {
       picture(size: LARGE)
    }
    ... on Cat {
       picture(size: SMALL)
    }
  }
}

Because there are two possible arguments values there we need to disambiguate but this should all be possible because that only happens on concrete types and we should have __typename.

Of course that won't work if CacheResolver or CacheKeyGenerator need type information but this might open opportunities for more dynamic runtime caching (because we won't need the schema anymore).

martinbonnin avatar Mar 23 '22 18:03 martinbonnin