benawad/type-graphql-series

[Question] async/await or Promises in @Field decorator

Closed this issue · 5 comments

Thanks for your great tutorial!

I have in my "User-entity" a field like the "name" field you create at about minute 2:30 of video 3, but I am returning a value from Redis. So it is actually returning a promise in stead of a string. It is working, but I get an TypeScript error saying:

"Type 'Promise<string | null>' is not assignable to type 'string'."

  @Field()
  userName(@Root() parent: User): string {
    return redis.get(usernamePrefix + parent._id)
  }

Do you know how to properly return the Redis output?
So, it is working, I am just wondering if there is a better way or how to get rid of the TypeSript Error.

Thanks and regards,

Wilco

@CoonHouse

  @Field(type => String, { nullable: true })
  userName(@Root() parent: User): Promise<string | null> {
    return redis.get(usernamePrefix + parent._id)
  }

@19majkel94
Thanks! that did it!
I only get the TypeScript error:

'type' is declared but its value is never read.

Wilco

@CoonHouse
_type => String

You should check your tsconfig.json and find out what the flags like noUnusedParameters do.

@19majkel94
Thank you!
Yes, I have noUnusedParameters set to true because it makes sense to me, not to include parameters that are not used. Isn't it? Would you recommend to turn it off?

Regards,

Wilco

Would you recommend to turn it off?

No, you just need to know what it checks, how it checks and how to suppress the error (using _).