nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.46k stars 396 forks source link

Apollo cache control - cache hints for code first - default max age #263

Closed websolutions-hamburg closed 5 years ago

websolutions-hamburg commented 5 years ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Is it possible to use the apollo cache control with Nest?
https://www.apollographql.com/docs/apollo-server/features/caching

This is an example code for schema first and the apollo cache control:

type Post @cacheControl(maxAge: 240) {
  id: Int!
  title: String
  author: Author
  votes: Int @cacheControl(maxAge: 30)
  comments: [Comment]
  readByCurrentUser: Boolean! @cacheControl(scope: PRIVATE)
}

Is it possible to use this also with Nest and code first instead of schema first?

I configured my graphql module with this configuration, to use the default max age:

GraphQLModule.forRoot({
  autoSchemaFile: true,
  path: '/api/gql',
  debug: true,
  introspection: true,
  playground: true,
  tracing: true,
  cacheControl: {
    defaultMaxAge: 60,
    stripFormattedExtensions: false,
    calculateHttpHeaders: true
  }
})

This creates the correct response header with cache-control: max-age=60. But it doesn't work. Every request in the next 60 seconds is not cached. All the time my resolvers will be trigered and generate a new response.

# Response Headers:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json
cache-control: max-age=60, public
Content-Length: 873
Date: Wed, 22 May 2019 22:25:41 GMT
Connection: keep-alive
# Request Headers:

POST /api/gql HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 78
accept: */*
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
DNT: 1
content-type: application/json
Referer: http://localhost:3000/api/gql
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7

Environment


@nestjs/core: 6.2.4
@nestjs/graphql: 6.2.1

For Tooling issues:
- Node version: 12.2.0
- Platform: Mac, Linux
arjunyel commented 5 years ago

I could be wrong but I dont think it works, I think because apollo-server-express doesn't do caching, hopefully will be fixed with v3 https://github.com/apollographql/apollo-server/issues/2360

Not fully sure that this is the case though, I haven't found a definite answer :D

arjunyel commented 5 years ago

There's also this, I will try it out when I get a chance https://github.com/19majkel94/type-graphql/blob/v0.17.4/examples/apollo-engine/index.ts

esistgut commented 5 years ago

Did you make any progress on this?

arjunyel commented 5 years ago

Havent had a chance and it might be a while, check this out https://github.com/19majkel94/type-graphql/blob/v0.17.4/examples/apollo-engine/cache-control.ts

jogelin commented 5 years ago

@arjunyel @esistgut any news ? ;)

I tried to put my own cache class when I configure my graphQL module and indeed, it is not used:

    GraphQLModule.forRoot({
      autoSchemaFile: join(process.cwd(), 'apps/api/src/graphql/schema.graphql'),
      tracing: true,
      debug: true,
      cacheControl: true,
      cache: new SchemaCache()
    }),

I tried to add the Decorator CacheControl like the example https://github.com/19majkel94/type-graphql/blob/v0.17.4/examples/apollo-engine/cache-control.ts but nothing happen.

I already put in place the Dataloader to cache/batch my api REST/Database calls when resolving but I would like to cache the model already resolved to re-use it in services...any ideas ?

Thx ;)

kamilmysliwiec commented 5 years ago

Both cache and cacheControl options are being passed down directly to the ApolloServer constructor https://github.com/nestjs/graphql/blob/master/lib/graphql.module.ts#L179. I don't think that this issue is related to @nestjs/graphl to be honest.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.