Upgrade to Pro — share decks privately, control downloads, hide ads and more …

State Management with Apollo GraphQL

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for adamTrz adamTrz
August 27, 2018

State Management with Apollo GraphQL

Manage your Application State with Apollo GraphQL without a hassle.

Avatar for adamTrz

adamTrz

August 27, 2018

Other Decks in Programming

Transcript

  1. ... import ApolloClient from 'apollo-boost'; ... const client = new

    ApolloClient({ uri, clientState, }); Set up
  2. const clientState = { defaults: { cart: [], }, resolvers:

    { Mutation: { addItemToCart: (_, { id, value }, { cache }) => { const previous = cache.readQuery({ query: CART_QUERY }); const data = {...}; cache.writeData({ data }); return null; }, ... Set up
  3. export const CART_QUERY = gql` query GetCart { cart @client

    { id value } } `; export const ADD_ITEM_TO_CART = gql` mutation addItemToCart($id: ID!, $value: number!) { addItemToCart(id: $id, value: $value) @client } `; Usage
  4. Useful links • The Future of State Management by Peggy

    Razis
 http://bit.ly/apollo_medium • Apollo-link-state - Let’s manage state with queries
 http://bit.ly/apollo_yt • Docs! 
 http://bit.ly/apollo_docs