Skip to content

Commit

Permalink
fix: import prisma client for instanceof check using configured path (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored May 25, 2021
1 parent 60a77cd commit b796689
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ query {

- **@summary** Where Nexus Prisma will try to import your generated Prisma Client from. You should not need to configure this normally because Nexus Prisma generator automatically reads the Prisma Client generator `output` setting if you have set it. The value here will be used in a dynamic import thus following Node's path resolution rules. You can pass a node_modules package like `foo` `@prisma/client`, `@my/custom/thing` etc. or you can pass an absolute module/file path `/my/custom/thing` / `/my/custom/thing/index.js` or finally a relative path to be resolved relative to the location of Nexus Prisma source files (you probably don't want this).

- **@default** `@prisma/client`

- **@remarks** Nexus Prisma imports Prisma client internally for two reasons: 1) validation wherein a class reference to Prisma Client is needed for some `instanceof` checks and 2) for acquiring the DMMF as Nexus Prisma relies on some post-processing done by Prisma Client generator.

- **@example**
Expand Down
3 changes: 2 additions & 1 deletion src/generator/gentime/settingsSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export namespace Gentime {
* class reference to Prisma Client is needed for some `instanceof` checks and 2) for
* acquiring the DMMF as Nexus Prisma relies on some post-processing done by Prisma Client
* generator.
* @default '@prisma/client'
*/
prismaClientImportId?: string | null
prismaClientImportId?: string
}

export type SettingsData = Setset.InferDataFromInput<SettingsInput>
Expand Down
16 changes: 2 additions & 14 deletions src/generator/models/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ type NexusTypeDefConfigurations = Record<
export type Settings = {
runtime: Runtime.Settings
gentime: Gentime.SettingsData
internal: {
/**
* The import ID of prisma client.
*
* @remarks Used to get a class reference to do an instance check for runtime validation reasons.
* @default @prisma/client
*/
prismaClientImport: string
}
}

/**
Expand All @@ -61,9 +52,6 @@ export function createModuleSpec(gentimeSettings: Gentime.Settings): ModuleSpec
const models = ModelsGenerator.JS.createNexusTypeDefConfigurations(dmmf, {
runtime: Runtime.settings,
gentime: gentimeSettings,
internal: {
prismaClientImport: '@prisma/client',
}
})
const moduleExports = {
Expand Down Expand Up @@ -188,13 +176,13 @@ export function prismaFieldToNexusResolver(
}

// eslint-disable-next-line
const PrismaClientPackage = require(settings.internal.prismaClientImport)
const PrismaClientPackage = require(settings.gentime.prismaClientImportId)

// eslint-disable-next-line
if (!(ctx[settings.runtime.data.prismaClientContextField] instanceof PrismaClientPackage.PrismaClient)) {
// TODO rich errors
throw new Error(
`The GraphQL context.${settings.runtime.data.prismaClientContextField} value is not an instance of the Prisma Client (class reference for check imported from ${settings.internal.prismaClientImport}).`
`The GraphQL context.${settings.runtime.data.prismaClientContextField} value is not an instance of the Prisma Client (class reference for check imported from ${settings.gentime.prismaClientImportId}).`
)
}

Expand Down
6 changes: 3 additions & 3 deletions tests/__helpers__/testers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { ModuleSpec } from '../../src/generator/types'

const settingsDefaults: Settings = {
gentime: Gentime.settings.data,
internal: { prismaClientImport: '@prisma/client' },
runtime: Runtime.settings,
}

Expand Down Expand Up @@ -179,8 +178,9 @@ export async function integrationTest({

const nexusPrisma = ModelsGenerator.JS.createNexusTypeDefConfigurations(dmmf, {
...settingsDefaults,
internal: {
prismaClientImport: prismaClientImportId,
gentime: {
...settingsDefaults.gentime,
prismaClientImportId: prismaClientImportId,
},
}) as any

Expand Down

0 comments on commit b796689

Please sign in to comment.