Skip to content

Commit

Permalink
Use the GraphQL API for lists.<list>.count (#5552)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Apr 26, 2021
1 parent 18ae28b commit a0c5aa3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-pears-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Update `context.lists.<list>.count()` to use the GraphQL API rather than directly calling the resolver.
5 changes: 5 additions & 0 deletions .changeset/moody-oranges-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/adapter-prisma-legacy': patch
---

Improved handling of null filter inputs.
7 changes: 5 additions & 2 deletions packages-next/keystone/src/lib/context/itemAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ export function itemAPIForList(
},
async count(rawArgs = {}) {
if (!getArgs.count) throw new Error('You do not have access to this resource');
const args = getArgs.count(rawArgs!);
return (await list.listQueryMeta(args, context)).getCount();
const { first, skip, where } = rawArgs;
const { listQueryMetaName, whereInputName } = context.gqlNames(listKey);
const query = `query ($first: Int, $skip: Int, $where: ${whereInputName}) { ${listQueryMetaName}(first: $first, skip: $skip, where: $where) { count } }`;
const response = await context.graphql.run({ query, variables: { first, skip, where } });
return response[listQueryMetaName].count;
},
createOne({ query, resolveFields, ...rawArgs }) {
if (!getArgs.createOne) throw new Error('You do not have access to this resource');
Expand Down
17 changes: 9 additions & 8 deletions packages/adapter-prisma/src/adapter-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ class PrismaListAdapter {
const { first, skip } = args;

// Adjust the count as appropriate
if (skip !== undefined) {
if (skip !== undefined && skip !== null) {
count -= skip;
}
if (first !== undefined) {
if (first !== undefined && first !== null) {
count = Math.min(count, first);
}
count = Math.max(0, count); // Don't want to go negative from a skip!
Expand All @@ -410,9 +410,9 @@ class PrismaListAdapter {
from,
}: {
args: {
where?: Record<string, any>;
first?: number;
skip?: number;
where?: Record<string, any> | null;
first?: number | null;
skip?: number | null;
sortBy?: string[];
orderBy?: Record<string, any>;
search?: string;
Expand Down Expand Up @@ -475,11 +475,11 @@ class PrismaListAdapter {

// Add query modifiers as required
if (!meta) {
if (first !== undefined) {
if (first !== undefined && first !== null) {
// SELECT ... LIMIT <first>
ret.take = first;
}
if (skip !== undefined) {
if (skip !== undefined && skip !== null) {
// SELECT ... OFFSET <skip>
ret.skip = skip;
}
Expand Down Expand Up @@ -509,7 +509,8 @@ class PrismaListAdapter {
return ret;
}

processWheres(where: Record<string, any>): Record<string, any> | undefined {
processWheres(where: Record<string, any> | null): Record<string, any> | undefined {
if (where === null) return undefined;
const processRelClause = (fieldPath: string, clause: Record<string, any>) =>
this.getListAdapterByKey(this.fieldAdaptersByPath[fieldPath].refListKey!)!.processWheres(
clause
Expand Down

1 comment on commit a0c5aa3

@vercel
Copy link

@vercel vercel bot commented on a0c5aa3 Apr 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.