⚡️ Pro Feature ⚡️ This feature is bundled with GraphQL-Pro.

Overview

@defer is a directive for streaming GraphQL responses from the server to the client.

By streaming the response, the server can send the most critical (or most available) data first, following up with secondary data shortly afterward.

@defer was first described by Lee Byron at React Europe 2015 and got experimental support in Apollo in 2018.

@defer requires the new interpreter runtime which ships with GraphQL-Ruby 1.9+.

Example

GraphQL queries can be large and complex, requiring lots of computation or dependencies on slow external services.

In this example, the local server maintains an index of items (“decks”), but the item data (“cards”) is actually hosted on a remote server. So, GraphQL queries must make remote calls in order to serve that data.

Without @defer, the whole query is blocked until the last field is done resolving:

https://user-images.githubusercontent.com/2231765/53442028-4a122b00-39d6-11e9-8e33-b91791bf3b98.gif

But, we can add @defer to slow fields:

  deck {
    slots {
      quantity
-     card
+     card @defer {
        name
        price
      }
    }
  }

Then, the response will stream to the client bit by bit, so the page can load progressively:

https://user-images.githubusercontent.com/2231765/53442027-4a122b00-39d6-11e9-8d7b-feb7a4f7962a.gif

This way, clients get a snappy feel from the app even while data is still loading.

View the full demo at https://github.com/rmosolgo/graphql_defer_example.

Considerations

Next Steps

Set up your server%20to%20support%20%60@defer%60%20or%20read%20about%20%20%5Bclient%20usage%5D(/defer/usage) of it.