Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential regression: keyFields function called with normalized objects since 3.7.15 #12261

Open
phryneas opened this issue Jan 10, 2025 · 0 comments
Labels

Comments

@phryneas
Copy link
Member

phryneas commented Jan 10, 2025

Issue Description

It seems this happened with #10789 and was reported in Discord here:

https://discord.com/channels/1022972389463687228/1325644501976350881/1325644501976350881

Reproduction Steps

This test logs differently depending on version:

// in `policies.ts`
  it("`keyFields` function gets denormalized nested children", () => {
    const cache = new InMemoryCache({
      typePolicies: {
        Book: {
          keyFields(book, context) {
            console.log("keyFields", book);
            return ["isbn"];
          },
        },
      },
    });

    const bookQueryWithAuthorId = gql`
      query {
        book {
          title
          author {
            id
            name
          }
        }
      }
    `;

    const theInformationBookDataWithAuthorId = {
      __typename: "Book",
      isbn: "1400096235",
      title: "The Information",
      subtitle: "A History, a Theory, a Flood",
      author: {
        __typename: "Author",
        id: "123",
        name: "James Gleick",
      },
    };

    cache.writeQuery({
      query: bookQueryWithAuthorId,
      data: {
        book: theInformationBookDataWithAuthorId,
      },
    });

    expect(cache.extract(true)).toEqual({
      ROOT_QUERY: {
        __typename: "Query",
        book: {
          __ref: 'Book:{"isbn":"1400096235"}',
        },
      },
      "Author:123": {
        __typename: "Author",
        id: "123",
        name: "James Gleick",
      },
      'Book:{"isbn":"1400096235"}': {
        __typename: "Book",
        isbn: "1400096235",
        title: "The Information",
        author: { __ref: "Author:123" },
      },
    });
  });

Log pre-3.7.15:

  console.log
    keyFields {
      __typename: 'Book',
      isbn: '1400096235',
      title: 'The Information',
      subtitle: 'A History, a Theory, a Flood',
      author: { __typename: 'Author', id: '123', name: 'James Gleick' }
    }

Log from 3.7.15 on:

 console.log
    keyFields {
      __typename: 'Book',
      isbn: '1400096235',
      title: 'The Information',
      subtitle: 'A History, a Theory, a Flood',
      author: { __ref: 'Author:123' }
    }

      at keyFields (cache/inmemory/__tests__/policies.ts:366:21)
          at Map.forEach (<anonymous>)

Note the change here:

-      author: { __typename: 'Author', id: '123', name: 'James Gleick' }
+     author: { __ref: 'Author:123' }

Now this has been out for quite a while and it seems only one person was affected so far, so we'll have to weigh if it's possible to fix this while keeping the other bugfix in place.

This is mostly a tracking issue without a final call on that so far.

@apollo/client version

3.7.15 - present (3.12.x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants