tRPCttRPC
Powered by
himH
tRPC•3mo ago•
2 replies
him

Invalidating Queries

Hey! Might be being thick here but I'm implementing in an optimistic update layer and fully expected this to work:
await queryClient.invalidateQueries({ queryKey: scheduleKey });

// I compute the query key in a reusable hook
function useScheduleQueryKey() {
  const api = useTRPC();
  const [weekStart] = useScheduleParams();

  return api.schedule.getWeek.queryKey({ weekStart });
}
await queryClient.invalidateQueries({ queryKey: scheduleKey });

// I compute the query key in a reusable hook
function useScheduleQueryKey() {
  const api = useTRPC();
  const [weekStart] = useScheduleParams();

  return api.schedule.getWeek.queryKey({ weekStart });
}


However that won't work. You can make it work by either removing the input - so
getWeek.queryKey()
getWeek.queryKey()
or by passing the key as the only arg;
queryClient.invalidateQueries(queryKey) // Have a feeling this only works becuase it seems to just invalidate any active query so this is likely a red herring
queryClient.invalidateQueries(queryKey) // Have a feeling this only works becuase it seems to just invalidate any active query so this is likely a red herring


Is there a way I can just invalidate the query based on input or am I thinking about this wrong?

The query key is correct as I can use
setQueryData
setQueryData
on it fine;
    queryClient.setQueryData(scheduleKey, onMutateResult.currentData);
    queryClient.setQueryData(scheduleKey, onMutateResult.currentData);
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

best practice for invalidating protected queries upon profile switch
jJj / ❓-help
12mo ago
useContext not Invalidating
typedefTtypedef / ❓-help
3y ago
What would be considered best practice for getting data and invalidating queries?
zodiakZzodiak / ❓-help
3y ago
Invalidating data in server components
Answer OverflowAAnswer Overflow / ❓-help
3y ago