question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Local state management does not seem to work

See original GitHub issue

Hi,

We are currently struggling using Apollo Client to manage local state in a react application. We were able to build up a very simple example where we just display a div depending on the result of a mutation. The effect of the mutation should only be to display an alert message and return nothing.

We have come up with the following very simple example code:

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloClient } from 'apollo-client';
import { ApolloProvider } from 'react-apollo';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { withClientState } from 'apollo-link-state';

import App from './App';

const resolvers = {
  Mutation: {
    setData: (_, {myBool}) => {
      alert(myBool);
      return null;
    }
  }
};

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: withClientState({ resolvers })
});

ReactDOM.render(
  <ApolloProvider client={client}>
      <App />
  </ApolloProvider>,
  document.getElementById('root')
);

App.js:

import React, { Component, Fragment } from 'react';
import gql from 'graphql-tag';
import { Mutation } from 'react-apollo';

class App extends Component {

  setData = () => {
    alert('setData is called');
    const SET_DATA = gql`
      mutation SetData($myBool: Boolean!) {
        setData(myBool: $myBool) @client
      }
    `;
    return (
      <Mutation mutation={SET_DATA} variables={{ myBool: true }}>
        {(_, { loading, error }) => (
          <div>
            {loading && <p>...loading...</p>}
            {error && <p>ERROR ! Try reloading.</p>}
          </div>
        )}
      </Mutation>
    );
  };

  render() {

    return (
      <Fragment>
        <h1>App</h1>
        {this.setData()}
      </Fragment>
    );
  }
}

export default App;

Could someone please tell us why the alert method we call in the setData() resolver (from file index.js) isn’t called upon page loading?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jwdinkercommented, Apr 20, 2018

@comteharbour

check this out…this is how i did my auth in an example.

My saveUserDataLocal stores my tokens in Local Storage ( not the cache). Apollo cache is just a javascript object that gets nuked if you reload the page unless you persist it.

image

authenticateUser is my server response.

1reaction
jwdinkercommented, Apr 19, 2018

@zadigus The mutation component is a render props function. In order to trigger the mutation you have to call via the function that is passed down. Currently, your function that is being passed down isn’t doing anything (highlighted in yellow), therefore the mutation is never being triggered. Hope that clears it up.

2018-04-19_16-27-59

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local state management - Apollo GraphQL Docs
Learn how to work with your local data in Apollo Client.
Read more >
local state does not seem to exist before it has been queried
It looks like state is not initialized before it has been queried once, even though withClientState provides - according to the readme -...
Read more >
Why we decided against GraphQL for local state management
Imagine your application's local state: it really is just another source of data, after all, isn't it?
Read more >
4 ways to handle local state when using Apollo and GraphQL
Most cases for local state management can be covered by using the context API or component state if you don't want to migrate...
Read more >
Local State Management with Apollo Client
My “pause” button is the first use case that actually requires an application state, so it's time to think about state management.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found