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

State management in a GraphQL era [5 min version]

Avatar for Kitze Kitze
December 06, 2017

State management in a GraphQL era [5 min version]

External state management frameworks like Redux and MobX definitely helped with the problem of state-management, especially when the data is fetched from a REST endpoint.

But now that we're using GraphQL to take care of the data, is an external state-management library even needed?

Can GraphQL coexist with the other state management frameworks? What about server-side rendering? What are the pros and cons of each combination?

We'll find out what's the best way to approach this in 2017.

Avatar for Kitze

Kitze

December 06, 2017
Tweet

Other Decks in Programming

Transcript

  1. TL;DR GraphQL is the thing that’s eventually gonna replace REST,

    but you keep telling yourself that you don’t need to learn it.
  2. GraphQL query { currentUser { activity { posts { title

    dateCreated } } friends { name avatarUrl activity { likedPosts { id imageUrl } } } } }
  3. jQuery vs MooTools Angular vs Ember GraphQL vs Rest Redux

    vs MobX React vs Vue Apollo vs Relay Hillary vs Trump
  4. Mistake: Asking ❓what’s better❓ instead of ✅ what’s suitable for

    my app ✅ what’s suitable for my team ✅ what’s suitable for our use-case
  5. const fetchTodos = () => dispatch => { dispatch({type: 'FETCH_TODOS_INIT'});

    api.fetchTodos().then(todos => { dispatch({type: 'FETCH_TODOS_SUCCESS', todos}); }).catch(error => { dispatch({type: 'FETCH_TODOS_FAIL', error}) }) }; Redux - async action
  6. const todosReducer = ( state = {loading: false, todos: [],

    error: null}, action ) => { switch (action.type) { case 'FETCH_TODOS_INIT': { return { ...state, loading: true}; } case 'FETCH_TODOS_SUCCESS': { return { ...state, loading: false, todos: action.todos}; } case 'FETCH_TODOS_FAIL': { return { ...state, loading: false, error: action.error}; } } }; Redux - reducer
  7. class App { @observable todos = []; @observable loading =

    false; @observable error = null; @action fetchTodos = () => { this.loading = true; api.fetchTodos().then(todos => { this.todos = todos; this.loading = false; }).catch(error => { this.error = error; this.loading = false; }); } } MobX
  8. @graphql(gql` query { todos { title date } } `)

    class Todos extends Component { render(){ const {data: {todos}} = this.props; <ul> {todos.map(todo => <li> {todo.title} - {todo.date} </li> ) </ul> } } Apollo
  9. When using GraphQL, in 90% of the cases, you won’t

    need Redux, MobX, RxJS or *insert hipster state-management library here*
  10. If you still decide to use one, you just really

    like over-engineering ¯\_(ツ)_/¯