Graphene-django: Don't know how to convert the Django class django_mysql.models.fields.json.JSONField

Created on 27 Oct 2017  路  6Comments  路  Source: graphql-python/graphene-django

I've just added a JSONField on my models and graphene said Don't know how to convert the Django class django_mysql.models.fields.json.JSONField.
I think it wasn't supported yet but are there any way to work around?

thanks

Most helpful comment

Adding some more color to this in case there are others who arrive from google:

Suppose you have a django model like so:

from django.contrib.postgres.fields import JSONField

class Rapper(models.Model):
    name = models.CharField()
    bars = JSONField()

Here's what the Query object and connecting type would look like:

from graphene.types import generic
from graphene_django.types import DjangoObjectType

from .models import Rapper

class RapperType(DjangoObjectType):
    bars = generic.GenericScalar()

    class Meta:
        model = Rapper

And here's what a sample GraphQL query and response end up looking like:

{
  rapper {
    name
    bars
  }
}
{
  "data": {
    "rapper": {
      "name": "50 Cent",
      "bars": [
        "Joy wouldn't feel so good if it wasn't for pain",
        "It's your birthday"
      ]
    }
  }
}

All 6 comments

I end up with this snippet, thanks

import graphene
from graphene_django.converter import convert_django_field
from django_mysql.models import JSONField

@convert_django_field.register(JSONField)
def convert_json_field_to_string(field, registry=None):
    return graphene.String()

That's definitely the way to go for a quick way to add your own fields that graphene-django doesn't support.

We would also welcome an addition like that to converter.py that does something similar to the postgres JSONField.

I will also note that we personally overrode the graphene type to be a graphene.GenericScalar, and then the JSON flows through and you do not have to re-parse it on the client side.

I you found this issue looking for postgres-JSONField like me, maybe this patch will help you:

return graphene.GenericScalar() did not work for me, I had to import like from graphene.types.generic import GenericScalar and then return GenericScalar() instead. It is a great workaround, the JSON is passed through as expected, thanks!

Adding some more color to this in case there are others who arrive from google:

Suppose you have a django model like so:

from django.contrib.postgres.fields import JSONField

class Rapper(models.Model):
    name = models.CharField()
    bars = JSONField()

Here's what the Query object and connecting type would look like:

from graphene.types import generic
from graphene_django.types import DjangoObjectType

from .models import Rapper

class RapperType(DjangoObjectType):
    bars = generic.GenericScalar()

    class Meta:
        model = Rapper

And here's what a sample GraphQL query and response end up looking like:

{
  rapper {
    name
    bars
  }
}
{
  "data": {
    "rapper": {
      "name": "50 Cent",
      "bars": [
        "Joy wouldn't feel so good if it wasn't for pain",
        "It's your birthday"
      ]
    }
  }
}

@quytang and @spockNinja - Would it be helpful to add this to the docs? I can make a PR.

Also, an issue I've come across is that I'm having to register separately for each and every schema.py file where a query utilizes the custom type. I tried doing this at the the root schema.py, but it still errors out unless this is done at every single schema.py separately.

Was this page helpful?
0 / 5 - 0 ratings