graphql-python / graphene

GraphQL framework for Python

Home Page:http://graphene-python.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

InputObjectType causing encoding issues with JSONField

adilhussain540 opened this issue · comments

Note: for support questions, please use stackoverflow. This repository's issues are reserved for feature requests and bug reports.

  • What is the current behavior?
    I have an InputObjectType with multiple fields and receive it in my mutation as a param. But it throws Object of type proxy is not JSON serializable on passing it as a JSONField parameter for object creation.

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via
    a github repo, https://repl.it or similar.
    I have the following Django model with a JSONField inside:

from django_jsonfield_backport import models as mysql


class RaceReport(TimeStampedModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='race_reports')
    parameters = mysql.JSONField(blank=True, default=dict)

Then, there's a custom InputObjectType which looks like this:

class ParametersInput(graphene.InputObjectType):
    start_point = BigInteger(required=True)
    end_point = BigInteger(required=True)
    data_type = DataType()

I am using the above input type inside the following Mutation:

class CreateRaceReport(graphene.Mutation):
    class Arguments:
        parameters = ParametersInput(required=True)

    def mutate(_root, info, parameters):  # noqa: N805
        race_report = RaceReport.objects.create(user=info.context.user, parameters=parameters) # origin of the error
        return CreateRaceReport(race_report=race_report)
  • What is the expected behavior?
    The InputObjectType should be resolved after we access its attributes or useparameters.__dict__ but it stays lazy and causes the JSON encoding errors.

  • What is the motivation / use case for changing the behavior?
    Better compatibility with JSONField.

  • Please tell us about your environment:

    • Version: 2.1.8
    • Platform: MacOS 13.0.1
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow)