【发布时间】:2020-08-18 08:33:40
【问题描述】:
我有这个有效的功能:
export const tagsByLabel = async (params) => {
const findManyParams = {
where: { userId: userIdFromSession },
orderBy: { title: "asc" },
};
if (params) {
const { searchTerm } = params;
findManyParams.where.title = { contains: searchTerm };
}
console.log("findManyParams", findManyParams);
const tagsByLabelResult = await db.tag.findMany(findManyParams);
console.log("tagsByLabelResult", tagsByLabelResult);
return tagsByLabelResult;
};
如果我搜索“mex”,我会看到:
findManyParams {
where: { userId: 1, title: { contains: 'mex' } },
orderBy: { title: 'asc' }
}
tagsByLabelResult [
{
id: 9,
title: 'mex',
description: 'Mexican food',
userId: 1,
createdAt: 2020-05-03T22:16:09.134Z,
modifiedAt: 2020-05-03T22:16:09.134Z
}
]
对于空查询,tagsByLabelResult 包含所有标签记录。
如何调整我的tagsByLabel 函数以聚合(使用“分组依据”)记录并按计数降序为tagsByLabelResult 的每条记录输出一个“计数”?
tagsByLabelResult [
{
id: 9,
title: 'mex',
description: 'Mexican food',
count: 25,
userId: 1,
createdAt: 2020-05-03T22:16:09.134Z,
modifiedAt: 2020-05-03T22:16:09.134Z
}
]
我看到prisma.user.count() 的docs example,但这似乎检索了整个查询结果的简单计数,而不是作为具有“分组依据”的字段的计数。
我正在使用 RedwoodJs、Prisma 2、Apollo、GraphQL。
【问题讨论】:
标签: react-apollo prisma prisma-graphql redwoodjs