WhoBuilt

React Query with GraphQL code gen and Typescript

Oct 21, 2021· 59 min· 2.6K views·Watch on YouTube

Speaker

Arnaud Bezançon, an entrepreneur and CTO, presents a React Native data-fetching approach using React Query, GraphQL Code Generator, and TypeScript at React Native EU 2021 with Callstack.

A responsive data-fetching stack

Bezançon demonstrates an Expo and TypeScript app using React Query with graphql-request, GraphQL Code Generator, and a Hasura API backed by Postgres and hosted with Nhost. Its stale-while-revalidate behavior shows cached movie data immediately while fresh data loads in the background.

Generate typed queries and hooks

GraphQL Code Generator uses a schema endpoint, query files, and plugins to produce TypeScript types and React Query hooks. Bezançon recommends regenerating the output when the schema or operations change, then wrapping generated hooks in custom hooks to keep component code focused.

Shape loading and refresh behavior

The demo uses initial data from a movie list to show title and rating while fetching fuller details. Bezançon also covers refresh on app focus, reconnecting after offline periods, screen focus, and polling; polling can be set per query or globally, with care for energy use.

Mutations, lists, and persisted cache

For rating changes, the app updates the UI optimistically, saves previous data for rollback, and invalidates the movie list after success. Infinite lists use React Query's useInfiniteQuery with FlatList pagination, while cache persistence in AsyncStorage is demonstrated through experimental React Query features and an app-version cache purge.

Compare approaches and apply lessons

Bezançon contrasts universal query caching tools such as React Query with GraphQL state frameworks such as Relay, Apollo Client, and urql, describing tradeoffs in flexibility, schema rules, and cache behavior. His recommendations include starting with code generation, using custom hooks and a single source of truth, testing under poor network conditions, and monitoring queries.

Takeaways

  • Generate GraphQL types and React Query hooks from the schema and operation files to reduce manual upkeep.
  • Use initial data to show list information immediately while fetching richer screen details.
  • For optimistic mutations, save prior data so errors can roll the UI back, then invalidate affected queries after success.
  • Test loading, errors, and optimistic updates under slow or unreliable network conditions.
  • Choose between general query caching and GraphQL state frameworks based on team needs and desired schema constraints.

Mentioned in this talk

Search inside this talk

What was claimed 13

Show all 13 facts

Related talks