Graphene-django: Possibly a bug in graphene

Created on 25 Apr 2018  路  5Comments  路  Source: graphql-python/graphene-django

I spent hours on this and no luck it looks like a bug.
All fields show up in the results of my query but the "type" and there is no difference between the "type" and the "issuer" for example, both are foriegn keys. Graphene randomly removes the type while it is there and when I print it in the resolver i see it has a value.
Any thought?

`
class T(models.Model):
type=models.CharField(default="type 1",null=False,blank=False,max_length=40)
def __str__(self):
return self.type

class Post(models.Model):
slug=models.SlugField(unique=True)
issuer=models.ForeignKey(User,on_delete=models.SET_NULL,blank=False,null=True,related_name="posts")
date_created=models.DateTimeField(default=timezone.now)
last_edited=models.DateTimeField(null=True)
num_interests = models.IntegerField(verbose_name="Number of interests so far",default=0)
status=models.CharField(max_length=30,default="posted")
tags=TaggableManager()
title=models.CharField (max_length=200,blank=False)
description =models.TextField(max_length=settings.MAX_TEXTAREA_LEN,default="",
validators=[MaxLengthValidatorFactory(settings.MAX_TEXTAREA_LEN)],blank=False)
goodUntil = models.DateField(verbose_name=_("Date"),default=datetime.now()+timedelta(days=7))
subjects=models.ManyToManyField(to=Subject,related_name='posts',blank=False)
type=models.ForeignKey(T,on_delete=models.SET_NULL,blank=False,null=True,related_name="type")`

Most helpful comment

it's very unfortunate that your buggy program wasted so much of my time and without any reason or any error, it works erroneously.

All 5 comments

it's very unfortunate that your buggy program wasted so much of my time and without any reason or any error, it works erroneously.

Do you have a DjangoObjectType defined for T in your schema? If no that would be the issue.

For example, having the models

class First(models.Model):
    ...

class Second(models.Model):
    ...

class Third(models.Model):
    first = models.ForeignKey(First, related_name='third', on_delete=models.CASCADE)
    second = models.ForeignKey(Second, related_name='third', on_delete=models.CASCADE)
    ...

and the following type definitions for our schema

class FirstType(graphene.DjangoObjectType):
    class Meta:
        model = First

class ThirdType(graphene.DjangoObjectType):
    class Meta:
        model = Third

When we query based on ThirdType, something like this

query {
    getAllThirds {
        first { id }
        second { id }
    }
}

It will fail because the ThirdType class did not generate a second field for it because we don't have a type defined for the Second model.

So, after we define it like this

class SecondType(DjangoObjectType):
    class Meta:
        model = Second

it should work properly.

If this isn't the issue, could you please tell us how your schema is defined?

Thank you very much,
Yes that was the issue which I figured out after a day, but why? I dont have any query for T why should I Define a djangoobjecttype for that? And where is it recorded I'm not giving any reference to T-Type to my main model.

Mostly because graphene works with Nodes and Types (you have to provide a schema for you whole api) and needs a way to translate a django model into a type. And without explicitly creating a type for that django model, the framework itself doesn't know how to translate an instance into a graphql node and doesn't make any assumptions.

It seems this issue is now solved. Closing

Was this page helpful?
0 / 5 - 0 ratings