From ef6d560eceef8b17183a3724449d20563ebab5d2 Mon Sep 17 00:00:00 2001 From: Dewey Jose Date: Fri, 29 Mar 2019 11:30:58 -0400 Subject: [PATCH] feat(float_decorator): support float decorator --- examples/1-simple-model/src/user.model.ts | 13 +++++++++- examples/1-simple-model/src/user.resolver.ts | 13 ++++++++-- src/decorators/FloatField.ts | 27 ++++++++++++++++++++ src/decorators/index.ts | 1 + src/schema/SchemaGenerator.ts | 2 +- src/schema/TypeORMConverter.ts | 1 + 6 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/decorators/FloatField.ts diff --git a/examples/1-simple-model/src/user.model.ts b/examples/1-simple-model/src/user.model.ts index 6890492d..07c67474 100644 --- a/examples/1-simple-model/src/user.model.ts +++ b/examples/1-simple-model/src/user.model.ts @@ -1,4 +1,12 @@ -import { BaseModel, BooleanField, EmailField, IntField, Model, StringField } from '../../../src'; +import { + BaseModel, + BooleanField, + EmailField, + FloatField, + IntField, + Model, + StringField +} from '../../../src'; @Model() export class User extends BaseModel { @@ -16,4 +24,7 @@ export class User extends BaseModel { @BooleanField() isRequired?: boolean; + + @FloatField() + rating?: number; } diff --git a/examples/1-simple-model/src/user.resolver.ts b/examples/1-simple-model/src/user.resolver.ts index ea27d315..700ebe6b 100644 --- a/examples/1-simple-model/src/user.resolver.ts +++ b/examples/1-simple-model/src/user.resolver.ts @@ -4,7 +4,13 @@ import { Repository } from 'typeorm'; import { InjectRepository } from 'typeorm-typedi-extensions'; import { BaseContext, BaseResolver, StandardDeleteResponse } from '../../../src'; -import { UserCreateInput, UserUpdateArgs, UserWhereArgs, UserWhereInput, UserWhereUniqueInput } from '../generated'; +import { + UserCreateInput, + UserUpdateArgs, + UserWhereArgs, + UserWhereInput, + UserWhereUniqueInput +} from '../generated/classes'; import { User } from './user.model'; @@ -36,7 +42,10 @@ export class UserResolver extends BaseResolver { } @Mutation(returns => User) - async updateUser(@Args() { data, where }: UserUpdateArgs, @Ctx() ctx: BaseContext): Promise { + async updateUser( + @Args() { data, where }: UserUpdateArgs, + @Ctx() ctx: BaseContext + ): Promise { return this.update(data, where, 'fakeid1'); } diff --git a/src/decorators/FloatField.ts b/src/decorators/FloatField.ts new file mode 100644 index 00000000..202e4055 --- /dev/null +++ b/src/decorators/FloatField.ts @@ -0,0 +1,27 @@ +import { Field, Float } from 'type-graphql'; +import { Column } from 'typeorm'; + +import { composeMethodDecorators, MethodDecoratorFactory } from '../utils'; + +interface FloatFieldOptions { + nullable?: boolean; +} + +export function FloatField(args: FloatFieldOptions = {}): any { + const nullableOption = args.nullable === true ? { nullable: true } : {}; + + // These are the 2 required decorators to get type-graphql and typeorm working + const factories = [ + // We explicitly say string here because when we're metaprogramming without + // TS types, Field does not know that this should be a String + Field(type => Float, { + ...nullableOption + }), + Column({ + type: 'float8', + ...nullableOption + }) as MethodDecoratorFactory + ]; + + return composeMethodDecorators(...factories); +} diff --git a/src/decorators/index.ts b/src/decorators/index.ts index 26de46b9..cdad7a30 100644 --- a/src/decorators/index.ts +++ b/src/decorators/index.ts @@ -9,3 +9,4 @@ export * from './ManyToOne'; export * from './OneToMany'; export * from './IntField'; export * from './BooleanField'; +export * from './FloatField'; diff --git a/src/schema/SchemaGenerator.ts b/src/schema/SchemaGenerator.ts index d86b94e9..7cdffad0 100644 --- a/src/schema/SchemaGenerator.ts +++ b/src/schema/SchemaGenerator.ts @@ -29,7 +29,7 @@ export class SchemaGenerator { // This file has been auto-generated by Warthog. Do not update directly as it // will be re-written. If you need to change this file, update models or add // new TypeGraphQL objects - import { ArgsType, Field as TypeGraphQLField, ID, InputType as TypeGraphQLInputType, Int } from 'type-graphql'; + import { ArgsType, Field as TypeGraphQLField, Float, ID, InputType as TypeGraphQLInputType, Int } from 'type-graphql'; import { registerEnumType } from 'type-graphql'; import { BaseWhereInput, PaginationArgs } from '${warthogImportPath}'; ${entityListToImports(entities).join('')} diff --git a/src/schema/TypeORMConverter.ts b/src/schema/TypeORMConverter.ts index c310d3d7..48083859 100644 --- a/src/schema/TypeORMConverter.ts +++ b/src/schema/TypeORMConverter.ts @@ -346,6 +346,7 @@ export function columnToTypeScriptType(column: ColumnMetadata): string { const typeMap: any = { Boolean: 'boolean', DateTime: 'string', + Float: 'number', ID: 'string', // TODO: should this be ID_TYPE? Int: 'number', String: 'string'