Skip to content

Query cache results don't return readonly types in Typescript #12738

Closed
@CamJohnson26

Description

@CamJohnson26

Issue Description

Reopening this old issue: #5438

We've had a few incidents in production triggered by trying to add properties to frozen Apollo cache objects. These operations fail at runtime, but are allowed by typescript since the cache results aren't typed with readonly.

While ideally we never should modify the cache results, sometimes we change a query and want to write an adapter so we don't have to update all downstream components, so this is very easy to accidentally do if you shallow copy an object and try to push an element to a subfield.

I linked a codesandbox reproduction. FYI the template doesn't include Typescript out of the box, so had to add the typechecker plugin manually, but this shows that typescript won't catch the error.

Link to Reproduction

https://codesandbox.io/p/devbox/reverent-pond-8wgnss?workspaceId=ws_T8pRa7Jre9csCVyagESXB

Reproduction Steps

  • Make an apollo query and return data
  • Attempt to set a property on the data
  • Apollo will throw an error at runtime, but provide no typesafety.

@apollo/client version

3.11.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions