Checking cache for data before trying to query for it?

I have a scenario that I believe would be common to other websites. Lets say I have a blog that can show a list of posts and individual posts.

On the /index page, it loads 10 blog posts with full text and displays them in a list. The query could be getLatestBlogPosts and is trigger on the page load. Each post also has a link so that the post can be viewed on its own. That link is /posts/1 or /posts/2 etc. The query on that page is getPost({id:1}) and is triggered on page load. A user can get to that page by navigating through the index page or browsing directly to the post URL.

Now for my question… when the user navigates from the index page… I don’t want to refetch the post since I already have it. But since the /posts/1 has a different query, it gets executed.

I think I need to check if I have that post in my cached results from the getLatestBlogPosts query. If not, I want to fetch just that post to show it. Is there a common pattern I should follow for this?

1 Like

I can solve this by having my cell do two things:

  1. Retrieving a list of posts
  2. If a post ID is passed in as a prop… Filter the list by the ID to return one post. If no post ID is passed in, just show the list

<PostsCell postId={1} />

Since this is just one query, I don’t need to query again to show the post on its own. While this works I am not sure if it is the correct way to do it.

Just a comment: placing your issue in Debugging & Dev Problem Solving - RedwoodJS Community led you to a long standing thread on Proposal: Testing and Debugging Project, which tries to get the Redwood users (as different from contributors / core team members) engage in writing RedwoodJS online documentation on Testing and Debugging (code named Tandem).

This comment is written to explain a possible lack of answers to your cache related question which is very more special than the notion of “testing and debugging” - for possible fear of falling in the document writing trap :wink:

You may want to look into Introduction to Apollo Client - Apollo GraphQL Docs to setup and configure a client side cache for each post.

However, one consideration is that your get posts query may not have fetched all the attributes needed to display the individual post.

The master find many might just show title and author in a list but the detail would fetch title, author, body and time stamps.

Another approach would be to us GraphQL caching and preemptively cache each post detail on the background per the request operation such that subsequent calls to fetch the post uses the cache instead of a db query.

1 Like

Theres also a great library might be looking something similar to react-query. It has graphql support. Using it though would make you move away from redwoodjs recommended approach.

@adriatic I don’t understand your comment at all. Can you please explain?

I don’t understand your comment at all. Can you please explain?

It’s not very relevant to your case, but nevertheless: I am “driving” three issues all having the word “debugging” in their titles:

  1. Proposal: Testing and Debugging Project
  2. Tandem (Testing and Debugging project) Phase II
  3. Tandem - implementation, use and feedback collection

All three are discussions on how to design and implement the online tutorial on testing and debugging - tutorial that starts at the “place” where the original tutorial ends.

Now my poor choice of a joke was the advice not to place your discussion under the title that contains the word debugging, as all possible responders might ignore your post thinking that is is one mine.

Had I just responded with “never mind” that would be quite impolite :wink: