gpt4 book ai didi

node.js - 使用 join-monster 库 (GraphQL) 对根查询字段进行分页

转载 作者:太空宇宙 更新时间:2023-11-04 00:25:15 26 4
gpt4 key购买 nike

我正在努力按照文档使用 join-monster 库来实现应用程序层分页:http://join-monster.readthedocs.io/en/latest/pagination/

所有示例都是关于从特定 Node 对关系进行分页,但我想对 RootQuery 中的字段进行分页。我的 RootQuery 如下所示:

const RootQuery = new GraphQLObjectType({
name: "Query",
articles: {
type: new GraphQLList(ArticleType),
args: {},
resolve(parentValue, args, context, resolveInfo) {
return joinMonster(
resolveInfo,
{},
sql => {
return knex.raw(sql).then(result => {
return result[0];
});
},
{ dialect: "mysql" }
);
}
}
})
});

我想对文章字段进行分页,因此我尝试将其转换为连接:

const RootQuery = new GraphQLObjectType({
name: "Query",
fields: () => ({
articles: {
type: ArticleConnection,
args: gqlUtils.connectionArgs,
resolve(parentValue, args, context, resolveInfo) {
return joinMonster(
resolveInfo,
{},
sql => {
return knex.raw(sql).then(result => {
return gqlUtils.connectionFromArray(result[0], args);
});
},
{ dialect: "mysql" }
);
}
}
})
});

但是当对其运行查询时,我收到错误

function must return/resolve an array of objects where each object is a row from the result set. Instead got { edges...

现在我陷入困境了。我是否以错误的方式处理它?我该怎么做呢?

谢谢。

最佳答案

这几乎是正确的,问题是我在将数据发送到 joinMonster 之前调用了“connectionFromArray”,显然必须在之后调用它:

const RootQuery = new GraphQLObjectType({
name: "Query",
fields: () => ({
articles: {
type: ArticleConnection,
args: connectionArgs,
resolve(parentValue, args, context, resolveInfo) {
return joinMonster(
resolveInfo,
{},
sql => {
return knex.raw(sql).then(result => {
return result[0];
});
},
{ dialect: "mysql" }
).then(data => connectionFromArray(data, args));
}
}
})
});

关于node.js - 使用 join-monster 库 (GraphQL) 对根查询字段进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43006997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com