Как получить массив объектов, который является свойством документа в MongoDB
Как получить все объекты из массива посредством Graphql? Какая схема Mongoose должна быть и какой GraphQLObjectType создавать?
На данный момент у меня следующие файлы:
Схемы Mongoose
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const services = new Schema({
imagePath: String,
text: String
})
module.exports.services = mongoose.model('Services', services);
const homePage = new Schema({
services:[services]
});
module.exports.homePage = mongoose.model('HomePage', homePage);
Два GraphQLObjectType
const ServicesType = new GraphQLObjectType({
name: 'services',
fields: () => ({
imagePath: { type: GraphQLString },
text: {type: GraphQLString }
})
});
const HomePageType = new GraphQLObjectType({
name: 'HomePage',
fields: () => ({
id: { type: GraphQLID },
services: {
type: new GraphQLList(ServicesType)
}
})
});
И сам запрос
const HomePage = require('../modules/homePage').homePage;
const Query = new GraphQLObjectType({
name: 'Query',
fields: {
getHomePage: {
type: new GraphQLList(HomePageType),
resolve(parent, args){
return HomePage.find({});
}
}
}
});
В итоге я получаю:
{
"data": {
"getHomePage": []
}
}
Также в документе будут и другие поля с массивами объектов. Заранее спасибо!
