This is an example document of my mongoDB. I need to get the content.en
array via Apollo/GraphQL. But the nested object is getting a problem for me.
en
is the language tag, so it would be great if this could be used as a variable.
Data in MongoDB
{
"_id" : "9uPjYoYu58WM5Tbtf",
"content" : {
"en" : [
{
"content" : "Third paragraph",
"timestamp" : 1484939404
}
]
},
"main" : "Dn59y87PGhkJXpaiZ"
}
The graphQL result should be:
{
"data": {
"article": [
{
"_id": "9uPjYoYu58WM5Tbtf",
"content": [
{
"content" : "Third paragraph",
"timestamp" : 1484939404
}
]
}
]
}
}
That means, I need to get the ID and the language specific content array.
But this is not, what I'm getting with the following setup:
Type
const ArticleType = new GraphQLObjectType({
name: 'article',
fields: {
_id: { type: GraphQLID },
content: { type: GraphQLString }
}
})
GraphQL Schema
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
article: {
type: new GraphQLList(ArticleType),
description: 'Content of article dataset',
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLID)
}
},
async resolve ({ db }, args) {
return db.collection('articles').find({ main: args.id }).toArray()
}
}
}
})
})
Query
{
article(id: "Dn59y87PGhkJXpaiZ") {
_id,
content
}
}
Result
{
"data": {
"article": [
{
"_id": "9uPjYoYu58WM5Tbtf",
"content": "[object Object]"
}
]
}
}