How to Use Suspense and GraphQL with Apollo to Build Great User Experiences

Rate this content
Bookmark

For many app developers, GraphQL is instrumental in building great user experiences. With the introduction of React Suspense, developers have an ergonomic way to orchestrate loading states to improve upon the status quo. 


In this talk, the Apollo Client team will show you how we built a non-trivial application using Apollo Client’s new Suspense features, GraphQL features like the @defer directive, and how to combine them to build great user experiences in your own apps.

FAQ

Apollo Client's new suspense feature, released in version 3.8, integrates with React 18 Suspense to manage and coordinate transitions in applications smoothly. It provides a more seamless user experience by handling UI states during data fetching more effectively.

Using React Suspense with Apollo Client helps in reducing unnecessary loading states and coordinating UI transitions smoothly. It improves the overall user experience by minimizing the jarring effects of data fetching and rendering in a web application.

Use Suspense Query is a hook introduced in Apollo Client 3.8 that replaces the traditional useQuery hook for fetching data. It is designed to work with React 18 Suspense and does not manage loading states directly within components, instead relying on React Suspense boundaries to handle loading UIs.

Implementing suspense support in a data fetching library is complex because it involves intricate management of loading states, error handling, and UI transitions. This complexity arises from the need to seamlessly integrate these elements while maintaining performance and user experience.

Apollo Client's suspense feature handles transitions by using React 18's support for suspense and transitions. This allows developers to define suspense boundaries in their applications that manage how components wait for data and transition new views into the UI.

The defer directive in GraphQL allows parts of a query to be fetched and rendered asynchronously. This improves performance by allowing initial parts of the UI to load without waiting for slower parts of the data fetch, thus reducing perceived load times and improving user experience.

Apollo Client uses React's Start Transition feature to mark updates as low priority. This allows the application to maintain the existing UI while new data is fetched in the background, improving the user experience during interactions like infinite scrolling or loading additional data.

Jerel Miller
Jerel Miller
Alessia Bellisario
Alessia Bellisario
27 min
15 Nov, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's Talk is about using suspense and GraphQL with Apollo Client to create smooth user experiences. The Talk covers the introduction and power of suspense, the new Used Suspense Query hook in Apollo Client, updating components with suspense, using suspense boundaries and tradeoffs, optimizing playlist fetching with GraphQL, loading more tracks and using transitions, and concluding remarks and future developments.

1. Introduction to Suspense and Apollo Client

Short description:

Today we'll be talking about how to use suspense and GraphQL with Apollo Client to build great user experiences. We'll start with an overview of React Suspense concepts and then explore a real client rendered application using Apollo Client's new suspense hooks. We won't cover React Server Components and Streaming SSR, but you can check out our experimental package for more information. Andrew Clarke's tweet about iOS transitions sets the stage for the relevance of suspense. Suspense helps coordinate transitions in our app for a smoother user experience. We'll also discuss the use query hook in Apollo Client.

Hi, everyone. So today we'll be talking about how to use suspense and GraphQL with Apollo Client to build great user experiences. But first, let's quickly introduce ourselves. So I'm Gerald Miller. I'm a principal software engineer at Apollo GraphQL, working as a maintainer on Apollo Client. And you can find me online at Gerald Miller. And my name is Alessia Bellacerio. I'm a staff software engineer also working on Apollo Client, and you can reach me at AlessBell.

So first we'll be starting with what this talk is about. And we'll begin with a brief overview of React Suspense concepts. Then we'll look at a real client rendered application that's using Apollo Client's new suspense hooks, officially released in August of this year, to see what can be gained from pairing these technologies. And also importantly, let's talk about what we're not going to talk about today, which is React Server Components and Streaming SSR. While super cool, and a lot of great things happening there, just not something we have time to touch on. If you are interested in those, we highly encourage you to check out our experimental package at Apollo Experimental of XJS App Support. Shout out to our co-maintainer, Lenz, who has done a lot of the legwork in this. Also, we're not going to be talking about implementing suspense support in a data fetching library, because trust us, it's actually a little more complicated than meet-CI.

Okay, so let's go back to January 2018 when Andrew Clarke tweeted this out. Andrew Clarke is a member of the core React team, who's currently working at Percel. And this tweet was sent out into the universe January 2018, as I said, more than a month before Dan Abramoff would deliver his JSConf Iceland talk titled, Beyond React 16, with some of the first public demos of suspense. And I think this is a good place to start, because what Andrew is talking about here is very relevant to suspense and what we'll be showing you. Andrew said, one reason iOS feels so much nicer than the web, fewer unnecessary loading states. Look what happens when you tap an option in the settings app in iOS. Instead of transitioning the view immediately and showing a fallback spinner for a split second, it pauses for a moment, prepares the view. When the view is ready, it slides in. And it just feels so much smoother than if you would see a flash of a spinner. So, this is one of the things that Suspense helps us do, coordinate these transitions in our app for a really, really nice end user experience. First, let's talk a little bit about the status quo today. So, for those of you that have used Apollo Client for any amount of time, you should be familiar with the use query hook. So, we're going to start taking a look at just an example that uses use query, that should look familiar to all of us here. So, with a hook like use query, you're fetching a list of albums, and the important thing to note here is that the use query hook is going to give you this loading boolean.

2. Understanding the Power of Suspense

Short description:

Our albums component renders the loading fallback until data is returned from the network request. Suspense is more than just showing a different fallback UI. It provides powerful tools for managing transitions and creating a smooth user experience.

And so, our albums component that is going to be rendered when we click this button here is going to be responsible for rendering the loading fallback until the data is returned from our network request, and we see that dreaded flash of a spinner here.

One thing before we look at some practical examples, dive into a demo, that we wanted to note here is that we're going to be talking a lot about loading states in this talk. But to reduce suspense to just, you know, this notion that suspense is just about showing and a different way of showing your fallback UI in your app, would miss the core value prop of suspense entirely.

And so that's another takeaway that we hope you have today. That suspense gives us these really powerful tools for managing and coordinating transitions in our app, beyond just the initial loading experience or the UX on navigation, et cetera, but it's really just these transitions that are so fundamental to how the user is experiencing your app and making those as pleasant, as smooth as possible.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
In this talk, we'll look at some of the modern options for building a full-stack React and GraphQL app with strong conventions and how this can be of enormous benefit to you and your team. We'll focus specifically on RedwoodJS, a full stack React framework that is often called 'Ruby on Rails for React'.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
Though GraphQL is declarative, resolvers operate field-by-field, layer-by-layer, often resulting in unnecessary work for your business logic even when using techniques such as DataLoader. In this talk, Benjie will introduce his vision for a new general-purpose GraphQL execution strategy whose holistic approach could lead to significant efficiency and scalability gains for all GraphQL APIs.

Workshops on related topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.