[英]Error when building typedefs TypeError: Cannot read property 'some' of undefined
I am getting the following error when building Typedefs in Apollo Server:在 Apollo Server 中构建 Typedef 时出现以下错误:
return typeDef.definitions.some(definition => definition.kind === language_1.Kind.DIRECTIVE_DEFINITION &&
^
TypeError: Cannot read property 'some' of undefined
I tried to follow some solutions from here https://github.com/apollographql/apollo-server/issues/2961 but still, I am getting the error.我尝试从这里https://github.com/apollographql/apollo-server/issues/2961遵循一些解决方案,但仍然出现错误。
This is how I am creating the schema:这就是我创建架构的方式:
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'
}
}
});
type.js类型.js
const Book = gql`
type Book {
title: String!
author: String!
}
`;
export const types = () => [Book];
export const typeResolvers = {
};
mutation.js变异.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索引.js
export default {
types: () => [types, queryTypes, inputTypes, mutationTypes],
resolvers: Object.assign(queryResolvers, mutationResolvers, typeResolvers),
};
Any suggestions?有什么建议么? What could I be missing?
我可能会错过什么?
I just had the same issue for the past 2 hours.在过去的 2 小时里,我遇到了同样的问题。 I realized the file were i was instantiating my apollo server was being executed before the typedefs was created.
我意识到我正在实例化我的 apollo 服务器的文件在创建 typedef 之前正在执行。
Simple way to test for this is to make a console.log(types, queryTypes, inputTypes, mutationTypes)
right before the execution of const schema = new ApolloServer({...
.对此进行测试的简单方法是在 const schema = new
console.log(types, queryTypes, inputTypes, mutationTypes)
const schema = new ApolloServer({...
.
One of them is undefined.其中之一是未定义的。 Thanks.
谢谢。
After spending some time making changes, I finally got a working solution.花了一些时间进行更改后,我终于得到了一个可行的解决方案。
I had to make sure that typeDefs
was an array of GraphQL Documents and not a type of [Function: types]
.我必须确保
typeDefs
是 GraphQL 文档的数组,而不是[Function: types]
的类型。 To do that, I removed unnecessary function syntax.为此,我删除了不必要的 function 语法。
For example:例如:
I replaced this export const types = () => [Book];
我替换了这个
export const types = () => [Book];
with this export const types = Book;
用这个
export const types = Book;
I replaced this types: () => [types, queryTypes, inputTypes, mutationTypes]
with types: [types, queryTypes, inputTypes, mutationTypes]
我将此
types: () => [types, queryTypes, inputTypes, mutationTypes]
替换为types: [types, queryTypes, inputTypes, mutationTypes]
... and pretty much every where I had () =>
...几乎所有我拥有的地方
() =>
Finally, before instantiating ApolloServer
, instead of pushing tmp.types
to the array of types, I used concat
to use all defined graphql types in the I had defined the current file 'plus' the graphql types imported in every directory最后,在实例化
ApolloServer
之前,我没有将tmp.types
推送到类型数组,而是使用concat
来使用我定义的当前文件中的所有定义的 graphql 类型“加上”在每个目录中导入的 graphql 类型
typeDefs = typeDefs.concat(tmp.types);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.