Follow-along: Using @defer
We've isolated our problem—fortunately, there's a way to break apart the really slow fields from everything else. This mechanism is called @defer
.
The router has this built-in capability to handle fields in a query that resolve more slowly than the rest. Using the @defer
directive, we can specify which fields to defer—meaning that our query doesn't have to wait on the slowest pieces before it can return some data.
🎯 Goal: Improve the client experience by using @defer
.
To be deferred, a field needs to meet one of the following requirements:
- It's a root field of the
Query
type - It's a field of any entity type
Try running the operation below in Explorer.
query GetRecipePage {recipe(id: "recOZrH0RhjSjATBp") {idnamecookingTimeprepTimereadyTimeservingsinstructionsingredients {text}}recentlyAddedRecipes {namecookingTimeservings}}It takes a few seconds for the data to come back.
Place your cursor on
recentlyAddedRecipes
, then right-click and select "Wrap with inline@defer
fragment"Run the query again and check out the Response Timeline.
The Response Timeline lets us visualize how long it took for the various pieces of our query to resolve and send data back to us. We can see that by adding @defer
to the recentlyAddedRecipes
field, it no longer held up the rest of our query that was ready to return data much sooner.
Share your questions and comments about this lesson
Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.