setQueryData(...)
The method enables direct access to the QueryClient
cache to set the data for a specific Query.
See the TanStack queryClient.setQueryData ๐ด documentation.
- Without
options
- With
options
const data = qraft.<service>.<operation>.setQueryData(
parameters,
data,
queryClient
);
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 aQueryKey
as an array which is also strictly-typed โจ
data: TData
- Required data to set in the cache
queryClient: QueryClient
- Required
QueryClient
instance to use
- Required
const data = qraft.<service>.<operation>.setQueryData(
parameters,
data,
options,
queryClient
);
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 aQueryKey
as an array which is also strictly-typed โจ
data: TData
- Required data to set in the cache
options: SetQueryDataOptions
- Optional options to set the data in the cache
- See the TanStack queryClient.setQueryData ๐ด documentation.
queryClient: QueryClient
- Required
QueryClient
instance to use
- Required
Returnsโ
The data from the updater or undefined
if it returns nothing, strictly-typed โจ
Exampleโ
- Without
options
- With
options
- With
QueryKey
const parameters = { path: { petId: 123 } };
qraft.pet.getPetById.setQueryData(
parameters,
{ id: 123, name: 'Rex' },
queryClient
);
const pet = qraft.pet.getPetById.getQueryData(
parameters,
queryClient
);
expect(pet).toEqual({
id: 123,
name: 'Rex',
});
const pet = qraft.pet.getPetById.setQueryData(
{ path: { petId: 123 } },
{ id: 123, name: 'Rex' },
{ updatedAt: Date.now() - 60_000 },
queryClient
);
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}' },
{ petId: 123 },
],
{ id: 123, name: 'Rex' },
queryClient
);