Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
websolutions-hamburg opened this issue May 22, 2019 · 7 comments

Comments

@websolutions-hamburg
Copy link

websolutions-hamburg commented May 22, 2019

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
Copy link

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 apollographql/apollo-server#2360

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

@arjunyel
Copy link

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
Copy link

Did you make any progress on this?

@arjunyel
Copy link

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
Copy link

jogelin commented Jul 3, 2019

@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
Copy link
Member

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
Copy link

lock bot commented Apr 25, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants