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

fix!: scalars module only exports named exports #187

Merged
merged 17 commits into from
Feb 1, 2022
45 changes: 28 additions & 17 deletions src/cli/nexus-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

/** This script will be run by the prisma generator system. */

process.env.DEBUG_COLORS = 'true'
process.env.DEBUG_HIDE_DATE = 'true'
import { GeneratorConfig, generatorHandler } from '@prisma/generator-helper'
import dindist from 'dindist'
import expandTilde from 'expand-tilde'
import * as Path from 'path'

import { GeneratorConfig, generatorHandler } from '@prisma/generator-helper'

import { generateRuntimeAndEmit } from '../generator'
import { loadUserGentimeSettings, supportedSettingsModulePaths } from '../generator/gentime/settingsLoader'
import { Gentime } from '../generator/gentime'
import { Settings } from '../generator/Settings'
import { loadUserGentimeSettings, supportedSettingsModulePaths } from '../generator/Settings/Gentime/loader'
import { d } from '../helpers/debugNexusPrisma'
import { externalToInternalDmmf } from '../helpers/prismaExternalToInternalDMMF'
import { resolveGitHubActionsWindowsPathTilde } from '../helpers/utils'
import { renderCodeBlock, renderList, renderWarning } from '../lib/diagnostic'
import { PrismaUtils } from '../lib/prisma-utils'

process.env.DEBUG_COLORS = 'true'
process.env.DEBUG_HIDE_DATE = 'true'

// todo by default error in ci and warn in local
// enforceValidPeerDependencies({
Expand Down Expand Up @@ -46,7 +49,7 @@ generatorHandler({
throw new Error(`Failed to read the custom output path.`)
}

Gentime.changeSettings({
Settings.Gentime.changeSettings({
output: {
directory: generator.output.value,
},
Expand All @@ -71,31 +74,39 @@ generatorHandler({
)
}

// WARNING: Make sure this logic comes before `loadUserGentimeSettings` below
// otherwise we will overwrite the user's choice for this setting if they have set it.
Gentime.settings.change({
/**
* Set the place to import Prisma Client from to be whatever has been set as the output in their PSL schema.
*
* WARNING: Make sure this logic comes before `loadUserGentimeSettings` below
* otherwise we will overwrite the user's choice for this setting if they have set it.
*/
Settings.Gentime.settings.change({
prismaClientImportId: getPrismaClientImportIdForItsGeneratorOutputConfig(prismaClientGenerator),
})

const internalDMMF = externalToInternalDmmf(dmmf)

/**
* Loading the gentime settings will mutate the gentime settings assuming the user has
* imported and used the gentime settings in their configuration module.
*/
loadUserGentimeSettings()

/**
* If the output path is some explicit relative path then make it absolute relative to the Prisma Schema file directory.
*/
if (
Gentime.settings.data.output.directory !== 'default' &&
!Path.isAbsolute(Gentime.settings.data.output.directory)
Settings.Gentime.settings.data.output.directory !== 'default' &&
!Path.isAbsolute(Settings.Gentime.settings.data.output.directory)
) {
Gentime.settings.change({
Settings.Gentime.settings.change({
output: {
directory: Path.join(Path.dirname(schemaPath), Gentime.settings.data.output.directory),
directory: Path.join(Path.dirname(schemaPath), Settings.Gentime.settings.data.output.directory),
},
})
}

generateRuntimeAndEmit(internalDMMF, Gentime.settings)
const prismaClientDmmf = PrismaUtils.externalToInternalDmmf(dmmf)

generateRuntimeAndEmit(prismaClientDmmf, Settings.Gentime.settings)

process.stdout.write(
`You can now start using Nexus Prisma in your code. Reference: https://pris.ly/d/nexus-prisma\n`
Expand Down
4 changes: 1 addition & 3 deletions src/entrypoints/generator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { Gentime } from '../generator/gentime'

export const settings = Gentime.changeSettings
export { changeSettings as settings } from '../generator/Settings/Gentime'
19 changes: 5 additions & 14 deletions src/entrypoints/scalars.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { BigInt } from '../scalars/BigInt'
import { Bytes } from '../scalars/Bytes'
import { DateTime } from '../scalars/DateTime'
import { Decimal } from '../scalars/Decimal'
import { Json } from '../scalars/Json'

/**
* Predefined Nexus scalar type definitions to satisfy all custom scalars needed in GraphQL to map to the
* native scalars in Prisma. The mapping is as follows:
Expand Down Expand Up @@ -75,14 +69,11 @@ import { Json } from '../scalars/Json'
* names in Prisma. Then, you are expected to define those custom scalar types in your GraphQL
* API. For convenience you can use these ones.
*/
const NexusPrismaScalars = {
BigInt,
Bytes,
DateTime,
Decimal,
Json,
}

export default NexusPrismaScalars
import { BigInt } from '../scalars/BigInt'
import { Bytes } from '../scalars/Bytes'
import { DateTime } from '../scalars/DateTime'
import { Decimal } from '../scalars/Decimal'
import { Json } from '../scalars/Json'

export { BigInt, Bytes, DateTime, Decimal, Json }
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { DMMF } from '@prisma/client/runtime'
import dedent from 'dindist'
import { chain } from 'lodash'
import * as Nexus from 'nexus'
import { NexusEnumTypeConfig, NexusListDef, NexusNonNullDef, NexusNullDef } from 'nexus/dist/core'
import { inspect } from 'util'

import type { DMMF } from '@prisma/client/runtime'

import { MaybePromise, RecordUnknown, Resolver } from '../../helpers/utils'
import { Messenger } from '../../lib/messenger'
import { PrismaDmmf } from '../../lib/prisma-dmmf'
import { PrismaDocumentation } from '../../lib/prisma-documentation'
import { PrismaUtils } from '../../lib/prisma-utils'
import { Gentime } from '../gentime'
import { createWhereUniqueInput } from '../../lib/prisma-utils/whereUniqueInput'
import { Runtime } from '../runtime'
import { ModuleSpec } from '../types'
import { fieldTypeToGraphQLType } from './declaration'
import { inspect } from 'util'
import { Messenger } from '../../lib/messenger'
import { Module } from '../helpers/types'
import { Settings } from '../Settings'
import { fieldTypeToGraphQLType } from './TS'

type PrismaEnumName = string

Expand All @@ -31,18 +32,18 @@ type NexusTypeDefConfigurations = Record<
>

export type Settings = {
runtime: Runtime.Settings.Manager
gentime: Gentime.Settings.Data
runtime: Settings.Runtime.Manager
gentime: Settings.Gentime.Data
}

/**
* Create the module specification for the JavaScript runtime.
*/
export function createModuleSpec(params: {
export const createModule = (params: {
/**
* Resolved generator settings (whatever user supplied merged with defaults).
*/
gentimeSettings: Gentime.Settings.Manager
gentimeSettings: Settings.Gentime.Manager
/**
* Should the module be generated using ESM instead of CJS?
*/
Expand All @@ -51,14 +52,14 @@ export function createModuleSpec(params: {
* Detailed data about the Prisma Schema contents and available operations over its models.
*/
dmmf: DMMF.Document
}): ModuleSpec {
}): Module => {
const { esm, gentimeSettings, dmmf } = params

const esmModelExports =
dmmf.datamodel.models
.map((model) => {
return dedent`
export const ${model.name} = models['${model.name}']
export const ${model.name} = nexusTypeDefConfigurations['${model.name}']
`
})
.join('\n') || `// N/A -- You have not defined any models in your Prisma Schema.`
Expand All @@ -67,7 +68,7 @@ export function createModuleSpec(params: {
dmmf.datamodel.enums
.map((enum_) => {
return dedent`
export const ${enum_.name} = models['${enum_.name}']
export const ${enum_.name} = nexusTypeDefConfigurations['${enum_.name}']
`
})
.join('\n') || `// N/A -- You have not defined any enums in your Prisma Schema.`
Expand All @@ -80,7 +81,7 @@ export function createModuleSpec(params: {

// Static API Exports

export const $settings = Runtime.changeSettings
export const $settings = RuntimeSettings.changeSettings

// Reflected Model Exports

Expand All @@ -92,8 +93,8 @@ export function createModuleSpec(params: {
`
: dedent`
module.exports = {
$settings: Runtime.changeSettings,
...models,
$settings: RuntimeSettings.changeSettings,
...nexusTypeDefConfigurations,
}
`

Expand All @@ -106,13 +107,13 @@ export function createModuleSpec(params: {
const imports = esm
? dedent`
import { getPrismaClientDmmf } from '${importSpecifierToNexusPrismaSourceDirectory}/helpers/prisma'
import * as ModelsGenerator from '${importSpecifierToNexusPrismaSourceDirectory}/generator/models/index'
import { Runtime } from '${importSpecifierToNexusPrismaSourceDirectory}/generator/runtime/index'
import { ModuleGenerators } from '${importSpecifierToNexusPrismaSourceDirectory}/generator/ModuleGenerators/index'
import * as RuntimeSettings from '${importSpecifierToNexusPrismaSourceDirectory}/generator/Settings/Runtime/index'
`
: dedent`
const { getPrismaClientDmmf } = require('${importSpecifierToNexusPrismaSourceDirectory}/helpers/prisma')
const ModelsGenerator = require('${importSpecifierToNexusPrismaSourceDirectory}/generator/models/index')
const { Runtime } = require('${importSpecifierToNexusPrismaSourceDirectory}/generator/runtime/index')
const { ModuleGenerators } = require('${importSpecifierToNexusPrismaSourceDirectory}/generator/ModuleGenerators/index')
const RuntimeSettings = require('${importSpecifierToNexusPrismaSourceDirectory}/generator/Settings/Runtime/index')
`

return {
Expand All @@ -121,31 +122,32 @@ export function createModuleSpec(params: {
content: dedent`
${imports}

const gentimeSettings = ${JSON.stringify(gentimeSettings.data, null, 2)}
const gentimeSettingsData = ${JSON.stringify(gentimeSettings.data, null, 2)}
const runtimeSettingsManager = RuntimeSettings.settings

const dmmf = getPrismaClientDmmf({
// JSON stringify the values to ensure proper escaping
// Details: https://github.com/prisma/nexus-prisma/issues/143
// TODO test that fails without this code
require: () => require(${JSON.stringify(gentimeSettings.data.prismaClientImportId)}),
importId: gentimeSettings.prismaClientImportId,
importId: gentimeSettingsData.prismaClientImportId,
importIdResolved: require.resolve(${JSON.stringify(gentimeSettings.data.prismaClientImportId)})
})

const models = ModelsGenerator.JS.createNexusTypeDefConfigurations(dmmf, {
runtime: Runtime.settings,
gentime: gentimeSettings,
const nexusTypeDefConfigurations = ModuleGenerators.JS.createNexusTypeDefConfigurations(dmmf, {
gentime: gentimeSettingsData,
runtime: runtimeSettingsManager,
})

${exports}
`,
}
}

export function createNexusTypeDefConfigurations(
export const createNexusTypeDefConfigurations = (
dmmf: DMMF.Document,
settings: Settings
): NexusTypeDefConfigurations {
): NexusTypeDefConfigurations => {
return {
...createNexusObjectTypeDefConfigurations(dmmf, settings),
...createNexusEnumTypeDefConfigurations(dmmf, settings),
Expand All @@ -169,10 +171,10 @@ type NexusObjectTypeDefConfiguration = Record<
/**
* Create Nexus object type definition configurations for Prisma models found in the given DMMF.
*/
function createNexusObjectTypeDefConfigurations(
const createNexusObjectTypeDefConfigurations = (
dmmf: DMMF.Document,
settings: Settings
): NexusObjectTypeDefConfigurations {
): NexusObjectTypeDefConfigurations => {
return chain(dmmf.datamodel.models)
.map((model) => {
return {
Expand Down Expand Up @@ -206,7 +208,7 @@ const prismaNodeDocumentationToDescription = (params: {

// Complex return type I don't really understand how to easily work with manually.
// eslint-disable-next-line
export function prismaFieldToNexusType(field: DMMF.Field, settings: Settings) {
export const prismaFieldToNexusType = (field: DMMF.Field, settings: Settings) => {
const graphqlType = fieldTypeToGraphQLType(field, settings.gentime)

if (field.isList) {
Expand Down Expand Up @@ -246,11 +248,11 @@ export function prismaFieldToNexusType(field: DMMF.Field, settings: Settings) {
*
* but this is overall the better way to handle this detail it seems.
*/
export function nexusResolverFromPrismaField(
export const nexusResolverFromPrismaField = (
model: DMMF.Model,
field: DMMF.Field,
settings: Settings
): undefined | Resolver {
): undefined | Resolver => {
if (field.kind !== 'object') {
return undefined
}
Expand Down Expand Up @@ -374,10 +376,10 @@ type NexusEnumTypeDefConfiguration = AnyNexusEnumTypeConfig
/**
* Create Nexus enum type definition configurations for Prisma enums found in the given DMMF.
*/
function createNexusEnumTypeDefConfigurations(
const createNexusEnumTypeDefConfigurations = (
dmmf: DMMF.Document,
settings: Settings
): NexusEnumTypeDefConfigurations {
): NexusEnumTypeDefConfigurations => {
return chain(dmmf.datamodel.enums)
.map((enum_): AnyNexusEnumTypeConfig => {
return {
Expand Down
Loading