Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add select and include to findOne and findMany #7204

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/afraid-timers-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Add select and include options to findOne and findMany DB API calls
15 changes: 12 additions & 3 deletions packages/keystone/src/lib/core/queries/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { limitsExceededError, userInputError } from '../graphql-errors';
import { InitialisedList } from '../types-for-lists';
import { getDBFieldKeyForFieldOnMultiField, runWithPrisma } from '../utils';
import { checkFilterOrderAccess } from '../filter-order-access';
import { Prisma } from '@prisma/client';

// we want to put the value we get back from the field's unique where resolver into an equals
// rather than directly passing the value as the filter (even though Prisma supports that), we use equals
Expand Down Expand Up @@ -77,7 +78,7 @@ export async function accessControlledFilter(
}

export async function findOne(
args: { where: UniqueInputFilter },
args: { where: UniqueInputFilter; select: Prisma.UserSelect; include: Prisma.UserInclude },
list: InitialisedList,
context: KeystoneContext
) {
Expand All @@ -103,11 +104,17 @@ export async function findOne(
// Apply access control
const filter = await accessControlledFilter(list, context, resolvedWhere, accessFilters);

return runWithPrisma(context, list, model => model.findFirst({ where: filter }));
return runWithPrisma(context, list, model =>
model.findFirst({
where: filter,
select: args.select ?? undefined,
include: args.include ?? undefined,
})
);
}

export async function findMany(
{ where, take, skip, orderBy: rawOrderBy }: FindManyArgsValue,
{ where, take, skip, orderBy: rawOrderBy, select, include }: FindManyArgsValue,
list: InitialisedList,
context: KeystoneContext,
info: GraphQLResolveInfo,
Expand Down Expand Up @@ -141,6 +148,8 @@ export async function findMany(
orderBy,
take: take ?? undefined,
skip,
select: select ?? undefined,
include: include ?? undefined,
})
);

Expand Down
5 changes: 5 additions & 0 deletions packages/keystone/src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GraphQLSchema, ExecutionResult, DocumentNode } from 'graphql';
import { InitialisedList } from '../lib/core/types-for-lists';
import { BaseListTypeInfo } from './type-info';
import { GqlNames, BaseKeystoneTypeInfo } from '.';
import { Prisma } from '@prisma/client';

export type KeystoneContext<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneTypeInfo> = {
req?: IncomingMessage;
Expand Down Expand Up @@ -104,9 +105,13 @@ export type KeystoneDbAPI<KeystoneListsTypeInfo extends Record<string, BaseListT
readonly orderBy?:
| KeystoneListsTypeInfo[Key]['inputs']['orderBy']
| readonly KeystoneListsTypeInfo[Key]['inputs']['orderBy'][];
readonly select?: Prisma.UserSelect;
readonly include?: Prisma.UserInclude;
}): Promise<readonly KeystoneListsTypeInfo[Key]['item'][]>;
findOne(args: {
readonly where: KeystoneListsTypeInfo[Key]['inputs']['uniqueWhere'];
readonly select?: Prisma.UserSelect;
readonly include?: Prisma.UserInclude;
}): Promise<KeystoneListsTypeInfo[Key]['item']>;
count(args?: {
readonly where?: KeystoneListsTypeInfo[Key]['inputs']['where'];
Expand Down
3 changes: 3 additions & 0 deletions packages/keystone/src/types/next-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BaseListTypeInfo } from './type-info';
import { CommonFieldConfig } from './config';
import { DatabaseProvider } from './core';
import { AdminMetaRootVal, JSONValue, KeystoneContext, MaybePromise } from '.';
import { Prisma } from '@prisma/client';

export { Decimal };

Expand Down Expand Up @@ -446,6 +447,8 @@ export type FindManyArgs = {
>;
take: graphql.Arg<typeof graphql.Int>;
skip: graphql.Arg<graphql.NonNullType<typeof graphql.Int>, true>;
select: Prisma.UserSelect;
include: Prisma.UserInclude;
};

export type FindManyArgsValue = graphql.InferValueFromArgs<FindManyArgs>;
Expand Down