简体   繁体   English

forceFetch Relay容器中的特定片段

[英]forceFetch a specific fragment from a Relay container

If a container has multiple fragments, how can I forceFetch only one of them ? 如果容器有多个片段,我怎么能forceFetch只获取其中一个?

Relay.createContainer(MyContainerComponent, {
    fragments: {
        fragment1: () => Relay.QL`my first fragment`,
        fragment2: () => Relay.QL`my second fragment`
    } 
});

According to the docs, calling this.props.relay.forceFetch() will update every fragments associated to the container. 根据文档,调用this.props.relay.forceFetch()将更新与容器关联的每个片段。

While force fetching 'as is' does the job, the whole point of Relay is to avoid over/under fetching. 虽然按原样“按原样”取力,但是接力的全部要点是避免过度/不足。

Any advice ? 有什么建议?

Refetches are managed by the container, so one solution would be: 重新获取由容器管理,因此一个解决方案是:

Relay.createContainer(MyContainerComponent, {
    fragments: {
        fragment1: () => Relay.QL`fragment on SomeThing { ${SomeThingContainer.getFragment("something")}`,
        fragment2: () => Relay.QL`fragment on OtherThing { ${OtherThingContainer.getFragment("otherthing")}`
    } 
});

Then just use the two sub containers to refetch their individual fragments. 然后只需使用两个子容器来重新获取它们各自的片段。

If you actually need data from both fragments in MyContainerComponent so you can't separate those into sub-containers, refetching the full container fragment makes sense for me... 如果你真的需要MyContainerComponent两个片段的数据,所以你不能将它们分成子容器,重新获取完整的容器片段对我来说是有意义的...

However, if you really need data from both fragments and you really only want to refetch one of them, you can always use the imperative Store API to execute an ad-hoc query for whatever you want. 但是,如果您确实需要来自两个片段的数据并且您真的只想重新获取其中一个片段,那么您始终可以使用命令式Store API来执行任何您想要的即席查询。 See this question for details on that. 有关详细信息,请参阅此问题

ASIDE: 在旁边:

the whole point of Relay is to avoid over/under fetching 接力的全部意义在于避免过度/不足

I disagree... That's a welcome benefit of course, but the real "point" is to be a declarative framework for getting data to your components. 我不同意......当然,这是一个受欢迎的好处,但真正的“要点”是成为向组件提供数据的声明性框架。 There is no simple, declarative API for refetching individual container fragments, true. 没有用于重新获取单个容器碎片的简单声明性API,true。 Maybe there should be! 也许应该有! But I think it's probably more likely that you are prematurely optimizing here :) 但我认为你可能更有可能在这里过早优化:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM