• Provides an execution function that can be used to run the given query.

    This hook does not run the query immediately and does not set up a sync subscription. Use this hook for running mutating queries and ad-hoc queries in response to user actions. Be aware that mutations will not be synced to other peers unless you also set up a SyncSubscription for the same query, which can be done with a useQuery hook.

    Query arguments can be supplied when setting up the hook and when calling the execution function. When query arguments contain the same key in both places, a shallow merge is performed, with the arguments passed to the execution function taking precedence. Below is an example of how to use this feature.

    To avoid overly complex types, partial query arguments are only allowed on the execution function. This means that the query arguments passed when setting up the hook must be complete.

    Errors that occur during query execution are stored in the error property of the return value and are reset on each subsequent execution. You can also provide an onError callback in the hook parameters or when calling the execution function to handle errors as they occur.

    Type Parameters

    • T = any

      The type of the items returned by the query. Be aware that this is a convenience type that is not checked against the query being run.

    • U extends DQLQueryArguments = DQLQueryArguments

      The type of query arguments.

    Parameters

    • query: string

      The query to run. Must be a non-mutating query.

    • Optional params: UseExecuteQueryParams<U>

      Additional parameters to configure how the query is run.

    Returns UseExecuteQueryReturn<T, U>

    Example: Basic Usage

    const [insertTask] = useExecuteQuery(
    'insert into tasks documents (:task)',
    {
    queryArguments: { task: { description: 'Buy milk' } },
    },
    )

    Example: Merging query arguments

    const [executeQuery] = useExecuteQuery(
    'insert into tasks documents (:task)',
    {
    queryArguments: { task: { author: 'Alice' } },
    },
    )

    // Clicking the button will execute the query with query arguments
    // `{ task: { author: 'Alice', description: 'Buy milk' } }`.
    return <button
    onClick={() => executeQuery({ task: { description: 'Buy milk' } })}
    >Add Task</button>