Return Django ORM 联合结果在 Django-Graphene 中

Return Django ORM union result in Django-Graphene

我正在尝试查询两个单独的对象并将它们 return 作为一个结果集。我试过使用 Union 和 Interface,但不确定我在那里做错了什么。

我的模特:

class BaseActivity(models.Model):
    class Meta:
        abstract = True

    name = models.CharField(db_column="activity_type_name", unique=True, max_length=250)
    created_at = CreationDateTimeField()
    modified_at = ModificationDateTimeField()
    created_by = models.UUIDField()
    modified_by = models.UUIDField()
    deleted_at = models.DateTimeField(blank=True, null=True)
    deleted_by = models.UUIDField(blank=True, null=True)


class Activity(BaseActivity):
    class Meta:
        db_table = "activity_type"
        ordering = ("sort_order",)

    id = models.UUIDField(
        db_column="activity_type_id",
        primary_key=True,
        default=uuid.uuid4,
        editable=False,
    )
    sort_order = models.IntegerField()

    def __str__(self):
        return self.name


class CustomActivity(BaseActivity):
    class Meta:
        db_table = "organization_custom_activity_type"

    id = models.UUIDField(
        db_column="organization_custom_activity_type",
        primary_key=True,
        default=uuid.uuid4,
        editable=False,
    )
    farm = models.ForeignKey("farm.Farm", db_column="organization_id", on_delete=models.DO_NOTHING, related_name="custom_activity_farm")

我的架构:

class FarmActivities(graphene.ObjectType):
    id = graphene.String()
    name = graphene.String()


class ActivityType(DjangoObjectType):
    class Meta:
        model = Activity
        fields = ("id", "name", "requires_crop", "sort_order")



class CustomActivityType(DjangoObjectType):
    class Meta:
        model = CustomActivity
    fields = ("id", "name", "farm")

以及查询:

class Query(graphene.ObjectType):
    get_farm_activities = graphene.Field(FarmActivities, farm=graphene.String(required=True))

    def resolve_get_farm_activities(self, info, farm):

        farm_activities = Activity.objects.values("id", "name").filter(
            farmtypeactivityrel__farm_type__farm=farm, deleted_at=None
        )

        custom_activities = CustomActivity.objects.values("id", "name").filter(farm=farm, deleted_at=None)

        return list(chain(farm_activities, custom_activities))

有了这个,我确实从查询中得到了一个列表,但是当我调用 getFarmActivities 时它并没有通过解析器。

字面意思是列表 returns:

ExecutionResult(data={'getFarmActivities': {'id': None, 'name': None}}, errors=None)

这为我提供了使它正常工作所需的提示。我必须构建一个 Union 来解析模型而不是模式类型。

class FarmActivities(graphene.Union):
    class Meta:
        types = (ActivityType, CustomActivityType)

    @classmethod
    def resolve_type(cls, instance, info):
        if isinstance(instance, Activity):
            return ActivityType
        if isinstance(instance, CustomActivity):
            return CustomActivityType

        return FarmActivities.resolve_type(instance, info)

然后允许我像这样 运行 查询:

def resolve_get_farm_activities(自我、信息、农场):

farm_activities = Activity.objects.filter(
    farmtypeactivityrel__farm_type__farm=farm
)

custom_activities = CustomActivity.objects.filter(farm=farm)

return list(chain(farm_activities, custom_activities))

和 API 查询:

    query farmParam($farm: String!)
    {
        getFarmActivities(farm: $farm)
        {
            ... on CustomActivityType
            {
                id
                name
            }
            ... on ActivityType
            {
                id
                name
            }
        }
    }