python - Django GraphQL 返回 null

标签 python django graphql graphene-django

我有一个与 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"}}

最佳答案

乍一看,我认为您需要重命名您的解析函数。 Graphene 使用一种模式来查找 resole_x(),其中“x”是架构中字段的名称 (https://docs.graphene-python.org/en/latest/types/objecttypes/#resolvers)。或者,您也可以将 resolver=your_function 传递给 graphene.Field(),其中 your_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

应该可以解决问题。

你应该再看看文档。尽管它仍然不完美,但它的安静程度有所提高 :)。 希望对您有所帮助。

关于python - Django GraphQL 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59301736/

相关文章:

python - 将对象添加到 ManyToManyField 时,Django 如何填充 'trough' 中间模型中的字段?

graphql - Gatsby graphiQL - 无法为不可为空的字段 ImageSharpFluid.src 返回 null

ruby-on-rails - GraphQL/Rails 422 无法处理的实体 - 将 token 保存到 session

node.js - 如何在 GraphQL 中包含突变请求元数据?

python - 加载模型时如何使用 min max scaler 拟合测试数据?

python - 为什么 Python `Memory Error` 和列表 `append()` 剩余大量 RAM

c++ - 使用 distutils 构建 Python 扩展模块

python - Django ;通过相关对象的计数查询数据库

python - Django Web 应用程序的棘手编码逻辑

django - 如何使用 ForeignKeyRawIdWidget