API
React
useTransactionQuery

useTransactionQuery

A wrapper hook for useQuery(), designed for users familiar with the Apollo Client.

export default function MyComponent() {
  const { data, error, isLoading } = useTransactionQuery(
    (query) => ({
      foo: query.foo,
      bar: query.bar({ baz: 1 }),
    }),
    {
      skip: false,
    }
  );
 
  return (
    <div>
      {isLoading && <p>Loading...</p>}
      {error && <p>Error: {error.message}</p>}
      {data && (
        <div>
          <p>Foo: {data.foo}</p>
          <p>Bar: {data.bar}</p>
        </div>
      )}
    </div>
  );
}

Options

cachePolicy

The cache policy to use for the current query, defaults to default. You can read more about cache policy options in Concepts.

fetchPolicy

Legacy option kept for backward compatability, ignored when cachePolicy is in use.

ValueDescription
cache-firstTranslates to the cache policy force-cache.
cache-and-networkTranslates to the cache policy default.
network-onlyTranslates to the cache policy no-cache.
no-cacheTranslates to the cache policy no-store.

skip

Whether to skip the query, defaults to false.

pollInterval

Frequency in milliseconds for refetching the query, skip this option to disable.

pollInBackground

Whether to poll while the app is in background, defaults to false.

notifyOnNetworkStatusChange

Updates loading state or triggers suspense during refetch, does not affect initial fetch.

Defaults to true.

variables

Optional query variables to be supplied to the query function.

onCompleted

Optional callback for when a query fetch is successful.

(data: TData) => void;

onError

Optional callback for when a query fetch fails.

(error: GQtyError) => void;

operationName

Optional GraphQL operation name, useful for debugging and testing.

retry

Failing queries can be automatically retried, this option allows you to customize the retrying behavior, or disabling it altogether.

Retry PolicyDescription
trueEnables auto retry with the default options.
falseDisables auto retry.
numberEnables auto retry with the default options and a custom maxRetries count.
{ maxRetries, retryDelay }Enables auto retry with custom options.

When you specify retry as an object, the following properties are available.

maxRetries

The maximum number of times to retry a failed query, defaults to 3.

retryDelay

Retry DelayDescription
numberA fixed number of milliseconds to wait between retries.
(attempt: number) => numberA function that returns the number of milliseconds to wait between retries, the default is an exponential backoff function: min(2x,30)1000msmin({2^x}, 30) * 1000\text{ms}.

suspense

Use true to enable Suspense mode.

Return Value

The return value is an object with the following properties.

data

The data returned by the query function, only available after a successful fetch.

error

The error returned by the query function, only available after a failed fetch.

isCalled

Whether the query function has been called.

isLoading

Whether the query function is currently fetching.