简体   繁体   English

QueryRenderer在Relay Modern中的作用?

[英]Role of QueryRenderer in Relay Modern?

So, first a bit of background . 所以,首先是一些背景知识 I'm a native iOS/Android developer who is now starting my first ever React Native project. 我是一名原生的iOS / Android开发人员,现在正在开始我的第一个React Native项目。 It comes with all the benefits and pains of Javascript, but I like it a lot so far :-) I decided to also try my hand at GraphQL for the first time as well. 它带来了Javascript的所有好处和痛苦,但到目前为止我非常喜欢它:-)我还决定第一次尝试使用GraphQL。

Being new to the React milieu in general, I also don't have any prior knowledge of Relay, but chose it on recommendation from friends in my startup community and my web-dev colleagues. 作为一般的React环境的新手,我也没有任何关于Relay的先验知识,但是在我的创业社区的朋友和我的web-dev同事的推荐下选择了它。 I was also warned about a somewhat steep learning curve, but decided to go ahead anyway - I am already fighting an uphill battle with JS and a 0.xx version of a new mobile platform, so what the hell, right? 我也被警告过一个有点陡峭的学习曲线,但无论如何我决定继续 - 我已经在与JS和一个新的移动平台的0.xx版本进行一场艰苦的战斗,所以到底是什么,对吧? :-) I managed to set up my project correctly and punch a whole through to my GQL server with a QueryRenderer , which was a great relief :-) :-)我设法正确地设置了我的项目并用QueryRenderer了我的GQL服务器,这是一个很大的安慰:-)

So, on to the questions 所以,关于问题

I'm having a hard time figuring out the container/component relationship, and container composition in general. 我很难搞清楚容器/组件的关系,以及一般的容器组成。 Reading the docs on composition helped, but I'm still in doubt over the role of the QueryRenderer 阅读关于作文的文档有所帮助,但我对QueryRenderer的作用仍有疑问

  • QueryRenderer is said by the docs to be the root container for every Relay tree. QueryRenderer被docs称为每个Relay树的根容器。 Does that mean that one should have a QueryRenderer for the root in our app? 这是否意味着应该在我们的应用程序中为root创建一个QueryRenderer Or at the root of each navigation path (ie tabs in our app)? 或者在每个导航路径的根目录(即我们的应用程序中的选项卡)? Or just for each container component (as opposed to presentational/dumb/pure components, React wise)? 或者仅针对每个容器组件(与表示/哑/纯组件相反,React明智)? Note that I'm not looking for opinions, but arguments for best practice :-) 请注意,我不是在寻找意见,而是寻求最佳实践:-)
  • Can a FragmentContainer (or any other container, for that matter) work without a QueryRenderer in the 'parent' component? FragmentContainer (或任何其他容器)可以在“父”组件中没有QueryRenderer情况下工作吗?
  • How is the QueryRenderer linked to child containers? QueryRenderer如何链接到子容器? Does it fetch the sum of all the data that child containers want, and then the child containers read from the cache, or? 它是否获取子容器所需的所有数据的总和,然后从缓存中读取子容器,或者? If so, I've misunderstood the pros of Relay - we are under the impression that each component can retrieve data independently from every other components, and that each component does not know anything about the data requirements of other components (including parent/child components). 如果是这样,我误解了Relay的优点 - 我们认为每个组件都可以独立于其他所有组件检索数据,并且每个组件都不了解其他组件(包括父/子组件)的数据要求)。 I think this assumption is also what confuses me about the QueryRenderer , and the need for a “Root” container. 我认为这个假设也让我对QueryRenderer ,并且需要一个“Root”容器。
  • If QueryRenderer is a 'parent'/'root' Relay container to a Relay tree, how come it has to render view components based on it's request? 如果QueryRenderer是中继树的“父”/“根”中继容器,那么它如何根据它的请求呈现视图组件呢? And why does it have to have a request? 为什么它必须有一个请求? When and for what should we use a QueryRenderer ? 何时以及我们应该使用QueryRenderer

Any help is much appreciated :-) 任何帮助深表感谢 :-)

Thanks for bringing up this topic. 感谢您提出这个话题。 I too have just been getting into Relay with ReactNative, and with some exciting results. 我也刚刚使用ReactNative进入Relay,并获得了一些令人兴奋的结果。

Firstly, I am surprised how easy it has been to bring UI components reflecting GraphQL databases to the screen. 首先,我很惊讶将用于反映GraphQL数据库的UI组件带到屏幕上是多么容易。 After the initial overhead of learning the basics of JavaScript and the react-native pipeline, Relay has become a fantastic way to present data. 在学习了JavaScript和反应原生流水线的基础知识的最初开销之后,Relay已经成为一种呈现数据的绝佳方式。

In regards to best practices I cannot say for sure how to present your QueryRenderer and the fragmentContainer, however I can describe our way of presenting the data. 关于最佳实践,我无法确定如何呈现您的QueryRenderer和fragmentContainer,但是我可以描述我们呈现数据的方式。

