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

useSuspenseQuery reverts back to variables set in PreloadQuery when search params change #384

Open
Tam opened this issue Oct 29, 2024 · 2 comments

Comments

@Tam
Copy link

Tam commented Oct 29, 2024

We have a query setup that has some default variables on the server that can then change on the client. As an example:

const QUERY = gql`
    query GetChart ($id: UUID!, $compact: Boolean) {
        # ...
    }
`;

// Server
// =========================================================================

return (
    <PreloadQuery query={QUERY} variables={{ id, compact: true }}>
        {/* ... */}
    </PreloadQuery>
);

// Client
// =========================================================================

const [compact, setCompact] = useState(true);
const { data } = useSuspenseQuery(QUERY, {
    variables: { id, compact },
    fetchPolicy: 'cache-and-network',
});

The user can change the value of compact on the client.

If the search parameters then change (in our case clicking an "Edit" button might open a modal by pushing to the same route with ?edit=true) the compact state will persist with whatever value the user has set, but the value of data will revert back to whatever the initial value was loaded from the server.

In the example above this means that if the user changed compact from true to false and then clicked an "Edit" button, the graphql data would revert back to seeing compact as true while the rest of the UI would still be seeing it as false.

Package Version
@apollo/client 3.11.8
@apollo/experimental-nextjs-app-support 0.11.5
next 15.0.1
react 18
@phryneas
Copy link
Member

That really seems curious. I know it's a big ask, but could you maybe please create a small reproduction for this issue?

@Tam
Copy link
Author

Tam commented Dec 20, 2024

Sorry, haven't been able to create a reproduction yet, been up against it!

Adding to this, we're also having issues where we update something via a mutation client side, use refetchQueries on the mutation, call refetch on the useSuspenseQuery, set fetchPolicy: 'no-cache', on the useSuspenseQuery, have export const dynamic = 'force-dynamic'; on the page, and cache: 'no-cache' on the fetch function, but the change is lost when refreshing. The only way to see it is, at least locally, to cause a rebuild.

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

No branches or pull requests

2 participants