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โ
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 aQueryKeyas an array which is also strictly-typed โจ
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
options?: SetQueryDataOptions- Optional options to set the data in the cache
- See the TanStack queryClient.setQueryData ๐ด documentation for more details
Returnsโ
The TData from the updater or undefined if it returns nothing
Exampleโ
- Without
options - With
QueryKey - With
options
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',
});
It's also possible to use a QueryKey as an array instead of an object with {path, query, header}:
const pet = qraft.pet.getPetById.setQueryData(
[
{ method: 'get', url: '/pet/{petId}', infinite: false },
{ petId: 123 },
],
{ id: 123, name: 'Rex' }
);
const pet = qraft.pet.getPetById.setQueryData(
{ path: { petId: 123 } },
{ id: 123, name: 'Rex' },
{ updatedAt: Date.now() - 60_000 }
);