3

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.

WitVault
  • 19,604
  • 18
  • 88
  • 116
  • Can you try to put the cacheDirective on a directives.graphql and then import it ( # import cacheControl from 'directives.graphql' ) and see if it works ? – jgoday Mar 18 '19 at 13:13
  • @jgoday I tried that as well but still not working – WitVault Mar 20 '19 at 11:18

3 Answers3

2

Static hints are giving me the same errors, so I have tried with dynamic ones inside resolvers and it works.

Regarding to Apollo Docs:

const resolvers = {
  Query: {
    post: (_, { id }, _, info) => {
      info.cacheControl.setCacheHint({ maxAge: 60, scope: 'PRIVATE' });
      return find(posts, { id });
    }
  }
}

cache control

BartusZak
  • 593
  • 7
  • 20
1

Where do you declare those enum and directive? I kept getting these errors just because I've put them into a typedef file that was being referenced more than once. Then I've just moved this code to my main schema file

const CacheControl = gql`
    enum CacheControlScope {
        PUBLIC
        PRIVATE
    }

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

const typeDefs = [
    CacheControl,
    ...
]

const server = new ApolloServer({
    typeDefs,
    ...
})

and the problem was gone.

r9dman
  • 11
  • 4
0

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

Jimmy Muga
  • 86
  • 4