Replies: 2 comments
-
I find that this is an area that can get really complicated, really quickly. I think that rxjs, with all its power, can cause us to write more complex solutions that necessary. I have situations like yours all over my project and my personal approach is that I recognise my flow is fairly sequential so I have areas where I convert the subscriptions into promises. For example, in my user service: getUser() {
return this.useQuery(['sessionUser'], () =>
this.auth.getAuthUser().pipe(
filter(isUserUidDefined),
switchMap((auth) => this.fsCalls.getUser(auth.data.uid as string))
)
).result$;
}
getUserAsync(): Promise<User | undefined> {
return this.firstSuccess(this.getUser());
} Where And the usage for me is like so: const user = await this.getUserService.getUserAsync();
// do another query I get that this pattern might not be for everyone but it allows me to write really clean and working solutions in situations like yours where queries have dependencies on others |
Beta Was this translation helpful? Give feedback.
-
If you have the ability to update your backend api, you could create a new endpoint that takes a You would, of course, still not get a separate query key for each hotel. For that, you might want to define a separate query for the hotel room by the roomId (similar to your The one challenge I see with this approach is that you now have the same export class SomeService {
private readonly query = injectQuery();
private readonly queryClient = injectQueryClient();
private readonly hotelService = inject(HotelApiService);
public getHotel(id: number): {...roomIds: number[]} {
return this.query({queryKey: ['hotels', id], queryFn: () => this.hotelService.getHotelById({ id }) });
}
public getRoomsForHotel(hotelId: number) {
return this.query({ queryKey: ['roomsForHotel', hotelId], queryFn: () => this.hotelService.getRoomsForHotel({ hotelId }) });
}
public getRoom(roomId: number, hotelId?: number) {
return this.query(
queryKey: ['rooms', roomId],
queryFn: () => this.hotelService.getRoomById({ id: roomId }),
initialData: () => {
if(!hotelId) return undefined;
// Use a room from the '['roomsForHotel', hotelId]' query as the initial data for this query
return this.queryClient.getQueryData(['roomsForHotel', hotelId])?.find((room) => room.id === roomId)
},
);
}
} |
Beta Was this translation helpful? Give feedback.
-
It seems, my question is a bit connected to #104.
I have some queries in a service:
It's needed to fetch hotel first (
getHotel
) and then get some room byid
that I got fromgetHotel
response.❓ How to make these dependent queries for rooms (
getRoom
) properly?Of course, I can do something like this:
but I would like to store these 2 requests separately because I use them in different places separately. Also, I'd like to have cached values by their separate keys as well.
It also would be cool to track loaders as two separate
isLoading
things (one for hotel and another for room(s)).Any help is highly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions