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

Cannot modify the results of a query [Cannot assign to read only property]

Open dulfe opened this issue 2 years ago • 2 comments

Related to Issue #7507

I cannot modify the results of a Lazy Query (I have not checked regular Queries) if caching is enabled. The results seem frozen, but adding fetchPolicy: 'no-cache' fixes the issue

Intended outcome:

Be able to modify the results of a query.

Actual outcome:

TypeError: Cannot assign to read only property 'field' of object '#<Object>'

How to reproduce the issue:

  • Create a query using lazyQuery (const [loadData, {data}]= useLazyQuery(GQL_SOMETHING))
  • Execute query (loadData())
  • Try and modify the results, something like this: data.records.nodes[1].field='something'
  • The error occurs.

Versions

System: OS: Windows 10 10.0.22000 <-- It sais Win 10, but I have Win11 Binaries: Node: 16.13.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Spartan (44.22000.120.0), Chromium (99.0.1150.30) npmPackages: @apollo/client: 3.5.10 => 3.5.10

dulfe avatar Mar 09 '22 17:03 dulfe

I have the same issue with regular queries. If this behavior is intentional, its a breaking change and should have required a major version bump...

DersJ avatar Aug 04 '22 16:08 DersJ

I believe this behavior is intentional. It was optional behavior in v2 and became required with the upgrade to v3.

dylanwulf avatar Aug 04 '22 17:08 dylanwulf

@dylanwulf is correct - updating the cache can be done through a variety of methods but directly mutating the object is not one of them. This has been the case since version 2. Check out these docs for more information on how to read from and write to the cache safely!

bignimbus avatar Dec 12 '22 21:12 bignimbus