Skip to main content
Version: 2.x

ensureQueryData(...)

The ensureQueryData method retrieves cached data for a given query or fetches it if the data is not already available in the cache. This ensures that data is prepared and cached ahead of time, improving efficiency and performance.

To understand how ensureQueryData works, refer to the TanStack queryClient.ensureQueryData ๐ŸŒด documentation.

const result = api.<service>.<operation>.ensureQueryData({
parameters,
requestFn,
baseUrl,
revalidateIfStale: false,
...fetchQueryOptions,
});

Argumentsโ€‹

    • parameters: { path, query, header } | QueryKey | void
      • Required, OpenAPI request parameters for the query, strictly-typed โœจ
      • parameters will be used to generate the QueryKey
      • requestFn?: RequestFn
        • Optional, a function that will be used to execute the request
      • revalidateIfStale?: boolean
        • Optional, defaults to false. If true, stale data will be revalidated in the background while returning cached data immediately
      • baseUrl?: string
        • Optional, the base URL of the API
      • ...fetchQueryOptions?: FetchQueryOptions
        • Optional, represents the rest options of the fetchQuery(...) ๐ŸŒด method
        • queryOptions.queryFn could be provided instead of requestFn
        • queryOptions.queryKey could be provided instead of parameters

Returnsโ€‹

result: Promise<TData> - The result of the query execution