graphene-django:查询所有模型字段而不是请求的字段

Posted

技术标签:

【中文标题】graphene-django:查询所有模型字段而不是请求的字段【英文标题】:graphene-django: Query all the models fields and not the requested fields 【发布时间】:2020-04-21 16:28:39 【问题描述】:

我有两个模型:

class Type(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return f'self.name'


class Pet(models.Model):
    name = models.CharField(max_length=255)
    color = models.CharField(max_length=255)
    type = models.ForeignKey(Type, related_name="pets", on_delete=models.CASCADE,
                             null=True, blank=True)

    def __str__(self):
        return f'self.type.name self.name'

架构:

class Type(DjangoObjectType):
    class Meta:
        model = TypeModel

    @classmethod
    def get_node(cls, info, id):
        return TypeModel.objects.get(id=id)


class TypeConnector(graphene.Connection):
    count = graphene.Int()

    class Meta:
        node = Type

    def resolve_count(root, info):
        return len(root.edges)


class Pet(DjangoObjectType):
    class Meta:
        model = PetModel
        interfaces = (relay.Node,)

    @classmethod
    def get_node(cls, info, id):
        return PetModel.objects.get(id=id)


class PetConnector(graphene.Connection):
    count = graphene.Int()

    class Meta:
        node = Pet

    def resolve_count(root, info):
        return len(root.edges)


class Schema(ObjectType):
    pets = graphene.ConnectionField(PetConnector)
    types = graphene.ConnectionField(TypeConnector)

    def resolve_pets(self, info, **kwargs):
        # TODO: Query for requested fields only
        return PetModel.objects.all()

    def resolve_types(self, info, **kwargs):
        # TODO: Query for requested fields only
        return TypeModel.objects.all()

GraphQL 的目标之一是性能。为此,GraphQL 必须仅通过 GraphQL 请求(例如:GraphiQL)向数据库请求请求的字段

如果我请求以下查询:


  pets 
    edges 
      node 
        color
        type 
          name
        
      
    
  

graphene-django 库生成以下 SQL:

2020-01-03 03:16:18.184 UTC [136] LOG:  statement: SELECT "pets_pet"."id", "pets_pet"."name", "pets_pet"."color", "pets_pet"."type_id" FROM "pets_pet"
2020-01-03 03:16:18.189 UTC [136] LOG:  statement: SELECT "pets_type"."id", "pets_type"."name" FROM "pets_type" WHERE "pets_type"."id" = 1 LIMIT 21

它获取模型的所有字段!与 API Rest 问题相同,不符合 GraphQL 指南。

如果我从pets 模型中请求字段color,我希望查询是:

SELECT "pets_pet"."color" FROM "pets_pet"

还有...如何用这种方法解析外键?

【问题讨论】:

【参考方案1】:

您可以尝试使用graphene-django-optimizer。

它的作用是优化您的查询,以便您在一次查询中获取所有相关对象。

在您的情况下,这可能是您的解析器:

import graphene_django_optimizer as gql_optimizer

...

class Schema(ObjectType):
    pets = graphene.ConnectionField(PetConnector)
    types = graphene.ConnectionField(TypeConnector)

    def resolve_pets(self, info, **kwargs):
        return gql_optimizer.query(PetModel.objects.all(), info)

它会自动生成以下查询集:

  Pets.objects
    .select_related('type')
    .only('color', 'type__name')

【讨论】:

以上是关于graphene-django:查询所有模型字段而不是请求的字段的主要内容,如果未能解决你的问题,请参考以下文章

如何仅向graphene-django中的用户个人资料所有者显示特定字段?

如何查询以便显示模型的字段而不是实际模型

GraphQL graphene-django 基本使用文档

Graphene-Django 和模型属性

graphene-django 将 models.BigInteger() 转换为 graphene.Integer()

Graphene-Django:在模式中结合查询对象(只接受第一个参数)