Skip to content

Commit 1b17e9a

Browse files
committed
use the Kind.NON_NULL_TYPE for @Map
1 parent 4e37c3d commit 1b17e9a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

packages/plugins/typescript/mongodb/src/visitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class TsMongoVisitor extends BaseVisitor<TypeScriptMongoPluginConfig, Typ
168168
const type = this.convertName(coreType, { suffix: this.config.dbTypeSuffix });
169169

170170
tree.addField(
171-
mapPath || `${fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
171+
`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
172172
this._variablesTransformer.wrapAstTypeWithModifiers(`${type}['${this.config.idFieldName}']`, fieldNode.type)
173173
);
174174
}
@@ -197,7 +197,7 @@ export class TsMongoVisitor extends BaseVisitor<TypeScriptMongoPluginConfig, Typ
197197
}
198198

199199
tree.addField(
200-
mapPath || `${fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
200+
`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
201201
overrideType || this._variablesTransformer.wrapAstTypeWithModifiers(type, fieldNode.type)
202202
);
203203
}
@@ -212,7 +212,7 @@ export class TsMongoVisitor extends BaseVisitor<TypeScriptMongoPluginConfig, Typ
212212
const type = this.convertName(coreType, { suffix: this.config.dbTypeSuffix });
213213

214214
tree.addField(
215-
mapPath || `${fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
215+
`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
216216
this._variablesTransformer.wrapAstTypeWithModifiers(type, fieldNode.type)
217217
);
218218
}

packages/plugins/typescript/mongodb/tests/typescript-mongo.spec.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ describe('TypeScript Mongo', () => {
3232
nullableEmbedded: [EmbeddedType] @embedded
3333
mappedEmbedded: EmbeddedType @embedded @map(path: "innerEmbedded.moreLevel")
3434
changeName: String @column @map(path: "other_name")
35+
nonNullableColumnMap: String! @column @map(path: "nonNullableColumn")
36+
nullableLinkMap: LinkType @link @map(path: "nullableLinkId")
37+
nullableColumnMapPath: String @column @map(path: "nullableColumnMap.level")
38+
nonNullableColumnMapPath: String! @column @map(path: "nonNullableColumnMap.level")
3539
}
3640
3741
type EmbeddedType @entity {
@@ -229,18 +233,28 @@ describe('TypeScript Mongo', () => {
229233

230234
it('Should output the correct values for @map directive', async () => {
231235
const result = await plugin(schema, [], {}, { outputFile: '' });
232-
expect(result).toContain(`myInnerArray: Maybe<Array<Maybe<number>>>`); // simple @column with array and @map
233-
expect(result).toContain(`other_name: Maybe<string>`); // simple @map scalar
236+
expect(result).toContain(`myInnerArray?: Maybe<Array<Maybe<number>>>`); // simple @column with array and @map
237+
expect(result).toContain(`other_name?: Maybe<string>`); // simple @map scalar
234238
expect(result).toBeSimilarStringTo(`
235239
profile: {
236240
inner: {
237-
field: Maybe<string>,
241+
field?: Maybe<string>,
238242
},
239243
},`); // custom @map with inner fields
240244
expect(result).toBeSimilarStringTo(`
241245
innerEmbedded: {
242-
moreLevel: Maybe<EmbeddedTypeDbObject>,
246+
moreLevel?: Maybe<EmbeddedTypeDbObject>,
243247
},`); // embedded with @map
248+
expect(result).toContain(`nonNullableColumn: string`); // simple @column with @map
249+
expect(result).toContain(`nullableLinkId?: Maybe<LinkTypeDbObject['_id']>`); // nullable @link with @map
250+
expect(result).toBeSimilarStringTo(`
251+
nullableColumnMap: {
252+
level?: Maybe<string>,
253+
},`); // map with ;
254+
expect(result).toBeSimilarStringTo(`
255+
nonNullableColumnMap: {
256+
level: string,
257+
},`);
244258
await validate(result, schema, {});
245259
});
246260

0 commit comments

Comments
 (0)