首页 > 解决方案 > 未根据 GraphQL 放大查询中的过滤器获得计数

问题描述

根据过滤器获取扫描计数但不计数数据

count: null
items: [{id: "bcd75096-7fd9-4e9d-8675-6877f0609ac2", name: "dxfrdhjkhklklkl", description: "dgdxrfg",…},…]
0: {id: "bcd75096-7fd9-4e9d-8675-6877f0609ac2", name: "dxfrdhjkhklklkl", description: "dgdxrfg",…}
1: {id: "52f6ff60-fc07-4631-a1fb-b039f376ff21", name: "ghnfgyhj", description: "gyhkjmuhjolk",…}
2: {id: "f73dfb37-2778-4b87-88c7-e6f9f5b5c931", name: "drftgserty", description: "trse54rte54ty",…}
3: {id: "6df9f5c2-ec06-4e70-b5e2-133cb0d8e958", name: "tygujghukuh", description: "tuyjyuikuolnh",…}
4: {id: "9360a766-ac89-420c-881b-2b3089bcca7f", name: "kl;", description: "vcbghnjmk,l", is_active: true,…}
5: {id: "c0dcbaff-37d4-4e4c-9375-584ff7110d77", name: "dfhgbdcb", description: "dfxvcx", is_active: true,…},...
scannedCount: 100

我已按照这些教程获取计数如何使用 AWS AMPLIFY DYNAMODB 和 GRAPHQL 计算结果的数量

筛选

      var body = {
        filter: {
          is_active: {
            eq: true
          }
        }
      }

查询以获取待办事项列表

export const listTodos = /* GraphQL */ `
  query ListTodos(
    $filter: ModelTodoFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listTodos(filter: $filter, limit: $limit, nextToken: $nextToken) {
      count
      items {
        id
        name
        description
        is_active
        createdAt
        updatedAt
      }
      scannedCount
    }
  }
`;

GraphQl shema

type Todo @model {
  id: ID!
  name: String!
  description: String!
  is_active: Boolean
}

type ModelTodoConnection {
  items: [Todo]
  scannedCount: Int
  count: Int
  total: Int
}

如果我将限制设置为 5,如果我在数据库中的总数据在 110 左右,它将发回scannedCount5。我想计算数据的位置 is_active: { eq: true }

标签: graphqlamazon-dynamodbaws-amplifyaws-appsyncaws-amplify-cli

解决方案


查看我为解决此问题而编写的包:https ://github.com/multimeric/AmplifyCountDirective 。

按照安装说明进行操作后,为了解决您的问题,我会将架构更改为:

type Todo @model @count {
  id: ID!
  name: String!
  description: String!
  is_active: Boolean
}

然后您可以使用 GraphQL 查询来查询计数,例如:

{
    countTodo(filter: {
        is_active: {
            eq: true
        }
    })
}

推荐阅读