带有 cacheControl 指令的 Apollo Graphql 导入问题

Apollo Graphql Import Issue with cacheControl directive

我正在使用 "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.

所以在从 link -

获得提示后

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.

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

你在哪里声明那些枚举和指令? 我一直收到这些错误,只是因为我将它们放入了不止一次被引用的 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,
    ...
})

问题消失了。

静态提示给了我同样的错误,所以我在解析器中尝试了动态提示并且它有效。

关于Apollo Docs

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

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