如何为嵌套对象或同一父对象中的对象列表定义 GraphQLObjectType
How to define GraphQLObjectType for a nested object or list of objects in the same parent object
对于我的 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'
}
}
}
原来的数据是这样的
{
"_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"
.......
}
我是 graphQL 的新手,请帮忙。
我相信您在这里尝试做的是让您的文档字段成为文档列表。为此,您需要创建一个 "document" GraphQLObjectType
以传递到您的文档字段中的 new GraphQLList()
。像这样:
const DocumentType = new GraphQLObjectType({
name: 'DocumentType',
fields: {
doc_name: {
type: GraphQLString
},
doc_url: {
type: GraphQLString
},
status: {
type: GraphQLBoolean
}
});
然后,一旦您创建了具有正确依赖项的同一文件(或不同的文件并必须导入),您就可以将其插入上面发布的代码中,如下所示:
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'
}
}
}
希望对您有所帮助。
对于我的 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'
}
}
}
原来的数据是这样的
{
"_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"
.......
}
我是 graphQL 的新手,请帮忙。
我相信您在这里尝试做的是让您的文档字段成为文档列表。为此,您需要创建一个 "document" GraphQLObjectType
以传递到您的文档字段中的 new GraphQLList()
。像这样:
const DocumentType = new GraphQLObjectType({
name: 'DocumentType',
fields: {
doc_name: {
type: GraphQLString
},
doc_url: {
type: GraphQLString
},
status: {
type: GraphQLBoolean
}
});
然后,一旦您创建了具有正确依赖项的同一文件(或不同的文件并必须导入),您就可以将其插入上面发布的代码中,如下所示:
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'
}
}
}
希望对您有所帮助。