Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement caching on apollo-server-hapi graphql

i have graphql with apollo-server-hapi. i try to add cache control like below:

const graphqlOptions = {
  schema,
  tracing: true,
  cacheControl: true,
};

but when i try to add cache option on schema base:

type Author @cacheControl(maxAge: 60) {
  id: Int
  firstName: String
  lastName: String
  posts: [Post]
}

i got this error message:

Error: Unknown directive "cacheControl".

can you help, what is the correct way to apply cache control on schema?

i follow instruction from below, but it seem didn't work.

apollo-cache-control

like image 292
hbinduni Avatar asked Jun 24 '26 17:06

hbinduni


1 Answers

After learn more about caching on apollo graphql, basically, the issue was with makeExecutableSchema from apollo-server-hapi, didn't include directive for @cacheControl, so to make it work, we just need to define our own @cacheControl directive into graphql file, as below:

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

directive @cacheControl (
  maxAge: Int
  scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE

type Author @cacheControl(maxAge: 60) {
  id: Int
  firstName: String
  lastName: String
  posts: [Post]
}
like image 170
hbinduni Avatar answered Jun 28 '26 05:06

hbinduni