简体   繁体   中英

Received incompatible instance in Graphql query

When i hit insomnia with this request bellow then it shows this response. How can i solve this issue?

Request:

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

Response:

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

My expectation which i have already gotten in errors message but this way:

{
    'updated_at': datetime.date(2020, 4, 8),
    'affected': 137,
    'death': 42,
    'recovered': 104
}

My GraphQL query:

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'))

My model:

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)

Under CoronaQuery class, since you are returning a list of objects (instances), graphene.Field should be changed to graphene.List .

I mean:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM