graphene-django-extras icon indicating copy to clipboard operation
graphene-django-extras copied to clipboard

DjangoObjectType incorrectly produces nullable fields

Open iynaix opened this issue 5 years ago • 5 comments

Using the following simple schema and the default User model.

import graphene

from graphene_django import DjangoObjectType
from graphene_django_extras import DjangoObjectType as DjangoObjectTypeGDE

from django.contrib.auth.models import User


class UserType(DjangoObjectType):
    class Meta:
        model = User


class UserTypeGDE(DjangoObjectTypeGDE):
    class Meta:
        model = User


class Query(graphene.ObjectType):
    all_users = graphene.List(UserType)
    all_users_gde = graphene.List(UserTypeGDE)


schema = graphene.Schema(query=Query)

Produces the following graphql types:

UserType UserTypeGDE

In the schema generated by graphene-django-extras, every field (except for id) is marked as nullable. However, all the fields except lastLogin are marked as not null in the database and will always return a value.

iynaix avatar Aug 07 '19 03:08 iynaix

It appears that is_required isn't checking whether or not fields are nullable: https://github.com/eamigo86/graphene-django-extras/blame/master/graphene_django_extras/utils.py#L206

tpict avatar Nov 19 '19 23:11 tpict

Looks like the problem is at https://github.com/eamigo86/graphene-django-extras/blob/abb5a333ef74a3c0aa0ce50ee896ae1c3c3b3c32/graphene_django_extras/converter.py#L199 A string field can only be required if input_flag is 'create'. Similar logic also applies to other type of fields.

tnagorra avatar Nov 02 '20 10:11 tnagorra

is input_flag something users can set?

If not, any workarounds? I am using typescript on the frontend and having everything as optionally null when it's really not is quite painful.

Tom-Greenwood avatar May 05 '21 17:05 Tom-Greenwood

@Tom-Greenwood We didn't find a workaround for this. We were only using this library for a small functionality so we just stopped using this library. We are also using typescript and codegen to generate all the typings and we didn't want to lose that.

tnagorra avatar May 07 '21 09:05 tnagorra

Thanks @tnagorra I am exactly the same situation. Similarly, I found that I could build the same filtering functionality with just plain django and graphene after all.

Tom-Greenwood avatar May 07 '21 09:05 Tom-Greenwood