简体   繁体   中英

Graphene-django returns null node instead of just node field

I am facing a strange problem with graphene and 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 [].

I had no luck in the documentation or 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.

Any suggestions?

change the all_summary variable in this line

all_summary = all_provvedimenti = DjangoFilterConnectionField(SummaryType)

to allSummaries

and then change your function name in

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

to resolve_allSummaries as well

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

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