Skip to content

Commit 4d19e9a

Browse files
committed
fix(other): Fail model with single id field in mongodb
close: unlight#96
1 parent 6381d04 commit 4d19e9a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/handlers/input-type.ts

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export function inputType(
5656
const modelFieldSettings = model && fieldSettings.get(model.name);
5757
const moduleSpecifier = '@nestjs/graphql';
5858

59+
// console.log('sourceFile.getBaseName()', sourceFile.getBaseName());
60+
5961
importDeclarations
6062
.set('Field', {
6163
namedImports: [{ name: 'Field' }],
@@ -77,6 +79,12 @@ export function inputType(
7779
eventEmitter.emitSync('BeforeGenerateField', field, args);
7880

7981
const { inputTypes, isRequired, name } = field;
82+
83+
if (inputTypes.length === 0) {
84+
// No types
85+
continue;
86+
}
87+
8088
const usePattern = useInputType?.ALL || useInputType?.[name];
8189
const graphqlInputType = getGraphqlInputType(inputTypes, usePattern);
8290
const { isList, location, type } = graphqlInputType;

src/test/generate.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -2557,3 +2557,26 @@ describe('configuration custom scalars', () => {
25572557
});
25582558
});
25592559
});
2560+
2561+
describe('single model and field mongodb', () => {
2562+
before(async () => {
2563+
({ project, sourceFiles } = await testGenerate({
2564+
provider: 'mongodb',
2565+
schema: `
2566+
model Product {
2567+
id String @id @default(auto()) @map("_id") @db.ObjectId
2568+
}
2569+
`,
2570+
options: [`outputFilePattern = "{name}.{type}.ts"`],
2571+
}));
2572+
});
2573+
2574+
it('example input update type', () => {
2575+
const s = testSourceFile({
2576+
project,
2577+
file: 'update-one-product.args.ts',
2578+
});
2579+
// data field is missing because of single id field
2580+
expect(s.classFile.getProperties()).toHaveLength(1);
2581+
});
2582+
});

src/test/test-generate.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ const { '@prisma/generator-helper': generatorVersion } =
1919
export async function testGenerate(args: {
2020
schema: string;
2121
options?: string[];
22+
provider?: 'postgresql' | 'mongodb';
2223
createSouceFile?: {
2324
text: string;
2425
name: string;
2526
type: string;
2627
};
2728
onConnect?: (emitter: AwaitEventEmitter) => void;
2829
}) {
29-
const { schema, options, createSouceFile, onConnect } = args;
30+
const { schema, options, provider, createSouceFile, onConnect } = args;
3031
let project: Project | undefined;
3132
const connectCallback = (emitter: AwaitEventEmitter) => {
3233
onConnect && onConnect(emitter);
@@ -53,7 +54,7 @@ export async function testGenerate(args: {
5354
});
5455
};
5556
await generate({
56-
...(await createGeneratorOptions(schema, options)),
57+
...(await createGeneratorOptions(schema, options, provider)),
5758
skipAddOutputSourceFiles: true,
5859
connectCallback,
5960
});
@@ -119,10 +120,11 @@ export async function testGenerate(args: {
119120
async function createGeneratorOptions(
120121
schema: string,
121122
options?: string[],
123+
provider: 'postgresql' | 'mongodb' = 'postgresql',
122124
): Promise<GeneratorOptions & { prismaClientDmmf: DMMF.Document }> {
123125
const schemaHeader = `
124126
datasource db {
125-
provider = "postgresql"
127+
provider = "${provider}"
126128
url = env("DATABASE_URL")
127129
}
128130
generator client {

0 commit comments

Comments
 (0)