Skip to content

Commit

Permalink
Merge pull request #14 from EverWinter23/fix/query-variables
Browse files Browse the repository at this point in the history
fix: GISScalar query variables
  • Loading branch information
EverWinter23 committed Feb 11, 2022
2 parents 5f891fa + 91a5557 commit 1c9c7e7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ $ python setup.py sdist
$ twine upload dist/*
```

### UPDATE

Targeting graphene-v3 update by March'22.

### LICENSE [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

This code falls under the MIT license which permits the reuse of the proprietary software provided that all copies of the licensed software include a copy of the MIT License terms and the copyright notice. Go crazy!
2 changes: 1 addition & 1 deletion graphene_gis/scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def parse_literal(cls, node):

@classmethod
def parse_value(cls, node):
geometry = GEOSGeometry(node.value)
geometry = GEOSGeometry(node)
return json.loads(geometry.geojson)


Expand Down
84 changes: 84 additions & 0 deletions graphene_gis/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json

import graphene
from django.contrib.gis.geos import GEOSGeometry
from graphene_gis.converter import gis_converter # noqa
from graphene_gis.scalars import PointScalar
from graphene_gis.tests.mocks import PointModel, PointModelType


Expand Down Expand Up @@ -69,3 +72,84 @@ def resolve_point_model(self, info):
result = schema.execute(query)
assert not result.errors
assert json.loads(json.dumps(dict(result.data))) == expected


"""
NOTE: Refer https://github.com/EverWinter23/graphene-gis/issues/9
Some evasive issue. I wouldn't reccomend using the following for
implementing mutations, because it involves some unecessary steps.
The converter will convert the WKT geometry to geojson, which you'll
have to convert back to WKT using GEOSGeometry to initialize the object.
It's way simple rather use the graphene.String for the input argument for
WKT inputs. This is demonstarted in the next test case.
"""
def test_scalar_query_variables():
class Query(graphene.ObjectType):
point_model = graphene.Field(PointModelType, location=PointScalar(required=True))

def resolve_point_model(self, info, location):
return PointModel(location=GEOSGeometry(f"{location}"), props={"type": "Feature"})

schema = graphene.Schema(query=Query)

query = """
query TestQuery($location: PointScalar!) {
pointModel(location: $location) {
location
props
}
}
"""

expected = {
"pointModel": {
"location": {
"type": "Point",
"coordinates": [42.0, 15.0]
},
"props": {
"type": "Feature"
}
}
}

result = schema.execute(query, variables={"location": "POINT(42.0 15.0)"})
assert not result.errors
assert json.loads(json.dumps(dict(result.data))) == expected


def test_native_query_variables():
class Query(graphene.ObjectType):
point_model = graphene.Field(PointModelType, location=graphene.String(required=True))

def resolve_point_model(self, info, location):
return PointModel(location=location, props={"type": "Feature"})

schema = graphene.Schema(query=Query)

query = """
query TestQuery($location: String!) {
pointModel(location: $location) {
location
props
}
}
"""

expected = {
"pointModel": {
"location": {
"type": "Point",
"coordinates": [42.0, 15.0]
},
"props": {
"type": "Feature"
}
}
}

result = schema.execute(query, variables={"location": "POINT(42.0 15.0)"})
assert not result.errors
assert json.loads(json.dumps(dict(result.data))) == expected

0 comments on commit 1c9c7e7

Please sign in to comment.