From 277a98631555b0ce8cc883e0d695d4a21ffc7b4b Mon Sep 17 00:00:00 2001 From: Daniel Cousens <413395+dcousens@users.noreply.github.com> Date: Wed, 15 Nov 2023 14:32:51 +1100 Subject: [PATCH 1/2] unpack context.db/context.query types from type maps --- packages/core/src/types/context.ts | 179 +++++++++++++++-------------- 1 file changed, 90 insertions(+), 89 deletions(-) diff --git a/packages/core/src/types/context.ts b/packages/core/src/types/context.ts index fd3368b4321..284c0fe816f 100644 --- a/packages/core/src/types/context.ts +++ b/packages/core/src/types/context.ts @@ -33,62 +33,61 @@ export type KeystoneContext` below) - type UniqueWhereInput = false extends ListTypeInfo['isSingleton'] ? { readonly where: ListTypeInfo['inputs']['uniqueWhere'] } : { readonly where?: ListTypeInfo['inputs']['uniqueWhere'] } +type ListAPI = { + findMany( + args?: { + readonly where?: ListTypeInfo['inputs']['where'] + readonly take?: number + readonly skip?: number + readonly orderBy?: + | ListTypeInfo['inputs']['orderBy'] + | readonly ListTypeInfo['inputs']['orderBy'][] + readonly cursor?: ListTypeInfo['inputs']['uniqueWhere'] + } & ResolveFields + ): Promise[]> + findOne( + args: UniqueWhereInput & ResolveFields + ): Promise> + count(args?: { + readonly where?: ListTypeInfo['inputs']['where'] + }): Promise + updateOne( + args: UniqueWhereInput & { + readonly data: ListTypeInfo['inputs']['update'] + } & ResolveFields + ): Promise> + updateMany( + args: { + readonly data: readonly (UniqueWhereInput & { + readonly data: ListTypeInfo['inputs']['update'] + })[] + } & ResolveFields + ): Promise[]> + createOne( + args: { readonly data: ListTypeInfo['inputs']['create'] } & ResolveFields + ): Promise> + createMany( + args: { + readonly data: readonly ListTypeInfo['inputs']['create'][] + } & ResolveFields + ): Promise[]> + deleteOne( + args: UniqueWhereInput & ResolveFields + ): Promise | null> + deleteMany( + args: { + readonly where: readonly ListTypeInfo['inputs']['uniqueWhere'][] + } & ResolveFields + ): Promise[]> +} + export type KeystoneListsAPI> = { - [Key in keyof KeystoneListsTypeInfo]: { - findMany( - args?: { - readonly where?: KeystoneListsTypeInfo[Key]['inputs']['where'] - readonly take?: number - readonly skip?: number - readonly orderBy?: - | KeystoneListsTypeInfo[Key]['inputs']['orderBy'] - | readonly KeystoneListsTypeInfo[Key]['inputs']['orderBy'][] - readonly cursor?: KeystoneListsTypeInfo[Key]['inputs']['uniqueWhere'] - } & ResolveFields - ): Promise[]> - findOne( - args: UniqueWhereInput & ResolveFields - ): Promise> - count(args?: { - readonly where?: KeystoneListsTypeInfo[Key]['inputs']['where'] - }): Promise - updateOne( - args: UniqueWhereInput & { - readonly data: KeystoneListsTypeInfo[Key]['inputs']['update'] - } & ResolveFields - ): Promise> - updateMany( - args: { - readonly data: readonly (UniqueWhereInput & { - readonly data: KeystoneListsTypeInfo[Key]['inputs']['update'] - })[] - } & ResolveFields - ): Promise[]> - createOne( - args: { readonly data: KeystoneListsTypeInfo[Key]['inputs']['create'] } & ResolveFields - ): Promise> - createMany( - args: { - readonly data: readonly KeystoneListsTypeInfo[Key]['inputs']['create'][] - } & ResolveFields - ): Promise[]> - deleteOne( - args: UniqueWhereInput & ResolveFields - ): Promise | null> - deleteMany( - args: { - readonly where: readonly KeystoneListsTypeInfo[Key]['inputs']['uniqueWhere'][] - } & ResolveFields - ): Promise[]> - }; + [Key in keyof KeystoneListsTypeInfo]: ListAPI } type ResolveFields = { @@ -98,46 +97,48 @@ type ResolveFields = { readonly query?: string } +type DbAPI = { + findMany(args?: { + readonly where?: ListTypeInfo['inputs']['where'] + readonly take?: number + readonly skip?: number + readonly orderBy?: + | ListTypeInfo['inputs']['orderBy'] + | readonly ListTypeInfo['inputs']['orderBy'][] + readonly cursor?: ListTypeInfo['inputs']['uniqueWhere'] + }): Promise + findOne( + args: UniqueWhereInput + ): Promise + count(args?: { + readonly where?: ListTypeInfo['inputs']['where'] + }): Promise + updateOne( + args: UniqueWhereInput & { + readonly data: ListTypeInfo['inputs']['update'] + } + ): Promise + updateMany(args: { + readonly data: readonly (UniqueWhereInput & { + readonly data: ListTypeInfo['inputs']['update'] + })[] + }): Promise + createOne(args: { + readonly data: ListTypeInfo['inputs']['create'] + }): Promise + createMany(args: { + readonly data: readonly ListTypeInfo['inputs']['create'][] + }): Promise + deleteOne( + args: UniqueWhereInput + ): Promise + deleteMany(args: { + readonly where: readonly ListTypeInfo['inputs']['uniqueWhere'][] + }): Promise +} + export type KeystoneDbAPI> = { - [Key in keyof KeystoneListsTypeInfo]: { - findMany(args?: { - readonly where?: KeystoneListsTypeInfo[Key]['inputs']['where'] - readonly take?: number - readonly skip?: number - readonly orderBy?: - | KeystoneListsTypeInfo[Key]['inputs']['orderBy'] - | readonly KeystoneListsTypeInfo[Key]['inputs']['orderBy'][] - readonly cursor?: KeystoneListsTypeInfo[Key]['inputs']['uniqueWhere'] - }): Promise - findOne( - args: UniqueWhereInput - ): Promise - count(args?: { - readonly where?: KeystoneListsTypeInfo[Key]['inputs']['where'] - }): Promise - updateOne( - args: UniqueWhereInput & { - readonly data: KeystoneListsTypeInfo[Key]['inputs']['update'] - } - ): Promise - updateMany(args: { - readonly data: readonly (UniqueWhereInput & { - readonly data: KeystoneListsTypeInfo[Key]['inputs']['update'] - })[] - }): Promise - createOne(args: { - readonly data: KeystoneListsTypeInfo[Key]['inputs']['create'] - }): Promise - createMany(args: { - readonly data: readonly KeystoneListsTypeInfo[Key]['inputs']['create'][] - }): Promise - deleteOne( - args: UniqueWhereInput - ): Promise - deleteMany(args: { - readonly where: readonly KeystoneListsTypeInfo[Key]['inputs']['uniqueWhere'][] - }): Promise - }; + [Key in keyof KeystoneListsTypeInfo]: DbAPI } // GraphQL API From 5a7637ba3ad820a4e511c0285ce135cda68aa0bb Mon Sep 17 00:00:00 2001 From: Daniel Cousens <413395+dcousens@users.noreply.github.com> Date: Wed, 15 Nov 2023 12:17:03 +1100 Subject: [PATCH 2/2] use preferred type unpacking pattern --- .../core/src/types/config/access-control.ts | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/packages/core/src/types/config/access-control.ts b/packages/core/src/types/config/access-control.ts index b9ef6dffa07..ca0a6cd6927 100644 --- a/packages/core/src/types/config/access-control.ts +++ b/packages/core/src/types/config/access-control.ts @@ -24,43 +24,46 @@ export type ListFilterAccessControl< args: BaseAccessArgs & { operation: Operation } ) => MaybePromise -export type CreateListItemAccessControl = ( - args: BaseAccessArgs & { - operation: 'create' - - /** - * The input passed in from the GraphQL API - */ - inputData: ListTypeInfo['inputs']['create'] - } -) => MaybePromise - -export type UpdateListItemAccessControl = ( +export type ListItemAccessControl< + Operation extends ItemOperation, + ListTypeInfo extends BaseListTypeInfo +> = ( args: BaseAccessArgs & { - operation: 'update' + create: { + operation: 'create' - /** - * The item being updated - */ - item: ListTypeInfo['item'] + /** + * The input passed in from the GraphQL API + */ + inputData: ListTypeInfo['inputs']['create'] + } + update: { + operation: 'update' + + /** + * The input passed in from the GraphQL API + */ + inputData: ListTypeInfo['inputs']['update'] + + /** + * The item being updated + */ + item: ListTypeInfo['item'] + } + delete: { + operation: 'delete' - /** - * The input passed in from the GraphQL API - */ - inputData: ListTypeInfo['inputs']['update'] - } + /** + * The item being deleted + */ + item: ListTypeInfo['item'] + } + }[Operation] ) => MaybePromise -export type DeleteListItemAccessControl = ( - args: BaseAccessArgs & { - operation: 'delete' - - /** - * The item being deleted - */ - item: ListTypeInfo['item'] - } -) => MaybePromise +export type CreateListItemAccessControl = ListItemAccessControl<'create', ListTypeInfo> +export type UpdateListItemAccessControl = ListItemAccessControl<'update', ListTypeInfo> +export type DeleteListItemAccessControl = ListItemAccessControl<'delete', ListTypeInfo> type ListAccessControlFunction = ( args: BaseAccessArgs & { operation: AccessOperation } @@ -91,10 +94,10 @@ type ListAccessControlObject = { // These rules are applied to each item being operated on individually. They return `true` or `false`, // and if false, an access denied error will be returned for the individual operation. item?: { - // read?: not supported - create?: CreateListItemAccessControl - update?: UpdateListItemAccessControl - delete?: DeleteListItemAccessControl + // read?: not supported // TODO: why not + create?: ListItemAccessControl<'create', ListTypeInfo> + update?: ListItemAccessControl<'update', ListTypeInfo> + delete?: ListItemAccessControl<'delete', ListTypeInfo> } }