Reputation: 2123
I'm wiring up a graphQL backend with mongoose and I am having trouble with some of the conventions of mongoose for a nested query. If I have the following field in the root query:
course: {
type: CourseType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parentValue, { id }) {
return Course.findById(id);
},
},
A Course Type of this:
const CourseType = new GraphQLObjectType({
name: 'CourseType',
fields: () => ({
id: { type: GraphQLID },
sections: {
type: new GraphQLList(SectionType),
resolve(parentValue) {
return Course.findSections(parentValue._id);
}
}
}),
});
And my model looks like this:
const CourseSchema = new Schema({
_id: { type: String },
sections: [{
type: Schema.Types.String,
ref: 'Section'
}],
});
CourseSchema.statics.findSections = function(id) {
return this.findById(id)
.populate('Sections')
.then(course => {
return course.sections
});
}
mongoose.model('Course', CourseSchema, 'Courses');
const SectionSchema = new Schema({
_id: { type: String },
course: {
type: Schema.Types.String,
ref: 'Course',
},
});
mongoose.model('Section', SectionSchema, 'Sections');
I would expect that I could run a query like this:
query {
course(id: "4Zpm8zrZYqdbr2i4t") {
id
sections {
id
}
}
}
And I should get back a given course with all of its sections. The sections come back as an empty array while the course comes back as expected. I can tell that if I look in the findSections method that it has the proper id, but populate
doesn't seem to fetch the sections.
I have a feeling that I'm running into some issue with how mongoose uses its conventions for how to name stuff but I can't for the life of me figure out what is going on. Here is what my mongoDB looks like:
MongoDB:
Courses: { _id: "4Zpm8zrZYqdbr2i4t" }
Sections: { _id: "00000000000000000", courseId: "4Zpm8zrZYqdbr2i4t" }
Upvotes: 1
Views: 945
Reputation: 3500
One thing that does not look correct. You want to populate for the path sections but the string is in capital letter. See the docu.
So it should look like this:
CourseSchema.statics.findSections = function(id) {
return this.findById(id)
.populate('sections') // lower case
.then(course => {
return course.sections
});
}
Upvotes: 1