#Convert InputType fields type

9 messages · Page 1 of 1 (latest)

craggy arrow
#

Hey guys, is it possible to convert @InputType() fields? Say I have an InputType ts @InputType() class Foo { @Field() value: string }, is it possible to provide a value as a number, and have it converted to a string?

#

Looking at the docs I suppose I could write a middleware that converts numbers to strings, but is there a more built-in method?

#

Although trying that out doesn't seem to work, I guess the middleware is run after type validation?

strong mortar
#

@Type(() => String) should do it. Though you'll probably have to tell GQL that it's a number like @Field({ type: Number })

craggy arrow
strong mortar
#

Ah, whoops, @Field(() => Int)

#

Int is from @nestjs/graphql

craggy arrow
#

this is all it should take? ```ts

@Field(() => Float)
@Type(() => String)
someFloat: string

strong mortar
#

I believe so. With transform: true set in the ValidationPipe