【问题标题】:How to use 'limit' and 'start' properly (get the amount of number of instances)如何正确使用'limit'和'start'(获取实例数量)
【发布时间】:2019-08-11 07:20:03
【问题描述】:

我想获取模型的所有实例,但要使用分页来显示它们,所以我想在每个页面上获取 20 个项目。

获取模型的前 20 个项目如下所示:

const response = await strapi.request('POST', '/graphql', {
    data: {
        query: `query {
            exams (limit: 20, start: 0) {
              name,
              type
              _id,
            }
          }`
    }
})

问题在于,为了创建分页功能,我需要提前知道我有多少“考试”模型实例。

有人知道怎么做吗?

如果重要的话,我会使用带有 graphql 插件的 Strapi cms。

【问题讨论】:

  • 你的graphQL服务器如何连接到数据库/数据服务器?
  • 您需要在查询中再添加一个字段以返回存在的考试总数,它只是一个额外的字段,可以由字段解析器解析。
  • 我正在通过strapi cms 和mlab Pavan 连接那个额外的字段是什么?

标签: graphql strapi


【解决方案1】:

您必须添加计数功能。 这是 Restaurant 内容类型的示例。

./api/restaurant/config/schema.graphql.js

module.exports = {
  definition: /* GraphQL */ `
    type RestaurantsConnection {
      aggregate: RestaurantsAggregate
    }
    type RestaurantsAggregate {
      count: Int
    }
  `,
  query: /* GraphQL */ `
    restaurantsConnection(where: JSON): RestaurantsConnection
  `,
  resolver: {
    Query: {
      restaurantsConnection(_, args) {
        return args;
      },
    },
    RestaurantsConnection: {
      aggregate(args) {
        return args;
      },
    },
    RestaurantsAggregate: {
      count(args) {
        return strapi.controllers.restaurant.count({
          query: args.where || {},
        });
      },
    },
  },
};

现在你有一个连接来获取计数值

{
  restaurantsConnection {
    aggregate {
      count
     }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2016-03-30
    • 1970-01-01
    • 2021-06-17
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多