ardatan/graphql-import

Apollo Engine cacheControl directive not found in schema.graphql

Closed this issue ยท 14 comments

FYI, I initially raised this issue on graphql-yoga.

I setup a repository with an example of the issue I am running into: https://github.com/loicplaire/graphql-yoga-engine-issue

When I use the @cacheControl directive in my schema.graphql file, I get the following error:

/git/graphql-yoga-engine-issue/node_modules/graphql-import/dist/definition.js:113
                throw new Error("Directive " + directiveName + ": Couldn't find type " + directiveName + " in any of the schemas.");
                ^

Error: Directive cacheControl: Couldn't find type cacheControl in any of the schemas.
    at collectDirective (/git/graphql-yoga-engine-issue/node_modules/graphql-import/dist/definition.js:113:23)
    at Array.forEach (<anonymous>)
    at collectNewTypeDefinitions (/git/graphql-yoga-engine-issue/node_modules/graphql-import/dist/definition.js:47:34)
    at Object.completeDefinitionPool (/git/graphql-yoga-engine-issue/node_modules/graphql-import/dist/definition.js:23:41)
    at Object.importSchema (/git/graphql-yoga-engine-issue/node_modules/graphql-import/dist/index.js:85:41)
    at mergeTypeDefs (/git/graphql-yoga-engine-issue/node_modules/graphql-yoga/dist/src/index.js:320:37)
    at new GraphQLServer (/git/graphql-yoga-engine-issue/node_modules/graphql-yoga/dist/src/index.js:85:34)
    at Object.<anonymous> (/git/graphql-yoga-engine-issue/index.js:16:16)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)

My assumption is that graphql-import only understands built-in directives or directives directly imported in the schema.graphql file.

In that case how would I import the cacheControl directive from apollo-cache-control in my .graphql files when this doesn't seem to be exported by this package? Would I have to redeclare it?

Thanks!

That's a great point @loicplaire. I'm looping into @martijnwalraven who might have an idea for this.

This seems like an issue specific to graphql-import. I don't completely understand what this code is trying to do, but it seems wrong to me. Schema directives are not defined as part of the schema, so you won't find a DirectiveDefinitionNode for cacheControl.

(So far) graphql-import is meant to operate purely on SDL documents. Maybe a pre-compile step could be a possible solution to generate a directive definition for cacheControl (or other directives) so graphql-import can pick it up?

I'm having the same issue, no matter which way I attach my directives I continually get the same error. Super frustrating.

Correct me if I am wrong but if cacheControl was declared in a SDL document in apollo-cache-control then I could import it in my main schema file, like this example, couldn't I?

Try this:

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

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

Thanks a lot @Akryum, re-declaring the directive in my schema would work ๐Ÿ‘.
I just wish there was a way to avoid that by importing it somehow.

Thanks a lot @Akryum, re-declaring the directive in my schema would work ๐Ÿ‘. ...

@loicplaire I agree. Something feels wrong about this approach, even tho it works. Perhaps it is a setback due to the way graphql-import was designed

@netspencer during the time graphql-import was designed, SDL directives weren't yet part of the GraphQL spec. While I think it's a good workaround to use the snippet @Akryum has provided, I do agree that there should be a better way to make this work.

Would love to hear ideas about this! ๐Ÿ’ก

Hello! apollo engine returns the following: 'time = "2018-05-11T14: 11: 12-07: 00" level = debug msg = "QueryResponseCache not caching: Response overall cache maxAge is 0." cachePolicy = PUBLIC signature = "..." 'and I'm using the cacheControl directive in a type, do you know why it happens?

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

enum CacheControlScope {
    PUBLIC
    PRIVATE
}

 type FormOutline @cacheControl(maxAge: 60) {
	...
}

@Akryum I am 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.

When I added this code

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

enum CacheControlScope {
    PUBLIC
    PRIVATE
}

The same error here

Since 1.0.0 cacheControl is now considered a builtin directive so you wouldn't have that problem in 1.0.0! Feel free to open a new issue if the problem persists.

Just for future reference for people landing here when looking for "cacheControl directive not found".

This has been added to the Apollo docs:

To use the @CacheControl directive, you must add the following definitions to your server's schema

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

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

https://www.apollographql.com/docs/apollo-server/performance/caching/#in-your-schema-static