Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What if a model has more geodjango fields? #3

Open
zmasek opened this issue Jun 19, 2018 · 8 comments
Open

What if a model has more geodjango fields? #3

zmasek opened this issue Jun 19, 2018 · 8 comments
Labels
enhancement New feature or request

Comments

@zmasek
Copy link

zmasek commented Jun 19, 2018

Currently I see only support for one field per model. Is it possible to have more?

Thanks

@mongkok
Copy link
Member

mongkok commented Jun 21, 2018

Hi @zmasek,
right now only one geometry field is allowed within geojson_field.

We could include the GeometryCollection type for a geometry field list,
http://geojson.org/geojson-spec.html#geometry-collection

class PlaceType(graphql_geojson.GeoJSONType):

    class Meta:
        model = models.Place
        geojson_field = ('field_a', 'field_b')
{
   "type": "GeometryCollection",
   "geometries": [
      {
         "type": "Point",
         "coordinates": [0, 1]
      },
      {
         "type": "LineString",
         "coordinates": [ [0, 1], [1, 0] ]
      }
   ]
}

what do you think?

@mongkok mongkok added the enhancement New feature or request label Jun 21, 2018
@zmasek
Copy link
Author

zmasek commented Jun 21, 2018

Hi, @mongkok,

I tried to loop through the fields, but had some issues and got impatient. I ended up annotating fields with AsGeoJSON function and displaying them separately. I'd have a list of fields, though. Not a collection because it might be easier to mutate... maybe... thoughts?

@mongkok
Copy link
Member

mongkok commented Jun 22, 2018

Hi @zmasek,

You should not have any problem with mutations using more than one geometry field.
For resolvers, I think the GeometryCollection type could be the most appropriate.

mutation CreatePlace($fieldA: Geometry!, $fieldB: Geometry!) {
  createPlace(fieldA: $fieldA, fieldB: $fieldB) {
    place {
      geometries {
        type
        coordinates
      }
      properties {
        ...
      }
    }
  }
}

For now, you could use properties for the second geometry field:

mutation CreatePlace($fieldA: Geometry!, $fieldB: Geometry!) {
  createPlace(fieldA: $fieldA, fieldB: $fieldB) {
    place {
      geometry {
        type
        coordinates
      }
      properties {
        fieldB {
          type
          coordinates
        }
      }
    }
  }
}

@zmasek
Copy link
Author

zmasek commented Jun 23, 2018 via email

@mongkok
Copy link
Member

mongkok commented Jun 24, 2018

Then you could select a single geometry field within geojson_field.

@zmasek
Copy link
Author

zmasek commented Jun 24, 2018

And filtering would work in that case, I presume. How can I help?

@mongkok
Copy link
Member

mongkok commented Jun 29, 2018

I would like to create the GeometryCollection field soon, PRs are also welcome :)

@muriloacs
Copy link

muriloacs commented Jul 15, 2020

You can simply import the graphql_geojson.converter. This worked for me:

Model

from django.contrib.gis.db import models


class Partner(models.Model):
    name = models.CharField(max_length=100, blank=False)
    coverage_area = models.MultiPolygonField()
    address = models.PointField()

Type

from graphene import relay
from graphene_django.types import DjangoObjectType
from graphql_geojson import converter  # noqa

from myapp.partner.models import Partner


class PartnerType(DjangoObjectType):
    class Meta:
        model = Partner
        filter_fields = ['id']
        interfaces = (relay.Node, )

Query

from graphene import ObjectType, relay
from graphene_django.filter import DjangoFilterConnectionField

from .types import PartnerType


class PartnerQuery(ObjectType):
    partner = relay.Node.Field(PartnerType)
    all_partners = DjangoFilterConnectionField(PartnerType)

Even better because the API response body will not change:

Request

query {
  partner (id: "UGFydG5lclR5cGU6MQ==") {
    id
    name
    coverageArea {
      type
      coordinates
    }
    address {
      type
      coordinates
    }
  }
}

Response

{
  "data": {
    "partner": {
      "id": "UGFydG5lclR5cGU6MQ==",
      "name": "My Partner",
      "coverageArea": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                8.988174792345296,
                7.305908202108059
              ],
              [
                9.297306855044836,
                7.673950194244314
              ],
              [
                8.966471297361368,
                7.855224608281604
              ],
              [
                8.906780006290315,
                7.399291991157497
              ],
              [
                8.97732320720076,
                7.316894530231506
              ],
              [
                8.982749040383606,
                7.322387694293275
              ],
              [
                8.988174792345296,
                7.305908202108059
              ]
            ]
          ]
        ]
      },
      "address": {
        "type": "Point",
        "coordinates": [
          6.740986207824642,
          6.225814818469395
        ]
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants