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: remove bad prisma client on ctx check & export $settings #60

Merged
merged 11 commits into from
May 25, 2021
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ query {
$settings({
prismaClientImportId: '@my/custom/thing',
})
```

### Generator Settings

Expand Down
6 changes: 3 additions & 3 deletions src/generator/gentime/settingsSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export namespace Gentime {
* @default 'Int'
*/
projectIdIntToGraphQL?: 'ID' | 'Int'
// TODO add some examples
// TODO add some examples
/**
* Should Prisma Schema docs propagate as docs?
*
Expand Down Expand Up @@ -82,7 +82,7 @@ export namespace Gentime {
},
})

export function changeSettings(input: Setset.UserInput<SettingsInput>): Settings {
return settings.change(input)
export function changeSettings(input: Setset.UserInput<SettingsInput>): void {
settings.change(input)
}
}
20 changes: 18 additions & 2 deletions src/generator/models/declaration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DMMF } from '@prisma/generator-helper'
import dedent from 'dindist'
import * as OS from 'os'
import { LiteralUnion } from 'type-fest'
import { StandardGraphQLScalarType, StandardgraphQLScalarTypes } from '../../helpers/graphql'
import { PrismaScalarType } from '../../helpers/prisma'
Expand Down Expand Up @@ -32,7 +33,8 @@ export function renderTypeScriptDeclarationForDocumentModels(
const models = dmmf.datamodel.models
const enums = dmmf.datamodel.enums

return dedent`
return (
dedent`
import * as Nexus from 'nexus'
import * as NexusCore from 'nexus/dist/core'

Expand Down Expand Up @@ -116,7 +118,21 @@ export function renderTypeScriptDeclarationForDocumentModels(
})
.join('\n\n')
}
`

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
` + OS.EOL
)
}

function renderTypeScriptDeclarationForEnum(enum_: DMMF.DatamodelEnum, settings: Gentime.Settings): string {
Expand Down
14 changes: 6 additions & 8 deletions src/generator/models/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export function createModuleSpec(gentimeSettings: Gentime.Settings): ModuleSpec
}
})

module.exports = models
const moduleExports = {
...models,
$settings: Runtime.settings.change,
}

module.exports = moduleExports
`,
}
}
Expand Down Expand Up @@ -170,13 +175,6 @@ export function prismaFieldToNexusResolver(
}

return (root: RecordUnknown, _args: RecordUnknown, ctx: RecordUnknown): MaybePromise<unknown> => {
if (!ctx.prisma) {
// TODO rich errors
throw new Error(
'Prisma client not found in context. Set a Prisma client instance to `prisma` field of Nexus context'
)
}

const uniqueIdentifiers = resolveUniqueIdentifiers(model)
const missingIdentifiers = findMissingUniqueIdentifiers(root, uniqueIdentifiers)

Expand Down
4 changes: 2 additions & 2 deletions src/generator/runtime/settingsSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export namespace Runtime {
},
})

export function changeSettings(input: Setset.UserInput<SettingsInput>): Settings {
return settings.change(input)
export function changeSettings(input: Setset.UserInput<SettingsInput>): void {
settings.change(input)
}
}
8 changes: 7 additions & 1 deletion tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ it('When bundled custom scalars are used the project type checks and generates e
require('dotenv').config()

import { makeSchema, objectType, enumType, queryType } from 'nexus'
import { Bar, Foo, SomeEnumA } from 'nexus-prisma'
import { Bar, Foo, SomeEnumA, $settings } from 'nexus-prisma'
import * as customScalars from 'nexus-prisma/scalars'
import * as Path from 'path'

// Show that we can import and call settings as a NOOP
$settings({})

const types = [
customScalars,
Expand Down Expand Up @@ -294,6 +297,9 @@ it('When bundled custom scalars are used the project type checks and generates e

expect(results.runFirstBuild.exitCode).toBe(2)

expect(stripAnsi(results.runFirstBuild.stdout)).toMatch(
/.*error TS2305: Module '"nexus-prisma"' has no exported member '\$settings'.*/
)
expect(stripAnsi(results.runFirstBuild.stdout)).toMatch(
/.*error TS2305: Module '"nexus-prisma"' has no exported member 'Bar'.*/
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,21 @@ declare namespace $Types {
*
* enumType(Foo)
*/
export const Foo: $Types.Foo"
export const Foo: $Types.Foo

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;

exports[`When prisma enum has documentation then it is used for JSDoc and GraphQL enum description: index.d.ts 1`] = `
Expand Down Expand Up @@ -191,5 +205,19 @@ declare namespace $Types {
*
* enumType(Foo)
*/
export const Foo: $Types.Foo"
export const Foo: $Types.Foo

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,21 @@ declare namespace $Types {
*
* enumType(Foo)
*/
export const Foo: $Types.Foo"
export const Foo: $Types.Foo

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;

exports[`When an enum has no documentation comment, then it gets the default JSDoc and its description field is null: index.d.ts 1`] = `
Expand Down Expand Up @@ -191,5 +205,19 @@ declare namespace $Types {
*
* enumType(Foo)
*/
export const Foo: $Types.Foo"
export const Foo: $Types.Foo

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,21 @@ export const SomeModel: $Types.SomeModel
//
//

// N/A –– You have not defined any models in your Prisma schema file."
// N/A –– You have not defined any models in your Prisma schema file.

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;

exports[`When a model field has no documentation comment, then it gets the default JSDoc and its description field is null: index.d.ts 1`] = `
Expand Down Expand Up @@ -329,7 +343,21 @@ export const SomeModel: $Types.SomeModel
//
//

// N/A –– You have not defined any models in your Prisma schema file."
// N/A –– You have not defined any models in your Prisma schema file.

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;

exports[`When a model has a documentation comment, then it is used for the JSDoc of that model and its $description field: index.d.ts 1`] = `
Expand Down Expand Up @@ -475,7 +503,21 @@ export const SomeModel: $Types.SomeModel
//
//

// N/A –– You have not defined any models in your Prisma schema file."
// N/A –– You have not defined any models in your Prisma schema file.

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;

exports[`When a model has no documentation comment, then it gets the default JSDoc and its description field is null: index.d.ts 1`] = `
Expand Down Expand Up @@ -647,5 +689,19 @@ export const SomeModel: $Types.SomeModel
//
//

// N/A –– You have not defined any models in your Prisma schema file."
// N/A –– You have not defined any models in your Prisma schema file.

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,19 @@ export const Post: $Types.Post
//
//

// N/A –– You have not defined any models in your Prisma schema file."
// N/A –– You have not defined any models in your Prisma schema file.

//
//
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
// EXPORTS: OTHER
//
//

import { Runtime } from '../generator/runtime/settingsSingleton'

export const $settings: typeof Runtime.changeSettings
"
`;
Loading