Skip to content

Commit

Permalink
fix(other): Fail model with single id field in mongodb
Browse files Browse the repository at this point in the history
close: unlight#96
  • Loading branch information
unlight committed Apr 12, 2022
1 parent 6381d04 commit 4d19e9a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/handlers/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export function inputType(
const modelFieldSettings = model && fieldSettings.get(model.name);
const moduleSpecifier = '@nestjs/graphql';

// console.log('sourceFile.getBaseName()', sourceFile.getBaseName());

importDeclarations
.set('Field', {
namedImports: [{ name: 'Field' }],
Expand All @@ -77,6 +79,12 @@ export function inputType(
eventEmitter.emitSync('BeforeGenerateField', field, args);

const { inputTypes, isRequired, name } = field;

if (inputTypes.length === 0) {
// No types
continue;
}

const usePattern = useInputType?.ALL || useInputType?.[name];
const graphqlInputType = getGraphqlInputType(inputTypes, usePattern);
const { isList, location, type } = graphqlInputType;
Expand Down
23 changes: 23 additions & 0 deletions src/test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2557,3 +2557,26 @@ describe('configuration custom scalars', () => {
});
});
});

describe('single model and field mongodb', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
provider: 'mongodb',
schema: `
model Product {
id String @id @default(auto()) @map("_id") @db.ObjectId
}
`,
options: [`outputFilePattern = "{name}.{type}.ts"`],
}));
});

it('example input update type', () => {
const s = testSourceFile({
project,
file: 'update-one-product.args.ts',
});
// data field is missing because of single id field
expect(s.classFile.getProperties()).toHaveLength(1);
});
});
8 changes: 5 additions & 3 deletions src/test/test-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ const { '@prisma/generator-helper': generatorVersion } =
export async function testGenerate(args: {
schema: string;
options?: string[];
provider?: 'postgresql' | 'mongodb';
createSouceFile?: {
text: string;
name: string;
type: string;
};
onConnect?: (emitter: AwaitEventEmitter) => void;
}) {
const { schema, options, createSouceFile, onConnect } = args;
const { schema, options, provider, createSouceFile, onConnect } = args;
let project: Project | undefined;
const connectCallback = (emitter: AwaitEventEmitter) => {
onConnect && onConnect(emitter);
Expand All @@ -53,7 +54,7 @@ export async function testGenerate(args: {
});
};
await generate({
...(await createGeneratorOptions(schema, options)),
...(await createGeneratorOptions(schema, options, provider)),
skipAddOutputSourceFiles: true,
connectCallback,
});
Expand Down Expand Up @@ -119,10 +120,11 @@ export async function testGenerate(args: {
async function createGeneratorOptions(
schema: string,
options?: string[],
provider: 'postgresql' | 'mongodb' = 'postgresql',
): Promise<GeneratorOptions & { prismaClientDmmf: DMMF.Document }> {
const schemaHeader = `
datasource db {
provider = "postgresql"
provider = "${provider}"
url = env("DATABASE_URL")
}
generator client {
Expand Down

0 comments on commit 4d19e9a

Please sign in to comment.