I am using ApolloProvider with two authorizations, one for guests and the other for signed-in users. The default client is with the guest authorization and on sign-in, I create a new client with special specific tokens. However, queries that require user authorizations I make soon after sometimes return 401 Permission Denied error. How can I ensure I am using the correct client for my query?
Here is some sample code:
const CustomApolloProvider = ({ children }) => {
const [client, setClient] = useState(getGuestClient());
// gets called when user signs in
const onUserSignIn = () => {
setClient(getUserClient());
});
return <ApolloProvider client={client}>{children}</ApolloProvider>;
};
Another file that throws an error:
const [
checkUserExists, // needs special permissions
{
data: checkUserExistsData,
loading: loadingCheckUserExistsData,
error: checkUserExistsError,
},
] = useLazyQuery(gql(getUser), { fetchPolicy: 'network-only' });
// gets called when user signs in
const onUserSignIn = () => {
await checkUserExists();
});