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

fetchMore overrides the variables without respecting nested items #12079

Closed
ps173 opened this issue Sep 27, 2024 · 3 comments · Fixed by #12080
Closed

fetchMore overrides the variables without respecting nested items #12079

ps173 opened this issue Sep 27, 2024 · 3 comments · Fixed by #12080

Comments

@ps173
Copy link

ps173 commented Sep 27, 2024

Issue Description

variables with nested items are overriden when fetchMore is performed. I am not sure whether this issue has already been reported or not but I couldn't find it anywhere in repo so far.

for example

  const { data, fetchMore } = useQuery(MY_QUERY, {
    variables: {
      parentVariable: {
        a: "some_value",
      },
    },
  });

  // in fetch more
  async function fetchMoreItems() {
    await fetchMore({
      variables: {
        parentVariable: {
          // here instead of `a` some other value is passed. 
          // so new value of parentVariable will be 
          // { b : "some_value" } rather than { a: "some_value", b: "some_value" }
          b: "some_value"
        }
      },

      // concat items, can be ignored
      updateQuery: (previousResult, { fetchMoreResult }) => {
        const newEdges = fetchMoreResult?.people ?? [];
        const oldEdges = previousResult?.people ?? [];

        return {
          people: [...oldEdges, ...newEdges],
        };
      },
    });
  }
}

Although the doc says merged but docs don't really say that nested objects will be overriden with new value.
This might be intended and does make sense but should be documented

Link to Reproduction

https://codesandbox.io/p/devbox/practical-wozniak-d6mxd4?workspaceId=b0cc520f-8431-4414-9657-24f9bb60a21b

Reproduction Steps

No response

@apollo/client version

3.5.6

@jerelmiller
Copy link
Member

Hey @ps173 👋

Thats a good callout about the docs. The merge that happens is a shallow merge so what you're seeing is expected. I've opened #12080 to make it a bit clearer in the docs that variable merging is a shallow merge.

Copy link
Contributor

github-actions bot commented Oct 1, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Copy link
Contributor

github-actions bot commented Nov 1, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants