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

Queries ignore values returned from field policy read functions

Open vjrasane opened this issue 1 year ago • 4 comments

Issue Description

I've configured field policy for GraphQL types as described here: https://www.apollographql.com/docs/react/caching/cache-field-behavior/#the-read-function

Here is an excerpt:

typePolicies: {
      TickeType: {
        fields: {
          createdAt(value, opts) {
            return 'yolololoswag'
          },
        },
      }

I have verified by debugging that the field policy read function is called and that the value passed to it is correct. However, in the component using a regular useQuery hook that returns an object of the correct type, the value of the createdAt field is still equal to what was passed as the first argument to the field policy read function.

The client version I'm using is 3.8.8

Link to Reproduction

will provide at a later time

Reproduction Steps

No response

vjrasane avatar Dec 05 '23 12:12 vjrasane

Just to be sure that this is just an error that happened when you wrote down this example - it would need to be

typePolicies: {
      TickeType: {
        fields: {
          createdAt: {
            read(value, opts) {
              return 'yolololoswag'
            },
          },
        },
      }

So you need a nested read function. Are you doing that? Also, did you double-check that you are not using a no-cache field policy in your useQuery call?

phryneas avatar Dec 05 '23 13:12 phryneas

The nested read is not required, as documented here: https://www.apollographql.com/docs/react/caching/cache-field-behavior/#handling-field-arguments

Nonetheless I have verified that the function is called regardless of whether it is a nested read function or a direct value of the field policy.

I have not defined a fetch policy for this query and the default for this application is cache-first

vjrasane avatar Dec 05 '23 13:12 vjrasane

Sorry, I was not aware of that short notation.

What you are seeing is definitely weird.

I have used read functions in the past, and we are extentensively testing them, e.g. in the policy tests

Could you maybe create a reproduction that shows the problem?

phryneas avatar Dec 06 '23 13:12 phryneas

@vjrasane Are you sure the TickeType is correct typename? Maybe it should be written as TicketType?

broyde avatar Dec 11 '23 18:12 broyde