我已经创建了一个包含两个字段的GraphQLSchema,这两个字段都使用
通过这个,查询...
但我需要一个像这样的结果结构(内容应该在文章对象内):
期望结果
resolve()
从mongoDB获取数据。通过这个,查询...
{
article(id: "Dn59y87PGhkJXpaiZ") {
title
},
articleContent(id: "Dn59y87PGhkJXpaiZ") {
_id,
content(language: "en"),
type
}
}
...导致结果为:
{
"data": {
"article": {
"title": "Sample Article"
},
"articleContent": [
{
"_id": "Kho2N8yip3uWj7Cib",
"content": "group",
"type": "group"
},
{
"_id": "mFopAj4jQQuGAJoAH",
"content": "paragraph",
"type": null
}
]
}
}
但我需要一个像这样的结果结构(内容应该在文章对象内):
期望结果
{
"data": {
"article": {
"title": "Sample Article",
"content": [
{
"_id": "Kho2N8yip3uWj7Cib",
"content": "group",
"type": "group"
},
{
"_id": "mFopAj4jQQuGAJoAH",
"content": "paragraph",
"type": null
}
]
},
}
}
对我而言,问题在于我的模式中存在异步的mongoDB解析。
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
article: {
type: new GraphQLObjectType({
name: 'article',
fields: {
title: {
type: GraphQLString,
resolve (parent) {
return parent.title
}
}
}
}),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) {
return db.collection('content').findOne({ _id: id })
}
},
articleContent: {
type: new GraphQLList(new GraphQLObjectType({
name: 'articleContent',
fields: {
_id: { type: GraphQLID },
type: { type: GraphQLString },
content: {
type: GraphQLString,
args: {
language: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, { language }, context) {
return parent.content[language][0].content
}
}
}
})),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) {
return db.collection('content').find({ main: id }).toArray()
}
}
}
})
})
更新
如果我将内容嵌套在文章中,会出现错误无法读取未定义的属性“collection”
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
article: {
type: new GraphQLObjectType({
name: 'article',
fields: {
title: {
type: GraphQLString,
resolve (parent) {
return parent.title
}
},
articleContent: {
type: new GraphQLList(new GraphQLObjectType({
name: 'articleContent',
fields: {
_id: { type: GraphQLID },
type: { type: GraphQLString },
content: {
type: GraphQLString,
args: {
language: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, { language }, context) {
return parent.content[language][0].content
}
}
}
})),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) { // db is undefined here!!
return db.collection('content').find({ main: id }).toArray()
}
}
}
}),
args: {
id: { type: new GraphQLNonNull(GraphQLID) }
},
async resolve ({ db }, { id }) {
return db.collection('content').findOne({ _id: id })
}
}
}
})
})
String query = { article(id: "Dn59y87PGhkJXpaiZ") { _id, content(language:"en") { content, timestamp } } }
这样的查询,并根据此处所示更新您的模式以将内容嵌入文章中。我在那里还添加了一个完整的工作Java示例。请看一下并尝试解释这与该帖子有何不同。 - s7vrarticleContent
的数组,并且它的数据来自于find({ main: args.id })
。此外,我需要数据集的标题。这些数据来自于另一个文档的find({_id: args.id})
。这就是让我感到困扰的地方。 - user3142695{ "data": { "article": [ { "_id": "9uPjYoYu58WM5Tbtf", "title": "parent", "content": [ { "content": "第三段", "timestamp": 1484939404 } ] }, { "_id": "345869696665", "title": "parent", "content": [ { "content": "第一段", "timestamp": 1484939404 } ] } ] } }
- s7vr