Skip to content

Commit 4e23c3c

Browse files
committed
Improved python syntax and sorts (pep8). Improved Readme
1 parent 0434899 commit 4e23c3c

27 files changed

+131
-68
lines changed

.coveragerc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
omit = */tests/*,graphene_django/debug/sql/*

README.md

+31-21
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,10 @@ Please read [UPGRADE-v1.0.md](https://github.com/graphql-python/graphene/blob/ma
33

44
---
55

6-
# ![Graphene Logo](http://graphene-python.org/favicon.png) [Graphene-Django](http://graphene-python.org) [![Build Status](https://travis-ci.org/graphql-python/graphene-django.svg?branch=master)](https://travis-ci.org/graphql-python/graphene-django) [![PyPI version](https://badge.fury.io/py/graphene-django.svg)](https://badge.fury.io/py/graphene-django) [![Coverage Status](https://coveralls.io/repos/graphql-python/graphene-django/badge.svg?branch=master&service=github)](https://coveralls.io/github/graphql-python/graphene-django?branch=master)
6+
# ![Graphene Logo](http://graphene-python.org/favicon.png) Graphene-Django [![Build Status](https://travis-ci.org/graphql-python/graphene-django.svg?branch=master)](https://travis-ci.org/graphql-python/graphene-django) [![PyPI version](https://badge.fury.io/py/graphene-django.svg)](https://badge.fury.io/py/graphene-django) [![Coverage Status](https://coveralls.io/repos/graphql-python/graphene-django/badge.svg?branch=master&service=github)](https://coveralls.io/github/graphql-python/graphene-django?branch=master)
77

88

9-
[Graphene](http://graphene-python.org) is a Python library for building GraphQL schemas/types fast and easily.
10-
11-
- **Easy to use:** Graphene helps you use GraphQL in Python without effort.
12-
- **Relay:** Graphene has builtin support for Relay
13-
- **Django:** Automatic *Django model* mapping to Graphene Types. Check a fully working [Django](http://github.com/graphql-python/swapi-graphene) implementation
14-
15-
Graphene also supports *SQLAlchemy*!
16-
17-
*What is supported in this Python version?* **Everything**: Interfaces, ObjectTypes, Scalars, Unions and Relay (Nodes, Connections), in addition to queries, mutations and subscriptions.
18-
19-
**NEW**!: [Try graphene online](http://graphene-python.org/playground/)
9+
A [Django](https://www.djangoproject.com/) integration for [Graphene](http://graphene-python.org/).
2010

2111
## Installation
2212

@@ -28,30 +18,50 @@ pip install "graphene-django>=1.0.dev"
2818

2919
## Examples
3020

31-
Here is one example for get you started:
21+
Here is a simple Django model:
3222

3323
```python
3424
from django.db import models
35-
from graphene_django import DjangoObjectType
3625

3726
class UserModel(models.Model):
3827
name = models.CharField(max_length=100)
3928
last_name = models.CharField(max_length=100)
29+
```
30+
31+
To create a GraphQL schema for it you simply have to write the following:
32+
33+
```python
34+
from graphene_django import DjangoObjectType
4035

4136
class User(DjangoObjectType):
4237
class Meta:
43-
# This type will transform all the UserModel fields
44-
# into Graphene fields automatically
4538
model = UserModel
4639

47-
# An extra field in the User Type
48-
full_name = graphene.String()
40+
class Query(graphene.ObjectType):
41+
users = graphene.List(User)
4942

50-
def resolve_full_name(self, args, context, info):
51-
return "{} {}".format(self.name, self.last_name)
43+
@graphene.resolve_only_args
44+
def resolve_users(self):
45+
return UserModel.objects.all()
46+
47+
schema = graphene.Schema(query=QueryRoot)
48+
```
49+
50+
Then you can simply query the schema:
51+
52+
```python
53+
query = '''
54+
query {
55+
users {
56+
name,
57+
lastName
58+
}
59+
}
60+
'''
61+
result = schema.execute(query)
5262
```
5363

54-
If you want to learn even more, you can also check the following [examples](examples/):
64+
To learn more check out the following [examples](examples/):
5565

5666
* **Schema with Filtering**: [Cookbook example](examples/cookbook)
5767
* **Relay Schema**: [Starwars Relay example](examples/starwars)

django_test_settings.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import sys, os
1+
import sys
2+
import os
3+
24
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
35
sys.path.insert(0, ROOT_PATH + '/examples/')
46

examples/cookbook/cookbook/ingredients/schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from cookbook.ingredients.models import Category, Ingredient
2-
from graphene import ObjectType, Field, AbstractType, Node
2+
from graphene import AbstractType, Field, Node
33
from graphene_django.filter import DjangoFilterConnectionField
44
from graphene_django.types import DjangoObjectType
55

examples/cookbook/cookbook/schema.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import graphene
21
import cookbook.ingredients.schema
2+
import graphene
3+
34

45
# print cookbook.ingredients.schema.Query._meta.graphql_type.get_fields()['allIngredients'].args
56

7+
68
class Query(cookbook.ingredients.schema.Query, graphene.ObjectType):
79
pass
810

examples/starwars/schema.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import graphene
2-
from graphene import relay, resolve_only_args, Schema
2+
from graphene import Schema, relay, resolve_only_args
33
from graphene_django import DjangoObjectType
44

55
from .data import (create_ship, get_empire, get_faction, get_rebels, get_ship,
66
get_ships)
7-
from .models import (
8-
Character as CharacterModel,
9-
Faction as FactionModel,
10-
Ship as ShipModel
11-
)
7+
from .models import Character as CharacterModel
8+
from .models import Faction as FactionModel
9+
from .models import Ship as ShipModel
1210

1311

1412
class Ship(DjangoObjectType):

graphene_django/converter.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from django.db import models
22
from django.utils.encoding import force_text
33

4-
from graphene import Enum, List, ID, Boolean, Float, Int, String, Field, NonNull, Field, Dynamic
5-
from graphene.types.json import JSONString
4+
from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List,
5+
NonNull, String)
6+
from graphene.relay import is_node
67
from graphene.types.datetime import DateTime
8+
from graphene.types.json import JSONString
79
from graphene.utils.str_converters import to_const
8-
from graphene.relay import is_node
910

1011
from .compat import (ArrayField, HStoreField, JSONField, RangeField,
1112
RelatedObject, UUIDField)
12-
from .utils import get_related_model, import_single_dispatch
1313
from .fields import get_connection_field
14+
from .utils import get_related_model, import_single_dispatch
1415

1516
singledispatch = import_single_dispatch()
1617

@@ -37,9 +38,10 @@ def convert_django_field_with_choices(field, registry=None):
3738
name = '{}{}'.format(meta.object_name, field.name.capitalize())
3839
choices = list(get_choices(choices))
3940
named_choices = [(c[0], c[1]) for c in choices]
40-
named_choices_descriptions = {c[0]:c[2] for c in choices}
41+
named_choices_descriptions = {c[0]: c[2] for c in choices}
4142

4243
class EnumWithDescriptionsType(object):
44+
4345
@property
4446
def description(self):
4547
return named_choices_descriptions[self.name]

graphene_django/debug/middleware.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from promise import Promise
21
from django.db import connections
32

3+
from promise import Promise
4+
45
from .sql.tracking import unwrap_cursor, wrap_cursor
56
from .types import DjangoDebug
67

graphene_django/debug/types.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from graphene import ObjectType, List
1+
from graphene import List, ObjectType
2+
23
from .sql.types import DjangoDebugSQL
34

45

graphene_django/fields.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from functools import partial
22

33
from django.db.models.query import QuerySet
4+
45
from graphene.relay import ConnectionField, PageInfo
56
from graphql_relay.connection.arrayconnection import connection_from_list_slice
6-
from .utils import maybe_queryset, DJANGO_FILTER_INSTALLED
7+
8+
from .utils import DJANGO_FILTER_INSTALLED, maybe_queryset
79

810

911
class DjangoConnectionField(ConnectionField):

graphene_django/filter/fields.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from functools import partial
2+
23
from ..fields import DjangoConnectionField
34
from .utils import get_filtering_args_from_filterset, get_filterset_class
45

graphene_django/filter/filterset.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from django_filters import Filter, MultipleChoiceFilter
66
from django_filters.filterset import FilterSet, FilterSetMetaclass
77

8-
from ..forms import GlobalIDFormField, GlobalIDMultipleChoiceField
98
from graphql_relay.node.node import from_global_id
109

10+
from ..forms import GlobalIDFormField, GlobalIDMultipleChoiceField
11+
1112

1213
class GlobalIDFilter(Filter):
1314
field_class = GlobalIDFormField

graphene_django/filter/tests/test_fields.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from graphene import ObjectType, Schema, Field
5+
from graphene import Field, ObjectType, Schema
66
from graphene.relay import Node
77
from graphene_django import DjangoObjectType
88
from graphene_django.forms import (GlobalIDFormField,

graphene_django/filter/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import six
22

3-
from graphene import Argument, String
3+
from graphene import String
4+
45
from .filterset import custom_filterset_factory, setup_filterset
56

67

graphene_django/form_converter.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django import forms
22
from django.forms.fields import BaseTemporalField
33

4-
from graphene import ID, Boolean, Float, Int, String, List
4+
from graphene import ID, Boolean, Float, Int, List, String
5+
56
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
67
from .utils import import_single_dispatch
78

graphene_django/management/commands/graphql_schema.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ def handle(self, *args, **options):
6767
self.save_file(out, schema_dict)
6868

6969
style = getattr(self, 'style', None)
70-
SUCCESS = getattr(style, 'SUCCESS', lambda x: x)
70+
success = getattr(style, 'SUCCESS', lambda x: x)
7171

72-
self.stdout.write(SUCCESS('Successfully dumped GraphQL schema to %s' % out))
72+
self.stdout.write(success('Successfully dumped GraphQL schema to %s' % out))

graphene_django/registry.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
class Registry(object):
2+
23
def __init__(self):
34
self._registry = {}
45
self._registry_models = {}
56

67
def register(self, cls):
78
from .types import DjangoObjectType
8-
assert issubclass(cls, DjangoObjectType), 'Only DjangoObjectTypes can be registered, received "{}"'.format(cls.__name__)
9+
assert issubclass(
10+
cls, DjangoObjectType), 'Only DjangoObjectTypes can be registered, received "{}"'.format(
11+
cls.__name__)
912
assert cls._meta.registry == self, 'Registry for a Model have to match.'
10-
# assert self.get_type_for_model(cls._meta.model) == cls, 'Multiple DjangoObjectTypes registered for "{}"'.format(cls._meta.model)
13+
# assert self.get_type_for_model(cls._meta.model) == cls, (
14+
# 'Multiple DjangoObjectTypes registered for "{}"'.format(cls._meta.model)
15+
# )
1116
self._registry[cls._meta.model] = cls
1217

1318
def get_type_for_model(self, model):

graphene_django/tests/schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import graphene
22
from graphene import Schema, relay
3-
from ..types import DjangoObjectType
43

4+
from ..types import DjangoObjectType
55
from .models import Article, Reporter
66

77

graphene_django/tests/test_converter.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
from py.test import raises
55

66
import graphene
7-
from graphene.relay import Node, ConnectionField
7+
from graphene.relay import ConnectionField, Node
88
from graphene.types.datetime import DateTime
99
from graphene.types.json import JSONString
10-
# from graphene.core.types.custom_scalars import DateTime, JSONString
1110

1211
from ..compat import (ArrayField, HStoreField, JSONField, MissingType,
1312
RangeField)
1413
from ..converter import convert_django_field, convert_django_field_with_choices
1514
from ..registry import Registry
16-
from .models import Article, Reporter, Film, FilmDetails, Pet
1715
from ..types import DjangoObjectType
16+
from .models import Article, Film, FilmDetails, Reporter
17+
18+
19+
# from graphene.core.types.custom_scalars import DateTime, JSONString
20+
1821

1922

2023
def assert_conversion(django_field, graphene_field, *args, **kwargs):
@@ -166,6 +169,7 @@ def test_should_manytomany_convert_connectionorlist():
166169

167170
def test_should_manytomany_convert_connectionorlist_list():
168171
class A(DjangoObjectType):
172+
169173
class Meta:
170174
model = Reporter
171175

@@ -179,6 +183,7 @@ class Meta:
179183

180184
def test_should_manytomany_convert_connectionorlist_connection():
181185
class A(DjangoObjectType):
186+
182187
class Meta:
183188
model = Reporter
184189
interfaces = (Node, )
@@ -196,6 +201,7 @@ def test_should_manytoone_convert_connectionorlist():
196201
getattr(Reporter.articles, 'related')
197202

198203
class A(DjangoObjectType):
204+
199205
class Meta:
200206
model = Article
201207

@@ -213,6 +219,7 @@ def test_should_onetoone_reverse_convert_model():
213219
getattr(Film.details, 'related')
214220

215221
class A(DjangoObjectType):
222+
216223
class Meta:
217224
model = FilmDetails
218225

graphene_django/tests/test_form_converter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from py.test import raises
33

44
import graphene
5-
from ..form_converter import convert_form_field
65
from graphene import ID, List, NonNull
76

7+
from ..form_converter import convert_form_field
88
from .models import Reporter
99

1010

graphene_django/tests/test_query.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
from graphene.relay import Node
99

1010
from ..compat import MissingType, RangeField
11-
from ..types import DjangoObjectType
1211
from ..fields import DjangoConnectionField
13-
from ..registry import reset_global_registry, get_global_registry
12+
from ..types import DjangoObjectType
1413
from .models import Article, Reporter
1514

1615
pytestmark = pytest.mark.django_db

graphene_django/tests/test_schema.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from py.test import raises
22

3-
from ..types import DjangoObjectType
43
from ..registry import Registry
5-
4+
from ..types import DjangoObjectType
65
from .models import Reporter
76

87

@@ -24,10 +23,20 @@ class Meta:
2423

2524
def test_should_map_fields_correctly():
2625
class ReporterType2(DjangoObjectType):
26+
2727
class Meta:
2828
model = Reporter
2929
registry = Registry()
30-
assert list(ReporterType2._meta.fields.keys()) == ['id', 'first_name', 'last_name', 'email', 'pets', 'a_choice', 'articles', 'films']
30+
assert list(
31+
ReporterType2._meta.fields.keys()) == [
32+
'id',
33+
'first_name',
34+
'last_name',
35+
'email',
36+
'pets',
37+
'a_choice',
38+
'articles',
39+
'films']
3140

3241

3342
def test_should_map_only_few_fields():

0 commit comments

Comments
 (0)