Skip to content

Migrating from v1 to v2

gpui-query v2 (the main crate since 0.1.1, when the rewrite replaced the v1 API now frozen in gpui-query-legacy) is an options-first redesign. The hooks keep the same names, but the call signatures, return types, and a number of internal behaviors changed. This page is a checklist for porting a v1 codebase to v2, grouped by what breaks. The current release is 0.3.0, and its changelog entry lists only fixes and additive changes, so every step here applies to 0.3.x unchanged.

Area v1 v2
Primary call style Positional args (key, cache, request, fetcher) Options-first (QueryOptions::new(key), fetcher)
use_mutation return Entity only (Entity, Subscription) tuple
Fetcher signal Optional Always receives a QuerySignal
Error interop QueryError without Error impl QueryError: Display + Error (? and anyhow)
Infinite max_pages Unbounded Defaults to Some(50)
Mutation GC Effectively a no-op Real GC, respects gc_time_ms
Status dedup Re-rendered on every mutation QueryObserver deduplicates by status

The biggest mechanical change is the call signature. v1 took the key, cache policy, and request policy as separate positional arguments. v2 takes a single QueryOptions (or a bare key string, which converts via Into).

// v1
// let entity = use_query("users", cache_policy, request_policy, fetcher, cx);
// v2
use gpui_query::hook::use_query;
use gpui_query::QueryOptions;
let (entity, _sub) = use_query(
QueryOptions::new("users")
.cache_policy(cache_policy)
.request_policy(request_policy),
|signal| async move { Ok::<_, gpui_query::QueryError>(vec![]) },
cx,
);

For the simplest case (a key with default policies), pass the string directly:

let (entity, _sub) = use_query("users", |signal| async move { Ok(vec![]) }, cx);

v2 hooks return (Entity<…>, Subscription). You must store the Subscription or the observation is dropped immediately and your view will not re-render on data changes.

struct MyView {
users: gpui::Entity<gpui_query::QueryResource<Vec<User>, MyError>>,
_subscription: gpui::Subscription, // keep this
}

The change is use_mutation, which returned a bare Entity in v1. use_query already returned (Entity, Subscription), and use_infinite_query returned a triple ending in the Subscription; v2 returns the pair and moves the refetch closure out to fetch_next_page_infinite. use_query_select returns a triple including a second subscription for the mapped observer; store both.

v2 fetchers always receive a QuerySignal. If you have long-running fetches, check signal.is_cancelled() periodically to bail out early when a newer request supersedes yours. For short fetches you can ignore it: the two-phase accept_current_request guard discards stale results automatically.

If you cannot change the fetcher signature, use_query_unsignalled keeps the Fn() -> Fut (no-signal) shape for backward compatibility. It is not the recommended default.

Mutations default to RetryPolicy::no_retries() in both v1 and v2, because silently retrying a destructive action is dangerous. What changed in v2 is the call shape: use_mutation now takes options first, and use_mutation((), cx) keeps the same no-retries, 5-minute GC defaults (see section 5). If your mutation is idempotent and you want retries, opt in explicitly:

use gpui_query::{MutationOptions, RetryPolicy};
use gpui_query::hook::use_mutation;
let (entity, _sub) = use_mutation(
MutationOptions::default().retry_policy(RetryPolicy::new(2)),
cx,
);

5. Mutation options moved into use_mutation

Section titled “5. Mutation options moved into use_mutation”

The old options-taking mutation hook, deprecated in 0.2.0, has been removed. use_mutation accepts MutationOptions via Into, so one hook covers both shapes:

// with options
let (entity, sub) = use_mutation(opts, cx);
// with defaults (no retries, 5 minute GC)
let (entity, sub) = use_mutation((), cx);

If you were still on the deprecated hook, switch to the opts form above; the call reads the same and nothing else changes.

v1 infinite queries accumulated pages without limit. v2 defaults to max_pages: Some(50) to prevent unbounded memory growth from deep scrolling. If you need unbounded pages, opt back in:

use gpui_query::hook::InfiniteQueryOptions;
let opts = InfiniteQueryOptions::new("feed").unbounded_pages();

Otherwise, decide on a cap that fits your UI (max_pages(n)).

v2 gives QueryError full Display + Error impls, so it composes with ? and anyhow. You can now return it from fallible helpers directly:

use gpui_query::QueryError;
fn lookup() -> Result<Vec<User>, QueryError> {
// ...
}

And use QueryError::sanitized() at the boundary where you convert server responses, so tokens and connection strings do not leak into logs or DevTools.

v1 re-rendered views on every internal mutation (including retry ticks). v2’s QueryObserver / MutationObserver deduplicates by status, so a retrying query no longer re-renders your view on every attempt; since 0.3.0 the query observers also re-render on data-only writes to an already-Success entry (set_query_data, hydrate, prefetch completion), so optimistic updates reach the view while retry ticks stay silent. MutationObserver still dedupes by status alone. You can remove manual “did the status actually change?” guards from your render logic.

  • QueryKey and the hierarchical key model are unchanged.
  • QueryKeyFilter (Exact / Prefix / All) is unchanged, but remove_queries dropped its cx argument: call it as client.remove_queries(&filter). invalidate_queries and reset_queries keep their v1 (filter, cx) signatures, and cancel_queries is new in v2.
  • RequestPolicy and its semantics are unchanged. CachePolicy is not: StaleWhileRevalidate gained a stale_ms field (v1 had only ttl_ms), so v1 constructions of that variant must add it, and v2 serves fresh data within ttl_ms, then stale data with a background revalidation until ttl_ms + stale_ms. v1 SWR never short-circuited; every fetch ran.
  • The QueryClient is still a GPUI Global, but the constructor changed: v1’s QueryClient::new(cache_policy, request_policy) becomes QueryClient::new() in v2, so the setup line reads cx.set_global(QueryClient::new()). Pass custom defaults via QueryClient::with_policies(cache_policy, request_policy).
  • Cancellation is still cooperative via QuerySignal; only the staleness guard is stricter (no more TOCTOU window).

If a v1 pattern is not covered here, the source of truth is the “gpui-query-v2 (Experimental Rewrite)” subsection of [0.1.0] and the [0.1.1] entry in the repository’s CHANGELOG.md, where the v2 redesign is itemized and the rewrite became the main crate; module doc comments no longer carry v2 change lists. Run cargo doc -p gpui-query --open for the item-level reference.