由于本地缓存,使用中继搜索不包含新结果

Search with Relay doesn't include new results due to local cache

我已经在 React 和 Relay 中实现了一个“输入即搜索”的组件。它与

的设置大致相同

它按预期工作,但有一个例外。当我重新输入已经在客户端执行过的搜索时,服务器的新结果永远不会出现。在这种情况下,我看起来 Relay 总是去本地缓存。

因此,例如,假设我搜索了 'foo' 但没有找到任何结果。现在,几秒钟后,网站上的另一个用户创建了这个 'foo',但 Relay 永远不会查询服务器,因为对 'foo' 搜索的缓存响应是一个空结果。

是否有适用于此场景的模式或最佳实践?

查询如下。我调用 this.props.relay.setVariables 来执行搜索:

initialVariables: {
    search: '',
    hasSearch: false
},
fragments: {
    me: () => Relay.QL`
        fragment on Viewer {
            relationSearch(search: $search) @include(if: $hasSearch) {
                ... on User {
                    username
                }
            }
        }
    `
}

答案似乎是将 this.props.relay.forceFetch 与搜索变量一起使用。

https://facebook.github.io/relay/docs/api-reference-relay-container.html#forcefetch

如果这不是最佳做法,请有人纠正我。