Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin Typescript mongoDB: generate optional type for @map #4417

Merged
merged 2 commits into from Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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