benawad / type-graphql-series

Typescript GraphQL Server built with TypeGraphQL
326 stars 129 forks source link

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

Closed CoonHouse closed 5 years ago

CoonHouse commented 5 years ago

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

MichalLytek commented 5 years ago

@CoonHouse

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

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

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

Wilco

MichalLytek commented 5 years ago

@CoonHouse _type => String

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

CoonHouse commented 5 years ago

@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

MichalLytek commented 5 years ago

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 _).