【问题标题】:fetching additional information for a particular list item in relay/graphql在 relay/graphql 中获取特定列表项的附加信息
【发布时间】:2015-12-13 10:59:51
【问题描述】:

使用 Relay 和 GraphQL,假设我有一个返回查看器的模式和一个嵌入的关联文档列表。根查询(由片段组成)看起来像这样:

query Root {
  viewer {
    id,
    name,
    groups {
      edges {
        node {
          id,
          name,
        }
      }
    }
  }
}

这将允许我显示用户及其所有关联组的列表。

现在假设我希望用户能够单击该列表项,并使其展开以显示与该特定列表项关联的 cmets。我应该如何重组我对中继路由的查询,以便我可以接收这些 cmets?如果我将 cmets 边缘添加到我的组边缘,那么它不会获取所有组的 cmets 吗?

query Root {
  viewer {
    id,
    name,
    groups {
      edges {
        node {
          id,
          name,
          comments {
            edges {
              node {
                id,
                content
              }
            }
          }
        }
      }
    }
  }
}

或者我应该更改路线查询以查找特定组?

query Root {
  group(id: "someid"){
    id,
    name,
    comments {
      edges {
        node {
          id,
          content
        }
      }
    }
  },
  viewer {
    id,
    name,
    groups {
      edges {
        node {
          id,
          name,
        }
      }
    }
  }
}

我特别担心在relay 的上下文中使用它。即,我怎样才能有效地构建一个路由查询,它只会获取扩展列表项(或多个项目)的 cmets,同时仍然利用已经存在的缓存数据,并且在执行突变时会更新?上面的示例可能适用于特定的扩展组,但我不确定如何在不为 所有 组项目获取这些字段的情况下同时扩展多个组。

【问题讨论】:

    标签: graphql relayjs graphql-js


    【解决方案1】:

    Relay 0.3.2 将支持 @skip@include 指令。

    Group = Relay.createContainer(Group, {
      initialVariables: {
        numCommentsToShow: 10,
        showComments: false,
      },
      fragments: {
        group: () => Relay.QL`
          fragment on Group {
            comments(first: $numCommentsToShow) @include(if: $showComments) {
              edges {
                node {
                  content,
                  id,
                },
              },
            },
            id,
            name,
          }
        `,
      },
    });
    

    在您的渲染方法中,仅在定义了 this.props.group.comments 时才渲染 cmets。在 Group 组件中调用 this.props.relay.setVariables({showComments: true}) 以包含 cmets 字段(并在必要时获取)。

    class Group extends React.Component {
      _handleShowCommentsClick() {
        this.props.relay.setVariables({showComments: true});
      }
      renderComments() {
        return this.props.group.comments
          ? <Comments comments={this.props.group.comments} />
          : <button onClick={this._handleShowCommentsClick}>Show comments</button>;
      }
      render() {
        return (
          <div>
            ...
            {this.renderComments()}
          </div>
        );  
      }
    }
    

    【讨论】:

    • 有没有办法为列表查询(edges)中的单个项目获取附加数据? 为什么? 我试图将列表项无缝传输/缩放到单个项目视图。我已经测试了所有类型的策略,但我找不到方法。
    • 是的。在您想要显示单个项目视图的那一刻,您将知道项目的 ID。此时,单个项目视图应在根目录上滚动到 node 查询。 node(id: $itemID) { ${Component.getFragment('item')} }。由于该商品已通过 edges 抓取进入商店,因此不需要额外的网络请求。
    • 是的,这就是我目前所拥有的,但这就是问题所在 - 两个不同的、孤立的视图和查询,我找不到使用此设置将列表动画化为单个项目的方法。请看this dribbble GIFReact 部分很简单,我将只计算点击时的屏幕大小并将列表项缩放到全屏,这会将其他列表项推出视口,但是如何获取该项目的其他数据?我不能将node() 查询用作同一列表组件的第二个查询,或者可以吗?
    • @Solo,你能在 Stack Overflow 上提出一个关于这个的新问题吗?我的回答太长,无法评论。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2017-05-12
    • 1970-01-01
    • 2022-01-11
    • 2016-02-01
    相关资源
    最近更新 更多