From c2f94b010280e98baeeab277b7cf79dde250a94c Mon Sep 17 00:00:00 2001 From: xiaoliu Date: Thu, 23 Jul 2020 18:45:46 +0800 Subject: [PATCH] Fix plugin Typescript mongoDB: generate optional type for @map (#4417) * use the Kind.NON_NULL_TYPE for @map * add comment --- .../plugins/typescript/mongodb/src/visitor.ts | 6 ++--- .../mongodb/tests/typescript-mongo.spec.ts | 22 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/plugins/typescript/mongodb/src/visitor.ts b/packages/plugins/typescript/mongodb/src/visitor.ts index 515dd7afd7b..dfe48e6d05f 100644 --- a/packages/plugins/typescript/mongodb/src/visitor.ts +++ b/packages/plugins/typescript/mongodb/src/visitor.ts @@ -168,7 +168,7 @@ export class TsMongoVisitor extends BaseVisitor { nullableEmbedded: [EmbeddedType] @embedded mappedEmbedded: EmbeddedType @embedded @map(path: "innerEmbedded.moreLevel") changeName: String @column @map(path: "other_name") + nonNullableColumnMap: String! @column @map(path: "nonNullableColumn") + nullableLinkMap: LinkType @link @map(path: "nullableLinkId") + nullableColumnMapPath: String @column @map(path: "nullableColumnMap.level") + nonNullableColumnMapPath: String! @column @map(path: "nonNullableColumnMap.level") } type EmbeddedType @entity { @@ -229,18 +233,28 @@ describe('TypeScript Mongo', () => { it('Should output the correct values for @map directive', async () => { const result = await plugin(schema, [], {}, { outputFile: '' }); - expect(result).toContain(`myInnerArray: Maybe>>`); // simple @column with array and @map - expect(result).toContain(`other_name: Maybe`); // simple @map scalar + expect(result).toContain(`myInnerArray?: Maybe>>`); // simple @column with array and @map + expect(result).toContain(`other_name?: Maybe`); // simple @map scalar expect(result).toBeSimilarStringTo(` profile: { inner: { - field: Maybe, + field?: Maybe, }, },`); // custom @map with inner fields expect(result).toBeSimilarStringTo(` innerEmbedded: { - moreLevel: Maybe, + moreLevel?: Maybe, },`); // embedded with @map + expect(result).toContain(`nonNullableColumn: string`); // simple @column with @map + expect(result).toContain(`nullableLinkId?: Maybe`); // nullable @link with @map + expect(result).toBeSimilarStringTo(` + nullableColumnMap: { + level?: Maybe, + },`); // map with nullable field; + expect(result).toBeSimilarStringTo(` + nonNullableColumnMap: { + level: string, + },`); // map with non-nullable field await validate(result, schema, {}); });