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