From 76692d26642eabf23d2ef038dec35d35d4e35d31 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Fri, 9 Apr 2021 12:59:13 +1000 Subject: [PATCH] Fix context.prisma undefined in onConnect (#5407) --- .changeset/silly-numbers-thank.md | 5 +++++ packages-next/keystone/src/lib/createKeystone.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .changeset/silly-numbers-thank.md diff --git a/.changeset/silly-numbers-thank.md b/.changeset/silly-numbers-thank.md new file mode 100644 index 00000000000..0378bba6d0d --- /dev/null +++ b/.changeset/silly-numbers-thank.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/keystone': patch +--- + +Fixed a bug where `context.prisma` was `undefined` in the `onConnect()` function. diff --git a/packages-next/keystone/src/lib/createKeystone.ts b/packages-next/keystone/src/lib/createKeystone.ts index 14a8fa227c5..15affd8b5b1 100644 --- a/packages-next/keystone/src/lib/createKeystone.ts +++ b/packages-next/keystone/src/lib/createKeystone.ts @@ -27,8 +27,10 @@ export function createKeystone(config: KeystoneConfig, prismaClient?: any) { const keystone: BaseKeystone = new Keystone({ adapter, queryLimits: graphql?.queryLimits, + // We call context.sudo() here to regenerate the `context` object *after* the keystone.connect() + // step. This ensures that context.prisma is correctly set up. // @ts-ignore The @types/keystonejs__keystone package has the wrong type for KeystoneOptions - onConnect: (keystone, { context } = {}) => config.db.onConnect?.(context), + onConnect: (keystone, { context } = {}) => config.db.onConnect?.(context?.sudo()), // FIXME: Unsupported options: Need to work which of these we want to support with backwards // compatibility options. // defaultAccess