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 Engine cacheControl directive not found in schema.graphql #153

Closed
loicplaire opened this issue Apr 28, 2018 · 14 comments
Closed

Apollo Engine cacheControl directive not found in schema.graphql #153

loicplaire opened this issue Apr 28, 2018 · 14 comments

Comments

@loicplaire
Copy link

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!

@schickling
Copy link
Contributor

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

@martijnwalraven
Copy link

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.

@schickling
Copy link
Contributor

(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?

@RyannGalea
Copy link

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

@loicplaire
Copy link
Author

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?

@Akryum
Copy link

Akryum commented May 9, 2018

Try this:

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

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

@loicplaire
Copy link
Author

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.

@netspencer
Copy link

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

@schickling
Copy link
Contributor

@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! 💡

@Dragons0458
Copy link

Dragons0458 commented May 11, 2018

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) {
	...
}

@techyrajeev
Copy link

techyrajeev commented Mar 13, 2019

@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
}

@BartusZak
Copy link

The same error here

@ardatan
Copy link
Owner

ardatan commented Mar 17, 2020

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.

@ardatan ardatan closed this as completed Mar 17, 2020
@pgarciacamou
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests