Handling relationships with React Query

17 Views Asked by At

Are there any pre-existing libraries that help with handling relational data with react query? For example, say I have a backend model called 'Person'

type Person = {
  id: string;
  age: number;
  name: string;
  ...
  addresses?: Address[];
  cards?: Card[];
}

That is, Person has one-to-many relationship with both Address and Card models;

When I make a network call to backend for Person, I can ask whether I want to 'include' a relationship. So, for example, on page X, I can ask for 'addresses' to be sent back with a Person, while on page y, I can ask for 'cards'.

If I just use queryKey: ['person', <person.id>] as my key, then the data is different on page X and Y. If I add a STALE_TIME and don't allow refetch unless stale, then the correct data would never be fetched on page Y.

I'm coming from Ember JS, where it handles this in house. So when on page X I get Person with addresses, it automatically updates the Person model with its specific data, and also updates Address model (by looking at the address.id).

Is there any way to do something like this with React Query? Worst case I can write a custom function but would like to know if there's already existing solutions.

I couldn't find from simple google searches

0

There are 0 best solutions below