Graphene-django: Support for GIS field types

Created on 3 Feb 2018  Â·  6Comments  Â·  Source: graphql-python/graphene-django

Right now, using a DjangoObjectType on a model that contains GIS fields (like django.contrib.gis.db.models.fields.PointField) will trigger an exception: Exception: Don't know how to convert the Django field myapp.MyModel.point (<class 'django.contrib.gis.db.models.fields.PointField'>).

It would be great to add support for this. Otherwise, is there any docs on how to create a custom Field with custom serialization/deserialization like in django-rest-framework?

Most helpful comment

Hi @sanfilippopablo,

the PR convert Geometry fields to well-known text (WKT):

{
    "id": "1",
    "location": "SRID=4326;POINT (0 1)"
}

if you want to convert to GeoJSON:

import json

from django.contrib.gis.db import models

import graphene
from graphene_django.converter import convert_django_field


class GeoJSON(graphene.Scalar):

    @classmethod
    def serialize(cls, value):
        return json.loads(value.geojson)


@convert_django_field.register(models.GeometryField)
def convert_field_to_geojson(field, registry=None):
    return graphene.Field(
        GeoJSON,
        description=field.help_text,
        required=not field.null)

Response:

{
    "id": "1",
    "location": {
        "type": "Point",
        "coordinates": [0.0, 1.0]
    }
}

Recently I have developed a library for Geographic types, inputs, filters..
https://github.com/flavors/django-graphql-geojson

All 6 comments

Hi @sanfilippopablo, it is quite easy to create a custom converter for a field, there are plenty of examples here: https://github.com/graphql-python/graphene-django/blob/master/graphene_django/converter.py#L85

Do you have time to do a PR with some tests? I

@mongkok Nice. That will return the result of calling repr on the field, right? Now, if I want to go a step further an customize how it will be converted to a string, (I want to use the property .json of the GeometryField which returns a json string and convert it to a JSONString graphene type), how would I go about it?

Hi @sanfilippopablo,

the PR convert Geometry fields to well-known text (WKT):

{
    "id": "1",
    "location": "SRID=4326;POINT (0 1)"
}

if you want to convert to GeoJSON:

import json

from django.contrib.gis.db import models

import graphene
from graphene_django.converter import convert_django_field


class GeoJSON(graphene.Scalar):

    @classmethod
    def serialize(cls, value):
        return json.loads(value.geojson)


@convert_django_field.register(models.GeometryField)
def convert_field_to_geojson(field, registry=None):
    return graphene.Field(
        GeoJSON,
        description=field.help_text,
        required=not field.null)

Response:

{
    "id": "1",
    "location": {
        "type": "Point",
        "coordinates": [0.0, 1.0]
    }
}

Recently I have developed a library for Geographic types, inputs, filters..
https://github.com/flavors/django-graphql-geojson

Awesome. Thank you! I will also check out your lib.

On Feb 4, 2018 19:13, "Dani" notifications@github.com wrote:

Hi @sanfilippopablo https://github.com/sanfilippopablo,

the PR convert Geometry fields to well-known text (WKT):

{
"id": "1",
"location": "SRID=4326;POINT (0 1)"
}

if you want to convert to GeoJSON:

import json
from django.contrib.gis.db import models
import graphenefrom graphene_django.converter import convert_django_field

class GeoJSON(graphene.Scalar):

@classmethod
def serialize(cls, value):
    return json.loads(value.geojson)

@convert_django_field.register(models.GeometryField)def
convert_field_to_geojson(field, registry=None):
return graphene.Field(
GeoJSON,
description=field.help_text,
required=not field.null)

Response:

{
"id": "1",
"location": {
"type": "Point",
"coordinates": [0.0, 1.0]
}
}

Recently I develop a library for Geographic types, inputs, filters..
https://github.com/flavors/django-graphql-geojson

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/graphql-python/graphene-django/issues/390#issuecomment-362944570,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABfVlGIAR6gH2qx_snX6Va0IgDiqLoyyks5tRiuVgaJpZM4R4NEw
.

I think we've reached some resolution here - closing down.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BrianChapman picture BrianChapman  Â·  3Comments

dan-klasson picture dan-klasson  Â·  4Comments

ZuluPro picture ZuluPro  Â·  3Comments

Eraldo picture Eraldo  Â·  3Comments

khankuan picture khankuan  Â·  4Comments