Firstly we create a react-navigation stack and tab. 首先,我们创建一个反应导航堆栈和选项卡。 Inside each major screen we run a QueryRenderer. 在每个主屏幕内,我们运行QueryRenderer。 Then within that QueryRenderer, for each specific UI component, we seperate into a fragmentContainer. 然后在该QueryRenderer中,对于每个特定的UI组件,我们分成一个fragmentContainer。

  • Navigation Flow (react-navigation, Stack/Tab Navigators) 导航流程(反应导航,堆栈/标签导航器)
  • Screen (QueryRenderer) UI 屏幕(QueryRenderer)UI
  • Widget/Component (fragmentContainer) 小部件/组件(fragmentContainer)

This allows us to create the required primary query for the screen then break up the components data to fit within consumable components that are easily defined by the GraphQL query fragment they represent. 这允许我们为屏幕创建所需的主要查询,然后拆分组件数据以适应易于由其所代表的GraphQL查询片段定义的可消耗组件。 However it does mean that we are running multiple queries across the app with no central account query to wrap the entire rendering up into a neat package. 但是,这确实意味着我们在应用程序中运行多个查询而没有中央帐户查询将整个呈现包装成一个整洁的包。

Ideally I would like to try a QueryRenderer at the top inside a Navigator, however I haven't quite yet got my head around how and if this would work as Navigators do not respond to a render() function, which is where the QueryRenderer is required. 理想情况下,我想在导航器的顶部尝试一个QueryRenderer,但是我还没有完全了解如何以及如果这可以工作,因为Navigators不响应render()函数,这是QueryRenderer所在的位置需要。

I would also be interested in hearing other peoples approaches to how they apply relay within a navigable react-native app. 我也有兴趣听听其他人如何在可导航的本地应用中应用中继的方法。

So, after having worked some more with our app, I thought I'd come back to post about our thoughts and experiences so far, in the hopes that it helps someone. 因此,在使用我们的应用程序进行了更多工作之后,我想我会回来发布关于我们迄今为止的想法和经验,希望它可以帮助某人。

Building on @Peter Suwara's great post, we arrived at a similar strategy initially 在@Peter Suwara的伟大帖子的基础上,我们最初达成了类似的策略

  • Have a root/parent nav tree 有一个根/父导航树
  • For each screen in the tree, have a QueryRenderer as the root for all presentational/dumb components it contains 对于树中的每个屏幕,将QueryRenderer作为其包含的所有表示/哑元组件的根
  • Use fragments to declare data dependencies within the subcomponents 使用片段在子组件中声明数据依赖性

Like we discussed in the comments below his answer though, this approach might not always be the best. 就像我们在下面的评论中讨论他的答案一样,这种方法可能并不总是最好的。 After further discussion internally, we came to the conclusion that there are too many cases where this might not make sense after all for a couple of reasons: 在内部进一步讨论之后,我们得出的结论是,由于以下几个原因,有太多案例可能没有意义:

  • If you retrieve data for each screen-load, you often times will end up with more roundtrips than necessary. 如果您为每个屏幕加载检索数据,通常会有更多的往返次数。 You want to take the app-flow into account, and fetch as much data as is viable/needed for a route and it's children. 您希望将app-flow考虑在内,并获取尽可能多的数据,以及路由及其子项所需的数据。
  • Also, if you retrieve all data for the child components of a screen on the same QueryRenderer, you might sometimes end up fetching data that will never be shown to the user (ie details for a details view that is rarely shown, or similar cases). 此外,如果您在同一QueryRenderer上检索屏幕的子组件的所有数据,您有时可能最终会获取永远不会向用户显示的数据(即很少显示的详细信息视图的详细信息或类似情况) 。

After some more thought and discussion, we came up with this rule-of-thumb for when to create a new QueryRenderer 'root' of a Relay tree: 经过一番思考和讨论后,我们想出了这个经验法则,以便何时创建一个Relay树的新QueryRenderer“root”:

Create a new QueryRenderer for whenever you need a new error-handling strategy 无论何时需要新的错误处理策略,都要创建一个新的QueryRenderer

This occured for the very practical reason that it's the only chance you have to handle errors/loading of data, but it also made sense to us in the way of user-flow and composition, as usually the two things collide - you want a new way of handling network errors when you reach a new 'flow'/part of your app. 这是因为非常实际的原因,它是您处理数据错误/加载的唯一机会,但它对用户流和组合的方式也有意义,因为通常两个事情发生冲突 - 你想要一个新的当您到达应用程序的新“流程”/部分时处理网络错误的方法。 Maybe some smarter heads at Facebook thought about this before us? 也许Facebook的一些聪明人在我们面前想到了这一点? :-) :-)

As with all rules of thumb, it's just that - a guideline, and not a rule, and it has exceptions, of course. 与所有经验法则一样,它只是 - 指南,而不是规则,当然也有例外。 However, it seems to make sense for us internally - at least for now. 然而,它似乎对我们内部有意义 - 至少目前如此。

Any and all feedback on these thoughts are very welcome, as I think the official docs are lackluster to say the least, and community discussion is therefore all we have until the docs and general patterns are settled upon with time. 关于这些想法的任何和所有反馈都是非常受欢迎的,因为我认为官方文档至少可以说是乏善可陈的,因此社区讨论是我们所有的,直到文档和一般模式随着时间的推移而解决。

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

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