Skip to main content

GeoJSON support for Django GraphQL

Project description

Pypi Wheel Build Status Codecov Code Climate

GeoJSON support for Django GraphQL

Dependencies

  • Python ≥ 3.4

  • Django ≥ 1.11

Installation

Update Graphene Django package.

pip install -U git+https://github.com/graphql-python/graphene-django.git@master

Install last stable version from Pypi.

pip install django-graphql-geojson

GeoJSONType

models.py

from django.contrib.gis.db import models


class Place(models.Model):
    name = models.CharField(max_length=255)
    location = models.PointField()

schema.py

from graphql_geojson.types import GeoJSONType


class PlaceType(GeoJSONType):

    class Meta:
        model = models.Place
        geojson_field = 'location'

Query

query {
  places {
    id
    type
    geometry {
      type
      coordinates
    }
    properties {
      name
    }
  }
}

GeoJSONInput

schema.py

import graphene

from graphql_geojson.types import GeoJSONInput


class CreatePlace(graphene.Mutation):
    place = graphene.Field(types.PlaceType)

    class Arguments:
        name = graphene.String(required=True)
        location = GeoJSONInput(required=True)

    @classmethod
    def mutate(cls, root, info, **args):
        place = models.Place.objects.create(**args)
        return cls(place=place)

Mutation

mutation CreatePlace($name: String!, $location: GeoJSONInput!) {
  createPlace(name: $name, location: $location) {
    place {
      id
    }
  }
}

GeoJSONInput object may be initialized in a few ways:

  • Well-known text (WKT):

'POINT(5 23)'
  • Hexadecimal (HEX):

'010100000000000000000014400000000000003740'
  • GeoJSON:

{
  "type": "Point",
  "coordinates": [
    5.000000,
    23.000000
  ]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-graphql-geojson-0.0.2.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

django_graphql_geojson-0.0.2-py2.py3-none-any.whl (6.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page