We’re trying to convert our web app into a PWA with offline features. I’m using apollo3-cache-persist to cache calls from graphql. And when I inspect, go to Application and check the cache, I can see that the query has been stored.
Specifically, I’m checking cast_blocks query. I checked the references it points to and it seems everything is stored in the cache:
But when I switch to offline and then refresh, when I check the Network tab, I can see that it still tries to make a Network call. Why is this? I was expecting it to retrieve data from the cache.
For reference, this is my client.ts:
import { ApolloClient, InMemoryCache } from "@apollo/client";
import { createUploadLink } from "apollo-upload-client";
import { LocalStorageWrapper, persistCache } from 'apollo3-cache-persist';
const uploadLink = createUploadLink({
uri: "/api/graphql",
});
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
account_and_user_details_screen: {
merge(existing, incoming) {
if (existing) {
return existing;
}
return incoming;
},
},
},
},
},
})
await persistCache({
cache,
storage: new LocalStorageWrapper(window.localStorage),
debug: true, // Enable debug mode for more logging
maxSize: false, // Disable size limit for debugging purposes
});
export const apolloClient = new ApolloClient({
cache: cache,
link: uploadLink,
connectToDevTools: true,
});
And this is the actual query:
export const QUERY_SCREEN = gql`
query Cast_blocks($slug: String!) {
cast_block_screen(slug: $slug) {
id
cast_blocks {
id
attributes {
type
properties {
... on CastBlockPropertiesButtonSearch {
_dummy
}
... on CastBlockPropertiesCarousel {
items {
src
image
url
}
}
... on CastBlockPropertiesCatalog {
uuid
}
... on CastBlockPropertiesMarkdown {
content
}
... on CastBlockPropertiesEmbed {
url
}
... on CastBlockPropertiesCourseStatus {
heading
}
}
}
}
}
}
`;
export interface ScreenCastBlockListProps {}
export function ScreenCastBlockList({}: ScreenCastBlockListProps) {
const { slug } = useParams();
const { loading, error, data } = useQuery(QUERY_SCREEN, {
variables: {
slug: slug,
},
});
useErrorHandler(error);
const isGuest = useContext(isGuestContext);
return (
<NavBarLayout>
{loading ? (
<div data-testid="loading">
<LoadingCentered />
</div>
) : (
<CastBlocks data={data} />
)}
</NavBarLayout>
);
}