如何 return 将 graphql 查询中的 QuerySet 列表作为单独的列表元素?
How to return List of QuerySets in graphql query as separate list elements?
我面临的问题是我有 custom_filter
的 MyModel
其中 return <QuerySet>
的列表喜欢
[<QuerySet [<MyModel: xyz>]>, <QuerySet [<MyModel: xyz>, <MyModel: xyz>,<MyModel: xyz>]>]
对象类型
class MyModelNode(DjangoObjectType):
class Meta:
model=MyModel
filter_fields=['id]
interfaces = (graphene.relay.Node,)
查询
class Query(graphene.ObjectType):
my_model_items = graphene.List(MyModelNode)
def resolve_my_model_items(self, info, **kwargs):
my_model_filtered_items = MyModel.objects.custom_filter(kwargs)
# my_model_filtered_items holds the list of querysets
return my_model_filtered_items
如何处理查询集列表。
查询的 graphql 响应应该给出一个列表,其中包含查询集作为元素。
[
{
//These are from first <QuerySet>
"myModelItems":[
{
"fieldsIaskedFor":"response"
}
]
},
{
//These are from second <QuerySet>
"myModelItems":[
{
"fieldsIaskedFor":"resp"
},
{
"fieldsIaskedFor":"resp"
},
{
"fieldsIaskedFor":"resp"
},
]
},
]
如何在单独的列表元素中获取不同查询集的结果?
<QuerySet>
个数不固定
我需要做什么才能达到这个目标?
这可以通过创建两种对象类型并简单地将一种嵌套在另一种中来实现。
第一个对象类型将是 MyModel
的 DjangoObjectType
class DjangoMyModelNode(DjangoObjectType):
class Meta:
model = MyModel
filter_fields = ['id']
interfaces = (graphene.relay.Node,)
第二种对象类型将是自定义的 ObjectType
class MyModelNode(graphene.ObjectType):
my_model_items = graphene.List(DjangoMyModelNode)
def resolve_my_model_items(self, info, **kwargs):
return self
查询将保持不变
class Query(graphene.ObjectType):
my_model_items = graphene.List(MyModelNode)
def resolve_my_model_items(self, info, **kwargs):
my_model_filtered_items = MyModel.objects.custom_filter(kwargs)
# my_model_filtered_items holds the list of querysets
return my_model_filtered_items
这将return事情根据期望的结果
我面临的问题是我有 custom_filter
的 MyModel
其中 return <QuerySet>
的列表喜欢
[<QuerySet [<MyModel: xyz>]>, <QuerySet [<MyModel: xyz>, <MyModel: xyz>,<MyModel: xyz>]>]
对象类型
class MyModelNode(DjangoObjectType):
class Meta:
model=MyModel
filter_fields=['id]
interfaces = (graphene.relay.Node,)
查询
class Query(graphene.ObjectType):
my_model_items = graphene.List(MyModelNode)
def resolve_my_model_items(self, info, **kwargs):
my_model_filtered_items = MyModel.objects.custom_filter(kwargs)
# my_model_filtered_items holds the list of querysets
return my_model_filtered_items
如何处理查询集列表。 查询的 graphql 响应应该给出一个列表,其中包含查询集作为元素。
[
{
//These are from first <QuerySet>
"myModelItems":[
{
"fieldsIaskedFor":"response"
}
]
},
{
//These are from second <QuerySet>
"myModelItems":[
{
"fieldsIaskedFor":"resp"
},
{
"fieldsIaskedFor":"resp"
},
{
"fieldsIaskedFor":"resp"
},
]
},
]
如何在单独的列表元素中获取不同查询集的结果?
<QuerySet>
个数不固定
我需要做什么才能达到这个目标?
这可以通过创建两种对象类型并简单地将一种嵌套在另一种中来实现。
第一个对象类型将是 MyModel
DjangoObjectType
class DjangoMyModelNode(DjangoObjectType):
class Meta:
model = MyModel
filter_fields = ['id']
interfaces = (graphene.relay.Node,)
第二种对象类型将是自定义的 ObjectType
class MyModelNode(graphene.ObjectType):
my_model_items = graphene.List(DjangoMyModelNode)
def resolve_my_model_items(self, info, **kwargs):
return self
查询将保持不变
class Query(graphene.ObjectType):
my_model_items = graphene.List(MyModelNode)
def resolve_my_model_items(self, info, **kwargs):
my_model_filtered_items = MyModel.objects.custom_filter(kwargs)
# my_model_filtered_items holds the list of querysets
return my_model_filtered_items
这将return事情根据期望的结果