Skip to content

Commit

Permalink
Fix plugin Typescript mongoDB: generate optional type for @Map (#4417)
Browse files Browse the repository at this point in the history
* use the Kind.NON_NULL_TYPE for @Map

* add comment
  • Loading branch information
xiaoliu-heng committed Jul 23, 2020
1 parent 48e0932 commit c2f94b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/plugins/typescript/mongodb/src/visitor.ts
Expand Up @@ -168,7 +168,7 @@ export class TsMongoVisitor extends BaseVisitor<TypeScriptMongoPluginConfig, Typ
const type = this.convertName(coreType, { suffix: this.config.dbTypeSuffix });

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

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

tree.addField(
mapPath || `${fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`,
this._variablesTransformer.wrapAstTypeWithModifiers(type, fieldNode.type)
);
}
Expand Down
22 changes: 18 additions & 4 deletions packages/plugins/typescript/mongodb/tests/typescript-mongo.spec.ts
Expand Up @@ -32,6 +32,10 @@ describe('TypeScript Mongo', () => {
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 {
Expand Down Expand Up @@ -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<Array<Maybe<number>>>`); // simple @column with array and @map
expect(result).toContain(`other_name: Maybe<string>`); // simple @map scalar
expect(result).toContain(`myInnerArray?: Maybe<Array<Maybe<number>>>`); // simple @column with array and @map
expect(result).toContain(`other_name?: Maybe<string>`); // simple @map scalar
expect(result).toBeSimilarStringTo(`
profile: {
inner: {
field: Maybe<string>,
field?: Maybe<string>,
},
},`); // custom @map with inner fields
expect(result).toBeSimilarStringTo(`
innerEmbedded: {
moreLevel: Maybe<EmbeddedTypeDbObject>,
moreLevel?: Maybe<EmbeddedTypeDbObject>,
},`); // embedded with @map
expect(result).toContain(`nonNullableColumn: string`); // simple @column with @map
expect(result).toContain(`nullableLinkId?: Maybe<LinkTypeDbObject['_id']>`); // nullable @link with @map
expect(result).toBeSimilarStringTo(`
nullableColumnMap: {
level?: Maybe<string>,
},`); // map with nullable field;
expect(result).toBeSimilarStringTo(`
nonNullableColumnMap: {
level: string,
},`); // map with non-nullable field
await validate(result, schema, {});
});

Expand Down

0 comments on commit c2f94b0

Please sign in to comment.