How to set Apollo's cacheControl Directive in Nestjs code-first

Viewed 194

I am trying to set a static cacheControl on some fields, as done here

From my understanding, I need to use a directive, so I used the following nest documentation to declare directives

So, I built a cacheControl directive, this is what my GraphQLModule.forRootAsync has in the buildSchemaOptions:

buildSchemaOptions: {
        directives: [
          new GraphQLDirective({
            name: 'cacheControl',
            locations: [
              DirectiveLocation.FIELD_DEFINITION,
              DirectiveLocation.OBJECT,
              DirectiveLocation.INTERFACE,
              DirectiveLocation.UNION
            ],
            args: {
              maxAge: { type: GraphQLInt },
              scope: {
                type: new GraphQLEnumType({
                  name: 'CacheControlScope',
                  values: {
                    PUBLIC: {
                      astNode: {
                        kind: 'EnumValueDefinition',
                        description: undefined,
                        name: {
                          kind: 'Name',
                          value: 'PUBLIC'
                        },
                        directives: []
                      }
                    },
                    PRIVATE: {
                      astNode: {
                        kind: 'EnumValueDefinition',
                        description: undefined,
                        name: {
                          kind: 'Name',
                          value: 'PRIVATE'
                        },
                        directives: []
                      }
                    }
                  }
                })
              },
              inheritMaxAge: { type: GraphQLBoolean }
            }
          })
        ]
      } 

And it did create the directive in the schema:

directive @cacheControl(maxAge: Int, scope: CacheControlScope, inheritMaxAge: Boolean) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

Now, I try to use it on my field declaration in my @ObjectType like so:

import { Directive, Field, Int, ObjectType } from '@nestjs/graphql'

@ObjectType('User')
export class User {
  @Directive('@cacheControl(maxAge:60)')
  @Field(() => Int)
  id!: number

  @Directive('@cacheControl(maxAge:60)')
  @Field()
  text!: string
}

But when doing some query to get all users, it does not seem to cache anything - and does not send a cache-control header and does the whole query each time. I tried to do a transformer, but not sure how to implement caching for the resolvers. What am I missing?

0 Answers
Related