The issue lays in your resolvers:
Based on the repo you linked your queries look like the following:
const People = require('../database/people');
const Service = require('../database/service');
const queries = {
People: () => People.find({}),
...
Service: () => Service.find({}),
...
};
module.exports = queries;
The People schema looks like this:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const peopleSchema = new Schema({
Xid: { type: String },
firstName: { type: String },
lastName: { type: String },
email: { type: String },
apps: { type: Array },
serviceId: { type: String },
service: { type: Schema.Types.ObjectId, ref: 'service' }
},{ versionKey: false })
module.exports = mongoose.model('people', peopleSchema);
People.find() will return only the service _id though not the whole service object. That is why you get null in the response.
The GraphQL relationship you implemented in People has a Service Type while you're getting back from the db only the service _id.
You have 2 solutions:
A) You want to retrieve the Service object as well when you query for People. In this case you need to use the mongoose populate function:
People: () => People.find({}).populate('service'),
The above will provide People with the referenced Service object (not just the _id)
Because you're using id instead of _id in your schema the above is not enough and you need to use the following instead where you also create an id field to return for each service
People: async () => {
const people = await People.find({}).populate('service').exec()
return people.map(person => ({
...person._doc,
id: person._doc._id,
service: {
...person._doc.service._doc,
id: person._doc.service._doc._id,
},
}))
}, return people
}
The above is quite convulted. I'd strongly suggest going with solution (B)
Docs about populate(): https://mongoosejs.com/docs/populate.html
B) User a type resolver
// Type.js
const Service = require('../database/service');
const types = {
People: {
// you're basically saying: In People get service field and return...
service: ({ service }) => Service.findById(service), // service in the deconstructed params is just an id coming from the db. This param comes from the `parent` that is People
},
Service: {
id: ({_id}) => _id, // because you're using id in your schema
},
};
module.exports = queries;
A note on the implementation of this option: