如何为 GraphQLObjectType 的 GraphQLList 定义中继片段?

How to define Relay fragment for GraphQLList of GraphQLObjectType?

我有一个 graphql-java 实现和定义的模式。 是否可以在 GraphQL 查询中创建 GraphQLList(SomeGraphQLObjectType) 类型的字段并在 Relay.QL {} 声明片段中使用它,这样我就可以收到所需对象的列表?这里是 Relay "Connections" 工作的地方吗?

export default Relay.createContainer(ChildComponent, {
  fragments: {
    item: () => Relay.QL`
      fragment on Item
      {
        id,
        name,
        color{
          id,
          name
        }
      }`
  }});


export default Relay.createContainer(ParentComponent, {
  fragments: {
    list: () => Relay.QL`
    fragment on ListOfItems{
       allItems
       {
         ${ChildComponent.getFragment('item')}
       }
    }`
  }
});

Is it possible to create a field of type GraphQLList(SomeGraphQLObjectType) in a GraphQL query and use it in a Relay.QL {} declaration fragment?

是的,在 @relay(plural: true) 指令的帮助下是可能的。它告诉 Relay 这个字段是一个列表,而不是单个项目。

像这样定义字段,其中 Item 是具有相同 name:

的 GraphQLObjectType
itemList: {
  type: new GraphQLList(Item),
  // other stuff including resolve function
},

既然你已经将你的中继容器划分为父容器和子容器,那么定义父容器是这样的:

export default Relay.createContainer(ParentComponent, {
  fragments: {
    itemList: () => Relay.QL`
      fragment on Item @relay(plural:true) {
        ${ChildComponent.getFragment('item')}
      }
    `,
  },
});

子容器是这样的:

export default Relay.createContainer(ChildComponent, {
  fragments: {
    item: () => Relay.QL`
      fragment on Item {
        id,
        name,
        color {
          id,
          name,
        }
      }
    `,
  },
});

您可以在 Clay Allsopp 的 this article 中了解有关中继指令的更多信息。

有个similar question你也可以看看

Is this the place where Relay "Connections" work?

视情况而定。如果您不希望一次性完成所有项目,那么连接就是解决之道。它支持增量获取数据。