我已经定义了 graphql-java 实现和模式。 是否可以在 GraphQL 查询中创建 GraphQLList(SomeGraphQLObjectType) 类型的字段并在 Relay.QL {} 声明片段中使用它,以便我可以接收所需对象的列表?这是中继“连接”工作的地方吗?
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
是具有相同名称
的 GraphQLObjectType:
itemList: {
type: new GraphQLList(Item),
// other stuff including resolve function
},
由于您已将 Relay 容器划分为父容器和子容器,因此定义父容器如下:
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,
}
}
`,
},
});
您可以在this article中了解有关中继指令的更多信息。作者:克莱·奥尔索普。
有一个similar question您也可以看一下。
Is this the place where Relay "Connections" work?
这要看情况。如果您不希望一次完成所有项目,那么连接就是最佳选择。它允许增量获取数据。
关于java - 如何为GraphQLObjectType的GraphQLList定义Relay片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146840/