简体   繁体   English

Graphene-django返回空节点,而不是仅返回节点字段

[英]Graphene-django returns null node instead of just node field

I am facing a strange problem with graphene and Django. 我面临着石墨烯和Django的奇怪问题。 The documentation seems to lack a solution and I couldn't figure out what I'm doing wrong. 该文档似乎缺乏解决方案,我无法弄清楚自己在做什么错。

I have the following models: 我有以下型号:

Class Sentence(models.Model):
    ref = models.CharField(max_length=30, primary_key=True)
    body = models.TextField(default=None)

Class Summary(models.Model):
    sentence = models.ForeignKey(Sentence, on_delete=models.CASCADE)
    text = models.TextField(default=None)

(Each sentence can have multiple summaries) (每个句子可以有多个摘要)

And the following schema: 以及以下架构:

Class SentenceType(DjangoObjectType):
    class Meta:
        model = models.Sentence
        filter_fields = {"ref": ["exact"]}
        interfaces = (graphene.Node, )

Class SummaryType(DjangoObjectType):
    class Meta:
        model = models.Summary
        filter_field = {"text": ["icontains"]}
        interfaces = (graphene.Node, )

Class Query(graphene.ObjectType):
    all_sentences = DjangoFilterConnectionField(SentenceType)
    sentence = graphene.Field(SentenceType, ref=graphene.string(), body=graphene.string())
    all_summary = all_provvedimenti = DjangoFilterConnectionField(SummaryType)
    summary = graphene.field(SummaryType, id=graphene.Int(), text=graphene.string())

def resolve_all_summaries(self, context, **kwargs):
    return models.Summary.objects.all()

It can occur that there is one or more summaries in my database with no corresponding sentence. 我的数据库中可能有一个或多个摘要,但没有相应的句子。

Now, when I query 现在,当我查询

{
  allSummaries{
    edges{
      node{
        text
        sentence{
          ref
        }
      }
    }
  }
}

If the the sentence exists for the summary, no problem at all. 如果该句子用于总结,则完全没有问题。 But if there is no corresponding sentence I get: 但是如果没有相应的句子,我会得到:

"errors": [
    {
      "message": "Sentence matching query does not exist.",
      "locations": [
        {
          "line": 6,
          "column": 9
        }
      ]
    }
],
...,
...,
"data":[
    ...,
    {
      "node": null
    },
    {
      "node":{
        "text": "blah blah blah sentence summary"
        "sentence": {
          "ref": "sentence_reference"
        }
      }
    }
    {
      "node": null
    },
    ...,
]

Naturally, the output I would expect is that whenever a corresponding sentence does not exist for a summary node it would still give me back the text of the summary and "sentence": null or []. 自然,我期望的输出是,只要摘要节点不存在相应的句子,它仍会给我返回摘要和“句子”的文本:null或[]。

I had no luck in the documentation or google. 我在文档或Google方面没有运气。 Seems I am the only one having this issue. 似乎我是唯一遇到此问题的人。 I can't understand whether I am making some mistake in Django, graphene or it is just a bug. 我不明白我是在Django,graphene中犯了错误还是只是一个错误。

Any suggestions? 有什么建议么?

change the all_summary variable in this line 更改此行中的all_summary变量

all_summary = all_provvedimenti = DjangoFilterConnectionField(SummaryType)

to allSummaries 对所有allSummaries

and then change your function name in 然后在中更改函数名称

def resolve_all_summaries(self, context, **kwargs):

to resolve_allSummaries as well 以及resolve_allSummaries

then you can query with: 然后您可以查询:

 {
  allSummaries{
    edges{
      node{
        text
        sentence{
          ref
        }
      }
    }
  }
}

all three of those identifiers refer to the same thing and therefor need to match 这些标识符中的所有三个都指同一事物,因此需要匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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