WhoBuilt

GQL all the Things!

Sep 14, 2018· 20 min· 1.0K views·Watch on YouTube

Speaker

Sara Vieira, a developer advocate at Wilde, explains how to manage local state with Apollo at a Callstack talk.

Why Sara chose Apollo for local state

Sara describes moving from Redux, whose boilerplate frustrated her, to MobX and then Apollo. She introduces Apollo Client State as a way to manage local state using the GraphQL tools developers already know.

Set up defaults and resolvers

Sara shows an Apollo Boost setup with client state defaults and resolvers. Her example stores whether a modal is open, with a type name for the cached object. A resolver reads or writes that data through Apollo’s cache.

Query and update the cache

Sara uses the @client directive to tell Apollo to read data from the cache instead of the server. She demonstrates querying the modal state and using Query and Mutation components to open or close it. For small changes, she also shows writing directly to the cache with Apollo Consumer.

Apollo updates and tooling

Sara describes Apollo changes available or planned at the time, including custom cache support for server-side rendering and Apollo CLI generation of TypeScript types for queries and mutations. She also discusses opt-in schema validation as a development aid that avoids adding its bundle cost in production.

Takeaways

  • Use Apollo’s cache for simple local state when it keeps data access consistent with the rest of an Apollo app.
  • Give cached objects type names to help Apollo manage its cache.
  • Use @client to mark fields that Apollo should resolve locally.
  • Wrap repeated loading and error handling in a reusable query component.
  • Reserve direct cache writes through Apollo Consumer for small updates such as toggling a value.

Mentioned in this talk

Search inside this talk

What was claimed 9

Show all 9 facts

Related talks