Skip to content

Allow access to queryClient / variables in initialData / placeholderData fns #73

@V1OL3TF0X

Description

@V1OL3TF0X

Summary

Small change, but imo with great DX: inject variables and queryClient into initialData / placeholderData functions.

Motivation

With usual REST API structure, it's not uncommon to have this type of endpoints:
/teams - gets all teams
/teams/${id} - gets specific team data
With that, I found it useful to do this query structure

export const useAllTeams = () => useQuery({
    queryKey: ['teams'],
   queryFn: () => client.get('/teams'),
});

export const useTeam = (id: string) => {
    const queryClient = useQueryClient();
    return useQuery({
        queryKey: ['teams', { id }],
        queryFn: () => client.get(`/teams/${id}`),
        initialData: () => queryClient.getQueryData(['teams']).find(team => team.id === id),
    })
}

Unfortunately, with how router / createQuery are currently implemented, there's no way to recreate that.

Is there some room to create / approve such change? I could implement it and submit a PR, if you see it fit the package

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions