On this page
← All product updates

@defer Support in Hive Router

Kamil Kisiela
Kamil Kisiela

Hive Router v0.3.23 supports @defer. Mark the slow part of an operation with it, and the client gets the rest of the response first, without waiting for the slowest subgraph.

query {
  product(id: "1") {
    name
    price
    ... @defer {
      reviews {
        rating
        body
      }
    }
  }
}

The router sends name and price as soon as they’re in. reviews follows in its own part once the reviews subgraph answers. With several @defers, a fast fragment doesn’t wait for a slow one.

It’s off by default:

router.config.yaml
defer:
  enabled: true

How it works

  • Real deferred fetches. Deferred fields get subgraph fetches of their own, which start as soon as their inputs are in and never hold back the primary part. That also works for fields of a subgraph the primary part already calls, when the fragment is on an entity: the router goes back to that subgraph through the type’s @key.
  • Every transport. Responses go out over multipart/mixed, SSE or WebSockets, in the incrementalSpec=v0.2 format (pending, incremental, completed). Clients that ask for multipart/mixed;deferSpec=20220824, like Apollo Client 3, get the older format.
  • @defer(if:) is planned once for both values. if: false gives a regular response.
  • Safe nulls. A null from a deferred fragment can’t take away data another part already delivered. If it would, the fragment fails on its own, with its errors and no data.
  • Limits. limits.max_defers caps the @defers in one operation (4 by default), and traffic_shaping.router.request_timeout covers every part.

Plugins, authorization, usage reporting, metrics, tracing and demand control all work with deferred responses.