【问题标题】:Apollo Graphql Import Issue with cacheControl directive带有 cacheControl 指令的 Apollo Graphql 导入问题
【发布时间】:2019-07-31 07:15:07
【问题描述】:

我正在使用"graphql-import": "^0.7.1"

我尝试将 @cacheControl 指令添加到我的 graphql 架构中

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

然后它给出了这个错误 -

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

所以在从链接中获取提示之后 -

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

我在下面添加了代码

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

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

但在那之后我开始收到这个错误 -

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.

我不知道如何解决这个问题。

【问题讨论】:

  • 你能不能尝试把cacheDirective放在一个directives.graphql上然后导入它(#import cacheControl from 'directives.graphql')看看它是否有效?
  • @jgoday 我也试过了,但还是不行

标签: graphql graphql-js apollo-server express-graphql


【解决方案1】:

你在哪里声明这些枚举和指令? 我不断收到这些错误,只是因为我将它们放入了不止一次引用的 typedef 文件中。 然后我刚刚将此代码移动到我的主架构文件中

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

问题就解决了。

【讨论】:

    【解决方案2】:

    静态提示给了我同样的错误,所以我尝试在解析器中使用动态提示,它可以工作。

    关于Apollo Docs

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

    【讨论】:

      【解决方案3】:

      也遇到了这个问题,找不到指令是由于模式拼接造成的。我通过将指令和枚举定义放在架构本身中来使用与您相同的工作。当我遇到该错误时,我必须至少升级到 2.6.6,因为他们在那里为您收到的欺骗错误添加了修复 ref:https://github.com/apollographql/apollo-server/pull/2762

      【讨论】:

        猜你喜欢
        • 2021-11-01
        • 2021-01-08
        • 2021-01-10
        • 2020-08-17
        • 2019-11-16
        • 2018-04-12
        • 2019-04-13
        • 2019-05-13
        • 2019-04-22
        相关资源
        最近更新 更多