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

时间:2019-03-09 11:49:23

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

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

我不知道如何解决此问题。

3 个答案:

答案 0 :(得分:1)

静态提示也给了我相同的错误,因此我尝试在解析器中使用动态提示,并且可以正常工作。

关于Apollo Docs

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

cache control

答案 1 :(得分:0)

您在哪里声明这些枚举和指令? 我一直收到这些错误,只是因为我将它们放入一个被多次引用的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 :(得分:0)

也解决了这个问题,并且由于模式缝合而导致找不到指令。我通过在架构本身中放置指令和枚举定义来进行相同的工作。当我遇到该错误时,我必须至少升级到2.6.6,因为那是他们在其中添加了针对dupe错误的修复程序,您正在获得引用:https://github.com/apollographql/apollo-server/pull/2762

相关问题