Search code examples
relayrelaymodern

Force refetch in Relay Modern RefetchContainer with no (new) variables


I'm trying to find out how/if it is possible to trigger a refresh in a Relay Modern RefreshContainer without passing (new) variables?

I’m looking for the best way to implement the good ol’ pull-to-refresh on a React Native list, that should simply refetch the original query - no variables needed?

According to docs (https://facebook.github.io/relay/docs/api-cheatsheet.html) this should be possible using

this.props.relay.refetch({}, callback, {force: true})

but I get an error saying "undefined is not an object ('evaluating taggedNode.modern')"

enter image description here

The query works just fine if I use a plain old FragmentContainer instead, but I'd just like a simple pull-to-refresh functionality :-)

EDIT Adding more code for clarity. Also updated call to reflect change to API that includes render variables, passing null

class HistoryList extends React.PureComponent<void, Props, State> {
  state = { refreshing: false };

  _renderRow = ({ item }) => {
    return <HistoryListItem item={item.node} />;
  };

  _renderHeader = ({ section }) => {
    return (
      <Text style={[cs.breadText, _styles.sectionHeader]}>
        {section.title}
      </Text>
    );
  };

  _onRefresh = () => {
    this.setState({ refreshing: true });
    this.props.relay.refetch({}, null, this._onRefreshDone(), { force: true });
  };

  _onRefreshDone = () => {
    this.setState({ refreshing: false });
  };

  _sortDataIntoSections = (edges: Array<Node>) => {
    return _.chain(edges)
      .groupBy(element => {
        return moment(element.node.huntDate).format('MMMM YYYY');
      })
      .map((data, key) => {
        return { title: key, data: data };
      })
      .value();
  };

  render() {
    return (
      <View style={_styles.container}>
        <SectionList
          renderSectionHeader={this._renderHeader}
          sections={this._sortDataIntoSections(
            this.props.entries.allJournalEntries.edges
          )}
          renderItem={this._renderRow}
          keyExtractor={(item, index) => item.node.__id}
          onRefresh={this._onRefresh}
          refreshing={this.state.refreshing}
        />
      </View>
    );
  }
}

export default createRefetchContainer(
  HistoryList,
  graphql`
    fragment HistoryList_entries on Viewer {
      allJournalEntries(orderBy: huntDate_DESC) {
        count
        edges {
          node {
            huntDate
            ...HistoryListItem_item
          }
        }
      }
    }
  `
);

Solution

  • A solution has been found by robrichard at github.

    I was missing the third argument for the RefetchContainer, which is the query to execute on refetch. This, combined with the suggestion from @zetavg was what was needed.

    The exported module now looks like this:

    export default createRefetchContainer(
      HistoryList,
      {
        entries: graphql`
          fragment HistoryList_entries on Viewer {
            allJournalEntries(orderBy: huntDate_DESC) {
              count
              edges {
                node {
                  huntDate
                  ...HistoryListItem_item
                }
              }
            }
          }
        `
      },
      graphql`
        query HistoryListRefetchQuery {
          viewer {
            ...HistoryList_entries
          }
        }
      `
    );