urql-graphql / urql

The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
https://urql.dev/goto/docs
MIT License
8.54k stars 444 forks source link

cacheExchange - cache.readQuery (updateQuery) can't find queries with aliased variables #3509

Closed farin closed 4 months ago

farin commented 4 months ago

Describe the bug

Let's use https://github.com/FormidableLabs/trygql/blob/main/graphql/intermittent-colors/__generated/schema.gen.graphql for reproduction

Even with minimalistic query

query Colors($l: Int!) {
    items: colors(limit: $l) {
      name
      hex
    }
  }

when variable is aliased (in this case limit as $l) then readQuery doesn't find it. In my example I used cache.inspectFields to be sure that query is cached and calling it with riight arguments.

If query use same name for query and field variable than all is ok.

Tried to debug it little in browser. Query is properly store in cache (with right key), but underlying readLink/getNode uses wrong fieldKey. For complex queries with more argument, the key is missing aliased argument and then doesn't match key under which is query stored in cache. Attaching trace with place when lookup fails. trace

Reproduction

https://github.com/farin/urql-cache-bug/blob/main/index.mjs

Urql version

@urql/exchange-graphcache v6.4.1

Validations

farin commented 4 months ago

Eventually I realized it's not a bug. What's inconvenient, that original query document can't be used in readQuery / updateQuery calls, bacause stored arguments has different names than accpets query.