I am trying to implement Gatsby with Apollo Client
Here is my gatsby-browser.js file
export const wrapPageElement = ({ element, props }) => {
const client = new ApolloClient({
uri: "http://localhost:3000/graphql",
cache: new InMemoryCache(),
credentials: "include",
});
window.a = client;
return (
<CookiesProvider>
<ApolloProvider client={client}>
<ChakraProvider theme={theme} {...props}>
{element}
</ChakraProvider>
</ApolloProvider>
</CookiesProvider>
);
};
I am persisting my logged in user details in GraphQL cache so that it can be accessed anywhere in my application. Here is the code of logging in
const [login, { data, error }] = useMutation(AUTH_USER, {
onCompleted: ({ login: data }) => {
try {
client.writeFragment({
id: "me",
fragment: FRAGMENTS_USER.all_details,
data: data,
});
setToken(data.token);
} catch (e) {}
},
});
My problem is that the cache is being reset on navigation. So, using gatsby-link or gatsby-navigation is resetting my cache
I have been using NextJS, and it is the first time I am writing applications in Gatsby, but it doesn't seem to behave like a Single-Page application in that case.