我试图使用“列表”实现窗口分页。我不需要基于光标的连接解决方案,因为我需要向用户显示带编号的页面。GraphQL:实现定期列表的窗口分页
有“用户”和“发布”对象。“用户”与“发布”具有一对多关系。
使用graphql-JS的架构, 这里是我的用户类型和postType模式:
var userType = new GraphQLObjectType({
name: 'User',
fields:() => ({
id: globalIdField('User'),
posts: {
type: new GraphQLList(postType),
args: {
page:{
type: GraphQLInt,
defaultValue: 0
}
},
resolve: (_, args) => {
//code to return relevant result set
},
},
totalPosts:{
type: GraphQLInt,
resolve:() => {
//code to return total count
}
},
}),
interfaces: [nodeInterface],
});
var postType = new GraphQLObjectType({
name: 'Post',
fields:() => ({
id: globalIdField('Post'),
name: {type: GraphQLString},
//other fields
}),
interfaces: [nodeInterface],
});
请注意在 “用户类型” 中的 “totalPosts” 字段。既然会有其他的用户列表,需要相同的分页,我最终会在分段中保留大量的“全部{Type}”变量。如果我能以某种方式发送List结果中的totalCount,这可以解决。
https://github.com/facebook/graphql/issues/4此问题讨论如何在列表上实现一个包装,以将totalCount包含在结果集中。
我试图创建一个像这样的包装:
var postList = new GraphQLObjectType({
name: 'PostList',
fields:()=>({
count: {
type: GraphQLInt,
resolve:()=>getPosts().length //this is total count
},
edges: {
type: new GraphQLList(postType),
resolve:() => {
return getPosts() ; // this is results for the page, though I don't know how to use 'page' argument here
},
}
}),
interfaces: [nodeInterface],
});
,但我应该怎么这个连接userType
的posts
场?我怎么能在这个包装上使用'页面'参数,就像我在原始的userType中一样?
谢谢!有效。不得不删除接口:[nodeInterface],从postList包装 – sanandrl
我很高兴你发现它很有用。根据您的反馈,我从'postList'包装中删除'interfaces'字段。谢谢! –