Skip to content

How to queryClient.invalidateQueries in hey-api/openapi-ts #980

@sagardwivedi

Description

@sagardwivedi

Description

I have implemented the following code:

const profileMutation = useMutation({
    ...updateUserMutation(),
    onSuccess: () => {
        toast({
            title: "Success",
            description: "Profile Updated"
        });
    },
    onError: (error) => {
        const errorMessage = getErrorMessage(error);

        toast({
            title: "Something went wrong",
            description: errorMessage,
            variant: "destructive",
        });
    },
    onSettled: () => {
        queryClient.invalidateQueries({ queryKey: ["readUser"] });
    }
});

I am encountering an issue determining the queryKey for readUser. The readUserOptions function is defined as follows:

export const readUserOptions = (options?: Options) => {
    return queryOptions({
        queryFn: async ({ queryKey }) => {
            const { data } = await readUser({
                ...options,
                ...queryKey[0],
                throwOnError: true,
            });
            return data;
        },
        queryKey: [createQueryKey("readUser", options)],
    });
};

The queryKey is generated by a function and it seems cumbersome to manually specify it for invalidateQueries. Could you advise on the best approach to handle this scenario?


Let me know if there's anything else you need!

Reproducible example or configuration

https://stackblitz.com/edit/hey-api-client-fetch-tanstackreact-query

OpenAPI specification (optional)

No response

System information (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🔥Broken or incorrect behavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions