简体   繁体   English

GraphQL / Relay - 是否需要在主 QueryRenderer 中查询所有表?

[英]GraphQL / Relay - do all tables need to be queried in main QueryRenderer?

I'm new to GraphQL and Relay and I'm struggling with queries, fragments, ...spreads & passing props.我是 GraphQL 和 Relay 的新手,我正在努力处理查询、片段、...传播和传递道具。 I think I'm unnecessarily passing props down through many, many components.我认为我不必要地通过许多组件传递道具。 I want to learn how to teleport my data objects from the QueryRenderer to deeply nested components, skipping all the ancestor components.我想学习如何将我的数据对象从 QueryRenderer 传送到深度嵌套的组件,跳过所有祖先组件。

Suppose I have a component structure like this.假设我有这样的组件结构。 I need a list of emojis from the 'emoji' table inside EmojisList component which is deep in the app.我需要来自应用程序深处的EmojisList组件内的“表情符号”表中的表情符号列表。 I'm not sure where to spread or pass props or when to ask for the actual scalars.我不确定在哪里传播或传递道具,或者何时询问实际的标量。

<MainApp>
  <QueryRenderer 
    environment={environment}
    query={graphql`
        query MainAppQuery {
        currentPerson { // current user
            ...Timeline_currentPerson
        }
        allEmojis {
            ...ReactionBar_emojisList
          }
        }
    `}
  />
  <Timeline currentPerson={props.currentPerson}>
    <PostList>
      <Post>
        <ReactionBar>
          <EmojisList>
            // I need this list
            export default createFragmentContainer(ReactionBar, {
              emojisList: graphql`
                fragment ReactionBar_emojisList on EmojisConnection @relay(plural: true) {
                  edges {
                    node {
                      name
                      rowId
                    }
                  }
                }
              `,
            });      
          </EmojisList>
        </ReactionBar>
      </Post>
    </PostList>
  </Timeline>
</MainApp>

控制台错误

You can use Fragment Container您可以使用片段容器

Wrap <EmojiList /> Component with createFragmentContainer HOC then it will grab all of the data you need that you have fetched at the root from QueryRenderer .使用createFragmentContainer HOC 包装<EmojiList />组件,然后它将获取您从QueryRenderer获取的所有您需要的数据。

The data will be accessible as props inside the component数据将作为组件内的道具访问

// EmojisList.js
import {createFragmentContainer, graphql} from 'react-relay';

class EmojisList extends React.Component {/* code */}

// Export a *new* React component that wraps the original `<EmojisList>`.
export default createFragmentContainer(EmojisList, {
  emojisList: graphql`
    fragment EmojisList_emojisList on EmojisConnection {
      edges {
        node {
         name
         rowId
        }
      }
    }
  `,
});

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

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