Skip to content

Add ability to easily update query cache #390

Open
@nickzelei

Description

Hey all, awesome project! This is allowing me to easily switch away from swr over to using tanstack/query.

One thing I miss however is the ability to easily update the query cache.

With swr, each instance has a mutate function that includes the curried key, so I can easily pass it the new value.
With the current iteration of this library, the refetch function that is returned from useQuery takes no parameters.

In order for me to update the underlying cache, I have to do something like this:

const queryclient = useQueryClient();
const { mutateAsync } = useMutation(createMyValue);

async function onSubmit(values: MyFormValues): Promise<void> {
  const resp = await mutateAsync(...values...);
  queryclient.setQueryData(
    createConnectQueryKey(geyMyValue, {
      id: resp.myvalue.id,
    }),
    new GetMyValueResponse({
      myvalue: resp.myvalue,
    })
  );
}

I'd love it if there were an easier way to trigger a cache update for a useQuery value that didn't involve so much boilerplate.

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