Skip to content

Commit 598f1b8

Browse files
committed
Django support (WIP)
1 parent 3b0da96 commit 598f1b8

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ python:
44
- 2.7
55
install:
66
- pip install pytest flake8
7-
- pip install -e .
7+
- pip install -e .[django]
88
script:
99
- py.test
1010
- flake8

graphql/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def resolve(self, schema):
9090
args, self.resolver, self.deprecation_reason, self.description
9191
)
9292

93+
def __call__(self, resolver):
94+
# Called when used as decorator
95+
self.resolver = resolver
96+
return self
97+
9398

9499
class LazyArgument(object):
95100
def __init__(self, typerefspec, default_value=None, description=None):

graphql/contrib/__init__.py

Whitespace-only changes.

graphql/contrib/django/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from graphql.api import Schema
2+
from django.db import models
3+
4+
5+
class DjangoSchema(Schema):
6+
def __init__(self):
7+
super(DjangoSchema, self).__init__()
8+
9+
def _define_object(self, dct):
10+
# Override
11+
model = dct.get('__model__')
12+
if model:
13+
for field in model._meta.get_fields():
14+
if field.is_relation:
15+
pass # TODO
16+
else:
17+
if isinstance(field, models.CharField):
18+
dct[field.name] = self.Field(self.String)
19+
# ... TODO
20+
return super(DjangoSchema, self)._define_object(dct)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def run_tests(self):
4949
install_requires=[],
5050
tests_require=['pytest>=2.7.2'],
5151
extras_require={
52-
'django': [],
52+
'django': ['Django>=1.8.0,<1.9'],
5353
},
5454

5555
cmdclass={'test': PyTest},

tests/contrib_django/test_django.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import django
2+
from django.conf import settings
3+
from graphql import graphql
4+
from graphql.contrib.django import DjangoSchema
5+
6+
settings.configure(
7+
DATABASES={
8+
'INSTALLED_APPS': [
9+
'graphql',
10+
],
11+
'default': {
12+
'ENGINE': 'django.db.backends.sqlite3',
13+
}
14+
}
15+
)
16+
17+
from django.db import models
18+
19+
class Human(models.Model):
20+
name = models.CharField()
21+
22+
class Meta:
23+
app_label = 'graphql'
24+
25+
django.setup()
26+
27+
28+
def test_auto_definition():
29+
gql = DjangoSchema()
30+
31+
class HumanType(gql.ObjectType):
32+
__typename__ = 'Human'
33+
__model__ = Human
34+
35+
class QueryRoot(gql.QueryRoot):
36+
@gql.Field(gql.List(gql.NonNull(HumanType)))
37+
def humans(self, *args, **kwargs):
38+
return [Human(name='hi')]
39+
40+
result = graphql(gql.to_internal(), '''{
41+
humans { name }
42+
}''')
43+
assert not result.errors
44+
assert result.data == {
45+
'humans': [
46+
{'name': 'hi'}
47+
]
48+
}

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ envlist = py27
44
[testenv]
55
deps=
66
pytest>=2.7.2
7+
django>=1.8.0,<1.9
78
flake8
89
commands=
910
py.test

0 commit comments

Comments
 (0)