title: Relay: Cache Restore Failed remounts component
body: <p>I have a Node application built with React/Relay/GraphQL. One of my components requires a call to <code>relay.setVariables</code> as input changes, and I have run into the following problem:</p>
  
  <p>The user changes input, and setVariables is called. Due to setVariables killing the component state, I set the state value in the ReadyStateChange callback:</p>
  
  <pre><code>setVariables({
      something: value,
  }, state =&gt; {
      if (state.done) {
          this.setState({
              value,
          });
      }
  });
  </code></pre>
  
  <p>When this is called for the first time, the following ready state changes occur:</p>
  
  <ul>
  <li><code>NETWORK_QUERY_START</code></li>
  <li><code>CACHE_RESTORE_START</code></li>
  <li><code>CACHE_RESTORE_FAILED</code></li>
  <li><code>ABORT</code></li>
  </ul>
  
  <p>This is followed by the component <em>unmounting</em>, and then <em>re-mounting</em>.</p>
  
  <p>After digging into <a href="https://github.com/facebook/relay/blob/17096c3f22fbd98d0b4ace26762917e6c6a54832/src/legacy/store/GraphQLQueryRunner.js" rel="nofollow noreferrer">GraphQLQueryRunner</a>, it seems this relay query failed because the <em>pendingFetch</em> is added to the <em>remainingRequiredFetchMap</em>. It then begins a cache restore, checks to see if there is sufficient data to satisfy the query, and in this case there isn't so the cache restore fails.</p>
  
  <p>At this point, if the input is deleted and is then changed to the same value again, the data is added to the store successfully. If the input is different, it fails again. So it seems the data is successfully returned by from the GraphQL query and added to the store -- it's just a little too late.</p>
  
  <p>So my main question here is <strong>why is the query failing, even though GraphQL returns the desired data, and why is it remounting my component?</strong></p>
  
  <p><em>Note: I do use relay.setVariables in other components, and some of them also fail, however the components are not force re-mmounted, which I think is the main issue in this case.</em></p>
  
  <p>Any insight is appreciated</p>