Skip to content

Commit

Permalink
Merge pull request #77 from goldcaddy77/float_decorator_support
Browse files Browse the repository at this point in the history
feat(float_decorator): support float decorator
  • Loading branch information
deweyjose authored Mar 29, 2019
2 parents 32673c3 + ef6d560 commit 9f1e29b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
13 changes: 12 additions & 1 deletion examples/1-simple-model/src/user.model.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,4 +24,7 @@ export class User extends BaseModel {

@BooleanField()
isRequired?: boolean;

@FloatField()
rating?: number;
}
13 changes: 11 additions & 2 deletions examples/1-simple-model/src/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -36,7 +42,10 @@ export class UserResolver extends BaseResolver<User> {
}

@Mutation(returns => User)
async updateUser(@Args() { data, where }: UserUpdateArgs, @Ctx() ctx: BaseContext): Promise<User> {
async updateUser(
@Args() { data, where }: UserUpdateArgs,
@Ctx() ctx: BaseContext
): Promise<User> {
return this.update(data, where, 'fakeid1');
}

Expand Down
27 changes: 27 additions & 0 deletions src/decorators/FloatField.ts
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './ManyToOne';
export * from './OneToMany';
export * from './IntField';
export * from './BooleanField';
export * from './FloatField';
2 changes: 1 addition & 1 deletion src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('')}
Expand Down
1 change: 1 addition & 0 deletions src/schema/TypeORMConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 9f1e29b

Please sign in to comment.