Skip to content

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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