1

我有一个与 djongo ( mongoDB ) 一起运行的 Django GraphQL 应用程序 ( graphene_django )。

当我尝试列出所有 twitter 查询(使用 GraphiQL)时,它返回空数据:

我的查询:

query {
  allTwitterQueries {
    id,
    keyword
  }
}

返回:

{
  "data": {
    "allTwitterQueries": null
  }
}

这是我的 Django 文件:

无标题/schema.py

import untitled.api.schema
import graphene

from graphene_django.debug import DjangoDebug


class Query(
    untitled.api.schema.Query,
    graphene.ObjectType,
):
    debug = graphene.Field(DjangoDebug, name="_debug")


schema = graphene.Schema(query=Query)

无标题/api/models.py

from django.db import models
from django.contrib.auth.models import User


class TwitterQuery(models.Model):
    user_key = models.ForeignKey(User, related_name="created_by", on_delete=models.CASCADE, default=0)
    keyword = models.TextField(default="null")
    active = models.BooleanField(null=True)
    created_at = models.BigIntegerField(default=0)
    updated_at = models.BigIntegerField(default=0)
    count = models.IntegerField(default=0)

    def __str__(self):
        return self.keyword

无标题/api/schema.py

import graphene
from graphene_django.types import DjangoObjectType

from untitled.api.models import TwitterQuery


class TwitterQueryType(DjangoObjectType):
    class Meta:
        model = TwitterQuery


class Query(object):
    twitter_query = graphene.Field(TwitterQueryType, id=graphene.Int(), keyword=graphene.String(), active=graphene.Boolean())
    all_twitter_queries = graphene.List(TwitterQueryType)

    def fetch_twitter_queries(self, context):
        return TwitterQuery.objects.all()

    def fetch_twitter_query(self, context, user_id=None, active=None):
        if user_id is not None:
            return TwitterQuery.objects.get(user_id=user_id)
        if active is not None:
            return TwitterQuery.objects.get(active=active)

        return None

我的 mongoDB 实例中有一项:

{"_id":{"$oid":"5df20401d4e39b1e89223b15"},
"id":{"$numberInt":"1"},
"user_key_id":{"$numberInt":"1"},
"keyword":"greve",
"active":true,
"created_at":{"$numberLong":"1575846000000"},
"updated_at":{"$numberLong":"1575846000000"},
"count":{"$numberInt":"0"}}
4

1 回答 1

5

乍一看,我认为您需要重命名您的解析函数。Graphene 使用一种模式,它在模式中查找resole_x()“x”是您的字段名称的位置 ( https://docs.graphene-python.org/en/latest/types/objecttypes/#resolvers )。或者,您也可以传递resolver=your_functiongraphene.Field() whereyour_function可以是任何函数。

Graphene 还提供了一个默认解析器,如果它是一个对象或字典,它会尝试从父级访问属性。由于您不是这种情况(我们在您的查询的根目录中),因此您会null返回,因为没有任何问题得到解决。

我认为将您的代码调整为此

class Query(object): 
   twitter_query = graphene.Field(TwitterQueryType, id=graphene.Int(), keyword=graphene.String(), active=graphene.Boolean()) 
   all_twitter_queries = graphene.List(TwitterQueryType) 

   def resolve_all_twitter_queries(self, context): 
      return TwitterQuery.objects.all() 

   def resolve_twitter_query(self, context, user_id=None, active=None): 
       if user_id is not None: 
          return TwitterQuery.objects.get(user_id=user_id) 
       if active is not None: 
          return TwitterQuery.objects.get(active=active)
       return None

应该做的伎俩。

您应该再看一下文档。它改善了安静一点,即使它仍然不完美:)。希望对您有所帮助。

于 2019-12-13T06:39:10.300 回答