Persistent User Data In Apollo Graphql

1 minute read | 2 months ago

When I was building a mobile app using Expo I encountred a problem where, if a user is logged out, and a diffrent user is logged in (Without reloading the app or closing it). When a request is sent to the server, it uses the data of the previous user to make requests. After days of debugging, I came to realize that, I had to set my fetchPolicy to no-cache, so that Apollo Graphql won't cache my data.

Previously

const { data, loading, error } = useQuery<{ user: UserResponse }>( GET_USER, );

It should have been

const { data, loading, error } = useQuery<{ user: UserResponse }>( GET_USER, { fetchPolicy: "no-cache" }, );

Reference: Apollo useQuery