使用join-monster库(GraphQL)

时间:2017-03-24 18:44:29

标签: node.js join pagination graphql

我正在努力使用文档后面的join-monster库实现应用层分页:http://join-monster.readthedocs.io/en/latest/pagination/

所有示例都是关于从特定节点分页关系,但我想从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" }
        );
      }
    }
  })
});

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

  

函数必须返回/解析每个对象所在的对象数组   结果集中的一行。取而代之的是{edge ...

现在我被卡住了。我是以错误的方式接近它吗?我该怎么办呢?

感谢。

1 个答案:

答案 0 :(得分:1)

这几乎是正确的,问题是我在将数据发送到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));
      }
    }
  })
});