Skip to content

Incorrect resolution of nested @client fields in v4 #12930

@broyde

Description

@broyde

Issue Description

I have a query with client field:

query {
  child {
    clientField @client
    normalField
  }
}

And typePolicies:

typePolicies: {
  Child: {
    fields: {
      clientField: (value = 1) => value,
    },
  },
},

When I do const {data} = await client.query({query}), I expect data.child.clientField to be 1, but it's null instead.

I assume it may be helpful:

  • With separated read and merge functions we can see that merge is called first in this case, and it writes null. That's the problem.
  • With root fields instead of nested ones, it works properly.
  • With @client only fields it works properly too.

Link to Reproduction

https://github.com/broyde/apollo-nested-client-field

Reproduction Steps

I've created simple jest tests to show the problem.

  1. git clone https://github.com/broyde/apollo-nested-client-field.git
  2. cd apollo-nested-client-field
  3. npm i
  4. npm test
  5. See the test nested-client-field.test.js and how it fails:
FAIL ./nested-client-field.test.js
  ● Console

    console.log
      merge { cachedValue: undefined, value: null }

    console.log
      read null

  ● Should resolve nested @client fields properly

    expect(received).toEqual(expected) // deep equality

    - Expected  - 1
    + Received  + 1

      Object {
        "__typename": "Child",
    -   "clientField": 1,
    +   "clientField": null,
        "normalField": 2,
      }

      49 |
      50 |   const {data} = await client.query({query});
    > 51 |   expect(data.child).toEqual({
         |                      ^
      52 |     __typename: 'Child',
      53 |     clientField: 1,
      54 |     normalField: 2,

      at Object.toEqual (nested-client-field.test.js:51:22)

@apollo/client version

4.0.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions