【发布时间】:2020-05-25 00:30:37
【问题描述】:
我有这个代码:
const ProductType = new GraphQLObjectType({
name: 'Product',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
category: {
type: CategoryType,
resolve: async (parent) => {
return await Category.findOne({_id: parent.category});
}
}
}
});
const CategoryType = new GraphQLObjectType({
name: 'Category',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
products: {
type: ProductType,
resolve: async (parent, args) => {
return await Product.find({category: parent._id});
}
}
}
});
const Query = new GraphQLObjectType({
name: 'Query',
fields: {
Categories: {
type: new GraphQLList(CategoryType),
resolve: async () => {
return await Category.find();
}
}
}
});
当我尝试编译时,我得到 ReferenceError: Cannot access 'CategoryType' before initialization。 我知道首先我应该声明并且只能在使用之后声明,但是我在 YouTube 上的一节课中看到了类似的代码,我认为它应该可以工作,但事实并非如此。
【问题讨论】:
标签: mongodb express mongoose graphql express-graphql