GraphQL:为常规列表

时间:2016-08-13 07:22:16

标签: graphql graphql-js

我正在尝试使用“List”实现窗口分页。我不需要基于游标的连接解决方​​案,因为我需要向用户显示编号页面。

有“用户”和“发布”对象。“用户”与“发布”有一对多关系。

将graphql-js用于架构, 这是我的userType和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],
});

请注意“userType”中的“totalPosts”字段。由于对于用户将会有其他列表,并且具有相同的分页需求,我将最终在片段中维护大量“total {Type}”变量。如果我能以某种方式在List结果中发送totalCount,就可以解决这个问题。

https://github.com/facebook/graphql/issues/4此问题涉及在List上实现包装器以在结果集中包含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的{​​{1}}字段相关联?我怎样才能在这个包装器上使用'page'参数,就像我在原始userType中一样?

1 个答案:

答案 0 :(得分:1)

  

我该如何将其连接到userType的帖子字段?我怎样才能使用'页面'关于这个包装器的参数,就像我在原始userType中一样?

实现您尝试执行的操作的一种简单方法是定义 dumb 包装器类型postList,如下所示:

var postList = new GraphQLObjectType({
    name: 'PostList',
    fields:()=>({
        count: { type: GraphQLInt },
        edges: { type: new GraphQLList(postType) }
        // Consider renaming 'edges'. In your case, it's a list, not a 
        // connection. So, it can cause confusion in the long run.
    }),
});

然后在userType定义中,添加该包装类型的字段并定义其解析函数,如下所示。至于参数page,只需在定义字段类型posts时进行描述。

posts: {
    type: postList,
    args: {
        page:{
            type: GraphQLInt,
            defaultValue: 0
        },
        ...otherArgs
    },
    resolve: async (_, {page, ...otherArgs}) => {
        // Get posts for the given page number.
        const posts = await db.getPosts(page);

        // Prepare a server-side object, which corresponds to GraphQL 
        // object type postList.
        const postListObj = {
            count: posts.length,
            edges: posts 
        };
        // Consider renaming 'edges'. In your case, it's a list, not a 
        // connection. So, it can cause confusion in the long run.
    },
},