Skip to content

Caching individual queries within manual endpoint query #4828

Closed
@litera

Description

@litera

I have a query that uses queryFn in that does a 1+N request (N is very low hence I do it this way) in which I do something along these lines:

// suppose getDetails endpoint name
queryFn: async (id, api, extras, baseQuery) => {
    try {
        // the "1" fetch
        const collectQuery = await baseQuery({ ... })
        if (collectQuery.error) throw collectQuery.error;

        // the "N" fetches
        const resultQueries = await Promise.all(
            collectQueries.data.map(
                async (id) =>
                    await baseQuery({ ... })
            )
        );

        if (resultQueries.some(q => q.error)) throw resultQueries.find(q => q.error)!.error;

        return { data: resultQueries.map(q => q.data) };
    }
    catch(error) {
        return { error };
    }
}

This result is of course cached for each id parameter, but I would like it to cache the inner queries, because I do have endpoints of all inner queries: the collectQuery endpoint is also defined as well as the resultQueries endpoint for individual details fething.

Can I use existing endpoints somehow?

My API is created using createApi(...).enhanceEndpoints(...).injectEndpoints({ endpoints: ...})
I see the _api.dispatch that may be used for this maybe something similar to this:

api.dispatch(myApi.endpoints.getDetails.initiate(id)

But these aren't actual Promises, so I can't use them in the Promise.all suing Typescript.

What could be done to cache individual sub queries or use existing endpoints within the queryFn?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions