1

在 Apollo Server 中构建 Typedef 时出现以下错误:

return typeDef.definitions.some(definition => definition.kind === language_1.Kind.DIRECTIVE_DEFINITION &&
                                   ^
TypeError: Cannot read property 'some' of undefined

我尝试从这里https://github.com/apollographql/apollo-server/issues/2961遵循一些解决方案,但仍然出现错误。

这就是我创建架构的方式:

fs.readdirSync(__dirname)
 .filter(dir => { console.log('dir', dir); return dir.indexOf('.') < 0 })
 .forEach((dir) => {
    const tmp = require(path.join(__dirname, dir)).default;
    resolvers = merge(resolvers, tmp.resolvers);
    typeDefs.push(tmp.types);
 });

const schema = new ApolloServer({
  typeDefs,
  resolvers, 
  playground: {
    endpoint: '/graphql',
    settings: {
      'editor.theme': 'light'
    }
  }
});

类型.js

const Book = gql`
  type Book {
    title: String!
    author: String!
  }
`;

export const types = () => [Book];

export const typeResolvers = {

};

突变.js

const Mutation = gql`
  extend type Mutation {
    addBook(book: BookInput): Book
  }
`;

export const mutationTypes = () => [Mutation];

export const mutationResolvers = {
  Mutation: {
    addBook: async (_, args, ctx) => {
      return []
    }
  }
};

index.js

export default {
  types: () => [types, queryTypes, inputTypes, mutationTypes],
  resolvers: Object.assign(queryResolvers, mutationResolvers, typeResolvers),
};

有什么建议么?我会错过什么?

4

2 回答 2

3

在过去的 2 个小时里,我遇到了同样的问题。我意识到该文件是我在创建 typedef 之前正在实例化我的 apollo 服务器。

对此进行测试的简单方法是console.log(types, queryTypes, inputTypes, mutationTypes)在执行const schema = new ApolloServer({ ....

其中之一是未定义的。谢谢。

于 2020-10-14T21:33:59.493 回答
0

在花了一些时间进行更改后,我终于得到了一个可行的解决方案。

我必须确保这typeDefs是一个 GraphQL 文档数组,而不是[Function: types]. 为此,我删除了不必要的函数语法。

例如:

我用这个替换export const types = () => [Book];了这个export const types = Book;

我将其替换types: () => [types, queryTypes, inputTypes, mutationTypes]types: [types, queryTypes, inputTypes, mutationTypes]

......几乎所有我拥有的地方() =>

最后,在实例化之前,我ApolloServer没有推tmp.types送到类型数组,而是concat使用了所有定义的 graphql 类型,我已经定义了当前文件“加上”每个目录中导入的 graphql 类型

typeDefs = typeDefs.concat(tmp.types);

于 2020-10-09T22:59:35.740 回答