Search code examples
graphqlgraphql-jsapollo-serverexpress-graphql

Apollo Graphql Import Issue with cacheControl directive


I am using "graphql-import": "^0.7.1"

I tried to add @cacheControl directive to my graphql schema

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

then it was giving this error -

Error: Directive cacheControl: Couldn't find type cacheControl in any of the schemas.

So after taking hints from link -

https://github.com/prisma/graphql-import/issues/153

I added below code

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

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

But after that I started getting this error -

Error: There can be only one type named "CacheControlScope".

Enum value "CacheControlScope.PUBLIC" can only be defined once.

Enum value "CacheControlScope.PRIVATE" can only be defined once.

I am not able to figure out how to fix this issue.


Solution

  • Faced this problem as well and the directive not being found has to due with schema stitching. I used the same work around you used by placing the directive and enum definition in the schema itself. When I encounter that error I had to upgrade to at least 2.6.6 because that is where they added a fix for the dupe error you are getting ref: https://github.com/apollographql/apollo-server/pull/2762