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

Mutations receive raw data without field read type policies applied

Open dharkness opened this issue 2 years ago • 6 comments

Intended outcome:

I have defined a read field policy for a type. When I call a mutation through the client (not the hook) using client.mutate(...), I expect to have that field policy applied to the promise's result. In this case, the object with the read policy is an embedded object without any key fields.

I rewrote this to use useMutation, and its onCompleted callback also receives the raw data without the read policy applied.

Actual outcome:

The returned promise receives the raw data from the server without the read policy applied even though the read policy is executed before resolving the promise.

~~The useMutation hook behaves as expected.~~ Strangely, so does client.query(...).

How to reproduce the issue:

I have created reproduction sandboxes for client.mutate and useMutation. The important bits are in index.jsx, specifically renamePerson and const client = ....

  1. Open the console when running it
  2. You'll see the three date strings get converted in the read policy on load.
  3. Enter a name and click the Rename Second Person button.
  4. You'll see the updated date string get converted and a message saying the time field is a string. It should be a Date object which is what the read policy returns.

Versions

The sandbox uses version 3.6.8. This is from my application:

System:
  OS: Linux 5.13 Ubuntu 20.04.5 LTS (Focal Fossa)
Binaries:
  Node: 14.19.0 - ~/.asdf/installs/nodejs/14.19.0/bin/node
  Yarn: 1.22.19 - /usr/bin/yarn
  npm: 6.14.16 - ~/.asdf/plugins/nodejs/shims/npm
Browsers:
  Chrome: 105.0.5195.52
  Firefox: 104.0
npmPackages:
  @apollo/client: ^3.5.10 => 3.5.10 

dharkness avatar Sep 08 '22 17:09 dharkness

any news there or how to patch it?

daniel-g avatar Nov 28 '22 22:11 daniel-g

We now have one instance where this happens during a query. It depends on the data somehow and is repeatable. For one entity type, some instances have this issue but not all.

Is there any clue so far what's causing it?

Edit: This new query issue turned out to be caused by an attached entity that implemented an interface, and not adding it in the list of possibleTypes for the interface. The mutation sandbox I created above does not use interfaces, but perhaps it's related.

dharkness avatar Feb 27 '23 18:02 dharkness

I thought I was crazy. Still doing this in 3.7.7.

timscott avatar Oct 05 '23 21:10 timscott

I'm also facing this issue (using useMutation). Any plan to fix this?

such avatar Jan 05 '24 10:01 such

A workaround is to add:

defaultOptions: {
  mutate: {
    update: () => {},
  }
}

when initializing ApolloClient

such avatar Jan 06 '24 12:01 such

Facing a similar issue and while the workaround suggested by @such does work it would be better to get a fix for this issue instead.

Geccles avatar Jan 29 '24 18:01 Geccles