Jay*_*hya 2 mongoose node.js graphql graphql-js
对于我的MongoDB对象,我有一个架构,其中有许多嵌套数据。
但是我在GraphQL中完美实现了查询以获取数据,但是由于无法定义类型,因此无法获取嵌套数据。
fields() {
return {
name: {
type: GraphQLString,
description: 'Name of the person'
},
age: {
type: GraphQLString,
description: 'Age'
},
documents: { // what to do here
type: new GraphQLList(),
description: 'All documents for the person'
}
}
}
Run Code Online (Sandbox Code Playgroud)
原始数据是这样的。
{
"_id" : ObjectId("5ae1da5e4f00b5eee4ab84ee"),
"name" : "Indira Gandhi International Airport",
"age" : 54,
"documents" : [
{
"doc_name" : "personal card",
"doc_url" : "http://",
"status" : true
},
{
"doc_name" : "bank card",
"doc_url" : "http://",
"status" : true
}
],
"latitude" : "",
"longitude" : "",
"timezone" : "Asia/kolkata"
.......
}
Run Code Online (Sandbox Code Playgroud)
我是graphQL的新手,请帮忙。
我相信您要在此处执行的操作是将您的“文档”字段作为文档列表。为此,您需要创建一个“文档” GraphQLObjectType以传递到“文档” new GraphQLList()字段中。像这样:
const DocumentType = new GraphQLObjectType({
name: 'DocumentType',
fields: {
doc_name: {
type: GraphQLString
},
doc_url: {
type: GraphQLString
},
status: {
type: GraphQLBoolean
}
});
Run Code Online (Sandbox Code Playgroud)
然后,一旦在具有正确依赖项的同一个文件(或另一个文件,必须导入)中创建了该文件,便可以将其插入上面发布的代码中,如下所示:
fields() {
return {
name: {
type: GraphQLString,
description: 'Name of the person'
},
age: {
type: GraphQLString,
description: 'Age'
},
documents: {
type: new GraphQLList(DocumentType),
description: 'All documents for the person'
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助。
| 归档时间: |
|
| 查看次数: |
451 次 |
| 最近记录: |