-
Notifications
You must be signed in to change notification settings - Fork 36
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
Can't create a client per page #322
Comments
You should only ever have one ApolloClient instance - and one provider - in your application. This is a restriction we have here because your page will run in SSR, and if you had multiple providers we wouldn't know which data should be rehydrated with which client instance. Just place your |
@phryneas In that case, how would I switch client between pages? The layout doesn't re-render on page navigation. |
You should never switch the client. Why do you want to do that? |
I have 2 applications within the same Next.js app that communicate with different graphql servers. As of now I don't have the control to merge these into a single GQL endpoint. |
Ah, that's indeed one of the very rare cases where that might make sense. You could call import { resetApolloClientSingletons } from "@apollo/experimental-nextjs-app-support"; |
That worked great, thanks! For future reference for those reading this issue: you want call resetApolloClientSingletons whenever the client changes. In my case I found it most natural to do so in export function ApolloWrapper({
children,
client,
}: React.PropsWithChildren<{ client: "a" | "b" }>) {
React.useEffect(() => {
resetApolloClientSingletons();
}, [client]);
return (
<ApolloNextAppProvider makeClient={getMakeClient(client)}>
{children}
</ApolloNextAppProvider>
);
} |
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. |
I'm not sure if this is a bug or a user error, but I can't wrap 2 different pages in 2 different GraphQL providers with respective clients, as
makeClient
is not called a subsequent time after navigating from page 1 to 2.I've created a sandbox here that demonstrates the problem: https://codesandbox.io/p/devbox/kind-tristan-jnk964
makeClient
is never called, and that the original client is cached somehowIs this a bug, or do I misunderstand how the provider should work?
The text was updated successfully, but these errors were encountered: