type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

Documentation for `@Info` decorator

Open apoco opened this issue 2 years ago • 5 comments

When trying to figure out how to get information about a request (like which fields are being requested), I searched through documentation but didn't find anything. On Stack Overflow I found out about the @Info decorator, but I'm not seeing where that's described.

Is information about @Info hard to find or is it undocumented?

apoco avatar Feb 10 '22 02:02 apoco

Maybe it's undocumented, I bet 1% of users need access to the GraphQLResolveInfo.

MichalLytek avatar Feb 10 '22 07:02 MichalLytek

@MichalLytek You need the @Info decorator, when you implement your own caching. I have several projects, where caching is implemented in the datasources, so you need to handover info.cacheControl.cacheHint from the query to your datasource when handling queries. I just assumed the @Info decorator is there and works as @context and @args, so I created a interface like I did for @context and it worked as expected (you can of course also directly use the ResolveInfo class: @Info() info: GraphQLResolveInfo,). For new or unexperienced users and to be complete with the GraphQL implementation documentations (e.g. Apollo), a documentation update would be highly appreciated :)

itpropro avatar Feb 27 '22 21:02 itpropro

@itpropro, did this actually work for you? I'm unable to get it to properly work without the @Directive decorator on the response, which seems unnecessary.

For example:

@Resolver()
export class AllPostsResolver {
  @Query(() => AllPostsResponse)
  async AllPosts(
    @Arg('category', () => String, { defaultValue: null, nullable: true })
    @Info() info: GraphQLResolveInfo,
  ) {
    // cache response for 10 minutes
    info.cacheControl.setCacheHint({ maxAge: 600, scope: CacheScope.Public });
   // ...get posts below...

I would have expected that to work, but it does not.

1thrasher avatar Apr 16 '22 14:04 1thrasher

How can I get the GraphQLResolveInfo object inside a parameter or method decorator?

arthurfiorette avatar Apr 24 '22 15:04 arthurfiorette

@arthurfiorette { root, args, context, info }

MichalLytek avatar Apr 24 '22 17:04 MichalLytek