Skip to content

Commit

Permalink
Add support for undefined in union
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnymac committed Apr 3, 2021
1 parent 3d1c3bc commit d9a288c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
8 changes: 8 additions & 0 deletions lib/plugin/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export function isNull(type: Type) {
}
}

export function isUndefined(type: Type) {
if (type.isUnion()) {
return Boolean(type.types.find((t) => hasFlag(t, TypeFlags.Undefined)));
} else {
return hasFlag(type, TypeFlags.Undefined);
}
}

export function hasFlag(type: Type, flag: TypeFlags) {
return (type.flags & flag) === flag;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ts from 'typescript';
import { HideField } from '../../decorators';
import { PluginOptions } from '../merge-options';
import { METADATA_FACTORY_NAME } from '../plugin-constants';
import { findNullableTypeFromUnion, getDescriptionOfNode, isNull } from '../utils/ast-utils';
import { findNullableTypeFromUnion, getDescriptionOfNode, isNull, isUndefined } from '../utils/ast-utils';
import {
getDecoratorOrUndefinedByNames,
getTypeReferenceAsString,
Expand Down Expand Up @@ -135,7 +135,7 @@ export class ModelClassVisitor {
pluginOptions?: PluginOptions,
): ts.ObjectLiteralExpression {
const type = typeChecker.getTypeAtLocation(node);
const isNullable = !!node.questionToken || isNull(type);
const isNullable = !!node.questionToken || isNull(type) || isUndefined(type);

const properties = [
...existingProperties,
Expand Down
2 changes: 1 addition & 1 deletion tests/plugin/fixtures/create-cat-alt.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class CreateCatDto2 {
this.status = Status.ENABLED;
}
static _GRAPHQL_METADATA_FACTORY() {
return { name: { nullable: false, type: () => String, description: "name description" }, age: { nullable: false, type: () => Number, description: "test on age" }, tags: { nullable: false, type: () => [String] }, status: { nullable: false, type: () => Status }, breed: { nullable: true, type: () => String }, nodes: { nullable: false, type: () => [Object] }, alias: { nullable: false, type: () => Object }, numberAlias: { nullable: false, type: () => Number }, union: { nullable: false, type: () => Object }, intersection: { nullable: false, type: () => Object }, optionalBoolean: { nullable: true, type: () => Boolean }, nested: { nullable: false, type: () => ({ first: { nullable: false, type: () => String }, second: { nullable: false, type: () => Number }, status: { nullable: false, type: () => Status }, tags: { nullable: false, type: () => [String] }, nodes: { nullable: false, type: () => [Object] }, alias: { nullable: false, type: () => Object }, numberAlias: { nullable: false, type: () => Number } }) }, tuple: { nullable: false, type: () => Object } };
return { name: { type: () => String, description: "name description" }, age: { type: () => Number, description: "test on age" }, tags: { type: () => [String] }, status: { type: () => Status }, breed: { nullable: true, type: () => String }, nodes: { type: () => [Object] }, alias: { type: () => Object }, numberAlias: { type: () => Number }, union: { type: () => Object }, intersection: { type: () => Object }, optionalBoolean: { nullable: true, type: () => Boolean }, nested: { type: () => ({ first: { type: () => String }, second: { type: () => Number }, status: { type: () => Status }, tags: { type: () => [String] }, nodes: { type: () => [Object] }, alias: { type: () => Object }, numberAlias: { type: () => Number } }) }, tuple: { type: () => Object } };
}
}
`;
2 changes: 1 addition & 1 deletion tests/plugin/fixtures/create-cat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CreateCatDto {
this.status = Status.ENABLED;
}
static _GRAPHQL_METADATA_FACTORY() {
return { name: { nullable: false, type: () => String }, age: { nullable: false, type: () => Number }, tags: { nullable: false, type: () => [String] }, status: { nullable: false, type: () => Status }, status2: { nullable: true, type: () => Status }, statusArr: { nullable: true, type: () => [Status] }, breed: { nullable: true, type: () => String }, nodes: { nullable: false, type: () => [Object] }, date: { nullable: false, type: () => Date } };
return { name: { type: () => String }, age: { type: () => Number }, tags: { type: () => [String] }, status: { type: () => Status }, status2: { nullable: true, type: () => Status }, statusArr: { nullable: true, type: () => [Status] }, breed: { nullable: true, type: () => String }, nodes: { type: () => [Object] }, date: { type: () => Date } };
}
}
__decorate([
Expand Down
2 changes: 1 addition & 1 deletion tests/plugin/fixtures/es5-class.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var CreateCatDtoEs5 = /** @class */ (function () {
this.obj = constants_1.CONSTANT_OBJECT;
}
CreateCatDtoEs5._GRAPHQL_METADATA_FACTORY = function () {
return { name: { nullable: false, type: function () { return String; } }, status: { nullable: false, type: function () { return Object; } }, obj: { nullable: false, type: function () { return Object; } } };
return { name: { type: function () { return String; } }, status: { type: function () { return Object; } }, obj: { type: function () { return Object; } } };
};
return CreateCatDtoEs5;
}());
Expand Down
4 changes: 2 additions & 2 deletions tests/plugin/fixtures/nullable.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class NullableDto {
statusArr?: Status[];
readonly breed?: string;
nodes: Node[];
date: Date;
date: Date | undefined;
}
`;

Expand All @@ -28,7 +28,7 @@ export class NullableDto {
this.status = Status.ENABLED;
}
static _GRAPHQL_METADATA_FACTORY() {
return { name: { nullable: true, type: () => String }, age: { type: () => Number }, tags: { type: () => [String] }, status: { type: () => Status }, status2: { nullable: true, type: () => Status }, statusArr: { nullable: true, type: () => [Status] }, breed: { nullable: true, type: () => String }, nodes: { type: () => [Object] }, date: { type: () => Date } };
return { name: { nullable: true, type: () => String }, age: { type: () => Number }, tags: { type: () => [String] }, status: { type: () => Status }, status2: { nullable: true, type: () => Status }, statusArr: { nullable: true, type: () => [Status] }, breed: { nullable: true, type: () => String }, nodes: { type: () => [Object] }, date: { nullable: true, type: () => Date } };
}
}
`

0 comments on commit d9a288c

Please sign in to comment.