Skip to main content
Version: 2.x ๐Ÿšง

setQueryData(...)

The method enables direct access to the QueryClient cache to set the data for a specific Query. See the TanStack queryClient.setQueryData ๐ŸŒด documentation.

const data = qraft.<service>.<operation>.setQueryData(
parameters,
updater,
options
);

Argumentsโ€‹

  1. parameters: { path, query, header } | QueryKey
    • Required parameters to set the data in the Query Cache.
    • Instead of an object with {path, query, header}, you can pass a QueryKey as an array which is also strictly-typed โœจ
  2. updater: TData | (oldData: TData | undefined) => TData | undefined
    • Required updater for the cache data
    • If a non-function value is passed, the data will be updated to this value
    • If a function is passed, it will receive the old data value and is expected to return a new one
  3. options?: SetQueryDataOptions

Returnsโ€‹

The TData from the updater or undefined if it returns nothing

Exampleโ€‹

const parameters = { path: { petId: 123 } };

qraft.pet.getPetById.setQueryData(
parameters,
{ id: 123, name: 'Rex' }
);

const pet = qraft.pet.getPetById.getQueryData(parameters);

expect(pet).toEqual({
id: 123,
name: 'Rex',
});