graphene-django-extras
graphene-django-extras copied to clipboard
DjangoInputObjectType doesn't allow overwriting model fields
Problem
I'm using custom logic for input validation of a model field (like, validate it as a string and then convert it into UUID v.5 before save). So, I have a model and schema defined below:
# models.py
class TestModel(models.Model):
test_field = models.UUIDField()
# schema.py
class TestModelInput(DjangoInputObjectType):
test_field = graphene.String(required=True)
class Meta:
model = TestModel
class CreateTestModel(graphene.Mutation):
class Input:
data = TestModelInput()
@staticmethod
def mutate(root, info, data):
# here I convert and save my model
What I expect from printing graphql schema:
input TestModelInput {
test_field: String!
...
What I get:
input TestModelInput {
test_field: UUID!
...
Solution
I found out that in the graphene_django_extras/types.py:182
this line
super(InputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
means that the method InputObjectType.__init_subclass_with_meta__
itself is not being called and this class attributes are not being converted into fields. The line should be changed into
super(DjangoInputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
to run correctly.
P.S. Thanks for the great package! It saved me lots of work =)
Hi @ollamh, could you tell me in enviroment where you've tried this fix, I'm trying Windows and it works fine, but in Debian it gives me problems:
and after revert your solution all work fine again, any idea why?
Hi @eamigo86 !
Sorry for a delayed response. It looks like options
dictionary already contains _meta
key. Could you provide the call parameters? I'm using Ubuntu 16.04, python 2.7.12 and python 3.5 and it works as expected.