python - 在 Graphql 查询中收到不兼容的实例

标签 python django postgresql graphql

当我用下面的这个请求失眠时,它会显示这个响应。我该如何解决这个问题?

请求:

query{
    datewiseCoronaCasesList{
        updatedAt,
        affected,
        death,
        recovered
    }
}

回复:
{
    "errors": [
        {
            "message": "Received incompatible instance \"{'updated_at': datetime.date(2020, 4, 8), 'affected': 137, 'death': 42, 'recovered': 104}\"."
        }
    ],
    "data": {
        "datewiseCoronaCasesList": [
            null
        ]
    }
}

我已经在错误消息中得到了我的期望,但是这样:
{
    'updated_at': datetime.date(2020, 4, 8),
    'affected': 137,
    'death': 42,
    'recovered': 104
}

我的 GraphQL 查询:
class CoronaQuery(graphene.ObjectType):
    datewise_corona_cases_list = graphene.Field(CoronaCaseType)

    def resolve_datewise_corona_cases_list(self, info, **kwargs):
        return CoronaCase.objects.values('updated_at').annotate(
affected=Sum('affected'),death=Sum('death'), recovered=Sum('recovered'))

我的型号:
class CoronaCase(models.Model):
    affected = models.IntegerField(default=0)
    death = models.IntegerField(default=0)
    recovered = models.IntegerField(default=0)
    district = models.CharField(max_length=265, null=False, blank=False)
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return "Affected from: {}".format(self.district)

最佳答案

CoronaQuery类,因为您要返回对象(实例)列表,graphene.Field应改为 graphene.List .
我的意思是:

class CoronaQuery(graphene.ObjectType):
    datewise_corona_cases_list = graphene.List(CoronaCaseType) 

关于python - 在 Graphql 查询中收到不兼容的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124950/

相关文章:

python - Matplotlib 到 Django 模板

python - 使用键中的冒号解析 JSON

python - 在生产中将复合主键添加到辅助关联表

postgresql - 如何找到 postgres NOTIFY/LISTEN 消息队列的当前长度?

python - 在python中使用函数之前应该定义函数吗?

python - 在 pandas 上添加新行?

python - 使用 .pfx 证书连接到 IIS 服务器

python - R runif 与 Python stats.uniform.ppf(不同的结果)

python - Django 帮助 : Resolving NOT NULL constraint failed error with CreateView

sql - Postgres 如何避免在 select 语句中重复 if then