Skip to content

Commit 4e08819

Browse files
committed
Merge remote-tracking branch 'graphql-python/0.4.0' into use-graphql-django-view
2 parents 77a588a + 9548d69 commit 4e08819

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1487
-798
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Here is one example for get you started:
2828

2929
```python
3030
class Query(graphene.ObjectType):
31-
hello = graphene.StringField(description='A typical hello world')
32-
ping = graphene.StringField(description='Ping someone',
33-
to=graphene.Argument(graphene.String))
31+
hello = graphene.String(description='A typical hello world')
32+
ping = graphene.String(description='Ping someone',
33+
to=graphene.String())
3434

3535
def resolve_hello(self, args, info):
3636
return 'World'

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ Here is one example for get you started:
3535
.. code:: python
3636
3737
class Query(graphene.ObjectType):
38-
hello = graphene.StringField(description='A typical hello world')
39-
ping = graphene.StringField(description='Ping someone',
40-
to=graphene.Argument(graphene.String))
38+
hello = graphene.String(description='A typical hello world')
39+
ping = graphene.String(description='Ping someone',
40+
to=graphene.String())
4141
4242
def resolve_hello(self, args, info):
4343
return 'World'

bin/autolinter

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
22

3-
autoflake ./ -r --remove-unused-variables --remove-all-unused-imports --in-place
3+
autoflake ./ -r --remove-unused-variables --remove-all-unused-imports --in-place
4+
autopep8 ./ -r --in-place --experimental --aggressive --max-line-length 120
45
isort -rc .

examples/starwars/schema.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,33 @@
1212

1313

1414
class Character(graphene.Interface):
15-
id = graphene.IDField()
16-
name = graphene.StringField()
17-
friends = graphene.ListField('self')
18-
appears_in = graphene.ListField(Episode)
15+
id = graphene.ID()
16+
name = graphene.String()
17+
friends = graphene.List('Character')
18+
appears_in = graphene.List(Episode)
1919

2020
def resolve_friends(self, args, *_):
2121
# The character friends is a list of strings
2222
return [get_character(f) for f in self.friends]
2323

2424

2525
class Human(Character):
26-
home_planet = graphene.StringField()
26+
home_planet = graphene.String()
2727

2828

2929
class Droid(Character):
30-
primary_function = graphene.StringField()
30+
primary_function = graphene.String()
3131

3232

3333
class Query(graphene.ObjectType):
3434
hero = graphene.Field(Character,
3535
episode=graphene.Argument(Episode)
3636
)
3737
human = graphene.Field(Human,
38-
id=graphene.Argument(graphene.String)
38+
id=graphene.String()
3939
)
4040
droid = graphene.Field(Droid,
41-
id=graphene.Argument(graphene.String)
41+
id=graphene.String()
4242
)
4343

4444
@resolve_only_args

examples/starwars_django/schema.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414
class Ship(DjangoNode):
15+
1516
class Meta:
1617
model = ShipModel
1718

@@ -21,11 +22,13 @@ def get_node(cls, id):
2122

2223

2324
class Character(DjangoObjectType):
25+
2426
class Meta:
2527
model = CharacterModel
2628

2729

2830
class Faction(DjangoNode):
31+
2932
class Meta:
3033
model = FactionModel
3134

@@ -35,9 +38,10 @@ def get_node(cls, id):
3538

3639

3740
class IntroduceShip(relay.ClientIDMutation):
41+
3842
class Input:
39-
ship_name = graphene.StringField(required=True)
40-
faction_id = graphene.StringField(required=True)
43+
ship_name = graphene.String(required=True)
44+
faction_id = graphene.String(required=True)
4145

4246
ship = graphene.Field(Ship)
4347
faction = graphene.Field(Faction)
@@ -48,7 +52,7 @@ def mutate_and_get_payload(cls, input, info):
4852
faction_id = input.get('faction_id')
4953
ship = create_ship(ship_name, faction_id)
5054
faction = get_faction(faction_id)
51-
return IntroduceShip(ship=ship, faction=faction)
55+
return IntroduceShip(ship=Ship(ship), faction=Faction(faction))
5256

5357

5458
class Query(graphene.ObjectType):

examples/starwars_relay/schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Ship(relay.Node):
1010
'''A ship in the Star Wars saga'''
11-
name = graphene.StringField(description='The name of the ship.')
11+
name = graphene.String(description='The name of the ship.')
1212

1313
@classmethod
1414
def get_node(cls, id):
@@ -17,7 +17,7 @@ def get_node(cls, id):
1717

1818
class Faction(relay.Node):
1919
'''A faction in the Star Wars saga'''
20-
name = graphene.StringField(description='The name of the faction.')
20+
name = graphene.String(description='The name of the faction.')
2121
ships = relay.ConnectionField(
2222
Ship, description='The ships used by the faction.')
2323

@@ -34,8 +34,8 @@ def get_node(cls, id):
3434
class IntroduceShip(relay.ClientIDMutation):
3535

3636
class Input:
37-
ship_name = graphene.StringField(required=True)
38-
faction_id = graphene.StringField(required=True)
37+
ship_name = graphene.String(required=True)
38+
faction_id = graphene.String(required=True)
3939

4040
ship = graphene.Field(Ship)
4141
faction = graphene.Field(Faction)

graphene/__init__.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
from graphql.core.type import (
2-
GraphQLEnumType as Enum,
3-
GraphQLArgument as Argument,
4-
GraphQLString as String,
5-
GraphQLInt as Int,
6-
GraphQLID as ID
2+
GraphQLEnumType as Enum
73
)
84

95
from graphene import signals
@@ -14,12 +10,24 @@
1410

1511
from graphene.core.types import (
1612
ObjectType,
13+
InputObjectType,
1714
Interface,
1815
Mutation,
16+
BaseType,
17+
LazyType,
18+
Argument,
19+
Field,
20+
InputField,
21+
String,
22+
Int,
23+
Boolean,
24+
ID,
25+
Float,
26+
List,
27+
NonNull
1928
)
2029

2130
from graphene.core.fields import (
22-
Field,
2331
StringField,
2432
IntField,
2533
BooleanField,
@@ -33,7 +41,31 @@
3341
resolve_only_args
3442
)
3543

36-
__all__ = ['Enum', 'Argument', 'String', 'Int', 'ID', 'signals', 'Schema',
37-
'ObjectType', 'Interface', 'Mutation', 'Field', 'StringField',
38-
'IntField', 'BooleanField', 'IDField', 'ListField', 'NonNullField',
39-
'FloatField', 'resolve_only_args']
44+
__all__ = [
45+
'Enum',
46+
'Argument',
47+
'String',
48+
'Int',
49+
'Boolean',
50+
'Float',
51+
'ID',
52+
'List',
53+
'NonNull',
54+
'signals',
55+
'Schema',
56+
'BaseType',
57+
'LazyType',
58+
'ObjectType',
59+
'InputObjectType',
60+
'Interface',
61+
'Mutation',
62+
'Field',
63+
'InputField',
64+
'StringField',
65+
'IntField',
66+
'BooleanField',
67+
'IDField',
68+
'ListField',
69+
'NonNullField',
70+
'FloatField',
71+
'resolve_only_args']

graphene/contrib/django/converter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
from graphene.contrib.django.fields import (ConnectionOrListField,
55
DjangoModelField)
6-
from graphene.core.fields import (BooleanField, FloatField, IDField, IntField,
7-
StringField)
6+
from graphene.core.types.scalars import ID, Boolean, Float, Int, String
87

98
try:
109
UUIDField = models.UUIDField
@@ -17,7 +16,8 @@ class UUIDField(object):
1716
@singledispatch
1817
def convert_django_field(field):
1918
raise Exception(
20-
"Don't know how to convert the Django field %s (%s)" % (field, field.__class__))
19+
"Don't know how to convert the Django field %s (%s)" %
20+
(field, field.__class__))
2121

2222

2323
@convert_django_field.register(models.DateField)
@@ -28,12 +28,12 @@ def convert_django_field(field):
2828
@convert_django_field.register(models.URLField)
2929
@convert_django_field.register(UUIDField)
3030
def convert_field_to_string(field):
31-
return StringField(description=field.help_text)
31+
return String(description=field.help_text)
3232

3333

3434
@convert_django_field.register(models.AutoField)
3535
def convert_field_to_id(field):
36-
return IDField(description=field.help_text)
36+
return ID(description=field.help_text)
3737

3838

3939
@convert_django_field.register(models.PositiveIntegerField)
@@ -42,23 +42,23 @@ def convert_field_to_id(field):
4242
@convert_django_field.register(models.BigIntegerField)
4343
@convert_django_field.register(models.IntegerField)
4444
def convert_field_to_int(field):
45-
return IntField(description=field.help_text)
45+
return Int(description=field.help_text)
4646

4747

4848
@convert_django_field.register(models.BooleanField)
4949
def convert_field_to_boolean(field):
50-
return BooleanField(description=field.help_text, required=True)
50+
return Boolean(description=field.help_text, required=True)
5151

5252

5353
@convert_django_field.register(models.NullBooleanField)
5454
def convert_field_to_nullboolean(field):
55-
return BooleanField(description=field.help_text)
55+
return Boolean(description=field.help_text)
5656

5757

5858
@convert_django_field.register(models.DecimalField)
5959
@convert_django_field.register(models.FloatField)
6060
def convert_field_to_float(field):
61-
return FloatField(description=field.help_text)
61+
return Float(description=field.help_text)
6262

6363

6464
@convert_django_field.register(models.ManyToManyField)

graphene/contrib/django/fields.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,64 @@
11
from graphene import relay
22
from graphene.contrib.django.utils import get_type_for_model, lazy_map
3-
from graphene.core.fields import Field, LazyField, ListField
3+
from graphene.core.exceptions import SkipField
4+
from graphene.core.fields import Field
5+
from graphene.core.types.base import FieldType
6+
from graphene.core.types.definitions import List
47
from graphene.relay.utils import is_node
58

69

710
class DjangoConnectionField(relay.ConnectionField):
811

912
def wrap_resolved(self, value, instance, args, info):
1013
schema = info.schema.graphene_schema
11-
return lazy_map(value, self.get_object_type(schema))
14+
return lazy_map(value, self.type.get_object_type(schema))
1215

1316

14-
class LazyListField(ListField):
17+
class LazyListField(Field):
1518

16-
def resolve(self, instance, args, info):
19+
def get_type(self, schema):
20+
return List(self.type)
21+
22+
def resolver(self, instance, args, info):
1723
schema = info.schema.graphene_schema
18-
resolved = super(LazyListField, self).resolve(instance, args, info)
24+
resolved = super(LazyListField, self).resolver(instance, args, info)
1925
return lazy_map(resolved, self.get_object_type(schema))
2026

2127

22-
class ConnectionOrListField(LazyField):
28+
class ConnectionOrListField(Field):
2329

24-
def get_field(self, schema):
25-
model_field = self.field_type
30+
def internal_type(self, schema):
31+
model_field = self.type
2632
field_object_type = model_field.get_object_type(schema)
2733
if is_node(field_object_type):
2834
field = DjangoConnectionField(model_field)
2935
else:
3036
field = LazyListField(model_field)
3137
field.contribute_to_class(self.object_type, self.name)
32-
return field
38+
return field.internal_type(schema)
3339

3440

35-
class DjangoModelField(Field):
41+
class DjangoModelField(FieldType):
3642

3743
def __init__(self, model, *args, **kwargs):
38-
super(DjangoModelField, self).__init__(None, *args, **kwargs)
3944
self.model = model
40-
41-
def resolve(self, instance, args, info):
42-
resolved = super(DjangoModelField, self).resolve(instance, args, info)
43-
schema = info.schema.graphene_schema
44-
_type = self.get_object_type(schema)
45-
assert _type, ("Field %s cannot be retrieved as the "
46-
"ObjectType is not registered by the schema" % (
47-
self.attname
48-
))
49-
return _type(resolved)
45+
super(DjangoModelField, self).__init__(*args, **kwargs)
5046

5147
def internal_type(self, schema):
5248
_type = self.get_object_type(schema)
53-
if not _type and self.object_type._meta.only_fields:
49+
if not _type and self.parent._meta.only_fields:
5450
raise Exception(
5551
"Model %r is not accessible by the schema. "
5652
"You can either register the type manually "
5753
"using @schema.register. "
58-
"Or disable the field %s in %s" % (
54+
"Or disable the field in %s" % (
5955
self.model,
60-
self.attname,
61-
self.object_type
56+
self.parent,
6257
)
6358
)
64-
return schema.T(_type) or Field.SKIP
59+
if not _type:
60+
raise SkipField()
61+
return schema.T(_type)
6562

6663
def get_object_type(self, schema):
6764
return get_type_for_model(schema, self.model)

graphene/contrib/django/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def contribute_to_class(self, cls, name):
3232
return
3333
if not self.model:
3434
raise Exception(
35-
'Django ObjectType %s must have a model in the Meta class attr' % cls)
35+
'Django ObjectType %s must have a model in the Meta class attr' %
36+
cls)
3637
elif not inspect.isclass(self.model) or not issubclass(self.model, models.Model):
3738
raise Exception('Provided model in %s is not a Django model' % cls)

0 commit comments

Comments
 (0)