Skip to content

Commit 1e1bee3

Browse files
committed
fix: Compatibility Issues with Prisma 5
fix: unlight#179
1 parent 137d0d3 commit 1e1bee3

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/handlers/input-type.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { getPropertyType } from '../helpers/get-property-type';
1010
import { ImportDeclarationMap } from '../helpers/import-declaration-map';
1111
import { propertyStructure } from '../helpers/property-structure';
1212
import { EventArguments, InputType } from '../types';
13+
import { isWhereUniqueInputType } from '../helpers/is-where-unique-input-type';
14+
import { getWhereUniqueAtLeastKeys } from '../helpers/get-where-unique-at-least-keys';
1315

1416
export function inputType(
1517
args: EventArguments & {
@@ -57,8 +59,6 @@ export function inputType(
5759
const modelFieldSettings = model && fieldSettings.get(model.name);
5860
const moduleSpecifier = '@nestjs/graphql';
5961

60-
// console.log('sourceFile.getBaseName()', sourceFile.getBaseName());
61-
6262
importDeclarations
6363
.set('Field', {
6464
namedImports: [{ name: 'Field' }],
@@ -96,8 +96,13 @@ export function inputType(
9696
});
9797
const modelField = model?.fields.find(f => f.name === name);
9898
const isCustomsApplicable = typeName === modelField?.type;
99+
const whereUniqueInputType =
100+
isWhereUniqueInputType(typeName) &&
101+
model &&
102+
`Prisma.AtLeast<${typeName}, ${getWhereUniqueAtLeastKeys(model)}>`;
99103
const propertyType = castArray(
100104
propertySettings?.name ||
105+
whereUniqueInputType ||
101106
getPropertyType({
102107
location,
103108
type: typeName,
@@ -115,7 +120,10 @@ export function inputType(
115120
if (propertySettings) {
116121
importDeclarations.create({ ...propertySettings });
117122
} else if (propertyType.includes('Decimal')) {
123+
// TODO: Deprecated and should be removed
118124
importDeclarations.add('Decimal', '@prisma/client/runtime/library');
125+
} else if (propertyType.some(p => p.startsWith('Prisma.'))) {
126+
importDeclarations.add('Prisma', '@prisma/client');
119127
}
120128

121129
// Get graphql type
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { DMMF } from '../types';
2+
3+
export function getWhereUniqueAtLeastKeys(model: DMMF.Model) {
4+
const names = model.fields
5+
.filter(field => field.isUnique || field.isId)
6+
.map(field => field.name);
7+
8+
for (const uniqueIndex of model.uniqueIndexes) {
9+
const name = uniqueIndex.name || uniqueIndex.fields.join('_');
10+
11+
names.push(name);
12+
}
13+
14+
return names.map(name => `'${name}'`).join(' | ');
15+
}

src/test/generate.spec.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,16 @@ describe('model with one id int', () => {
316316

317317
expect(s.classFile.getName()).toEqual('UserAggregateArgs');
318318
});
319+
320+
it('where uniq must be wrapped to prisma atleast', () => {
321+
const s = testSourceFile({
322+
project,
323+
class: 'FindManyUserArgs',
324+
property: 'cursor',
325+
});
326+
327+
expect(s.property?.type).toEqual(`Prisma.AtLeast<UserWhereUniqueInput, 'id'>`);
328+
});
319329
});
320330

321331
describe('duplicated', () => {
@@ -1877,7 +1887,7 @@ describe('object model options', () => {
18771887
});
18781888

18791889
describe('compound index', () => {
1880-
it('user unique input compound', async () => {
1890+
before(async () => {
18811891
({ project, sourceFiles } = await testGenerate({
18821892
schema: `
18831893
model User {
@@ -1899,10 +1909,29 @@ describe('compound index', () => {
18991909
`fields_Validator_input = true`,
19001910
],
19011911
}));
1902-
setSourceFile('user-email-name-compound-unique.input.ts');
1903-
const minLength = classFile.getProperty('name')?.getDecorator('MinLength');
1912+
});
1913+
1914+
it('user unique input compound', () => {
1915+
const s = testSourceFile({
1916+
project,
1917+
class: 'UserEmailNameCompoundUniqueInput',
1918+
});
1919+
1920+
const minLength = s.classFile.getProperty('name')?.getDecorator('MinLength');
19041921
expect(minLength?.getText()).toEqual('@Validator.MinLength(3)');
19051922
});
1923+
1924+
it('compound uniq where must be wrapped to prisma atleast', () => {
1925+
const s = testSourceFile({
1926+
project,
1927+
class: 'FindManyUserArgs',
1928+
property: 'cursor',
1929+
});
1930+
1931+
expect(s.property?.type).toEqual(
1932+
`Prisma.AtLeast<UserWhereUniqueInput, 'id' | 'email_name'>`,
1933+
);
1934+
});
19061935
});
19071936

19081937
describe('field type', () => {

0 commit comments

Comments
 (0)