Skip to content

Commit bd35fce

Browse files
committed
Merge branch 'refs/heads/features/plugins-autocamelcase' into features/django-debug
2 parents a153a01 + cd5d9b8 commit bd35fce

File tree

14 files changed

+80
-130
lines changed

14 files changed

+80
-130
lines changed

graphene/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Interface,
1212
Mutation,
1313
Scalar,
14-
BaseType,
14+
InstanceType,
1515
LazyType,
1616
Argument,
1717
Field,
@@ -51,7 +51,7 @@
5151
'NonNull',
5252
'signals',
5353
'Schema',
54-
'BaseType',
54+
'InstanceType',
5555
'LazyType',
5656
'ObjectType',
5757
'InputObjectType',

graphene/core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212

1313
from .types import (
14-
BaseType,
14+
InstanceType,
1515
LazyType,
1616
Argument,
1717
Field,
@@ -35,7 +35,7 @@
3535
'List',
3636
'NonNull',
3737
'Schema',
38-
'BaseType',
38+
'InstanceType',
3939
'LazyType',
4040
'ObjectType',
4141
'InputObjectType',

graphene/core/classtypes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import copy
22
import inspect
3-
from functools import partial
43
from collections import OrderedDict
4+
from functools import partial
55

66
import six
77

graphene/core/schema.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
from graphene import signals
1212

13+
from ..plugins import CamelCase, Plugin
1314
from .classtypes.base import ClassType
14-
from .types.base import BaseType
15-
from ..plugins import Plugin, CamelCase
15+
from .types.base import InstanceType
1616

1717

1818
class GraphQLSchema(_GraphQLSchema):
@@ -50,27 +50,27 @@ def add_plugin(self, plugin):
5050
plugin.contribute_to_schema(self)
5151
self.plugins.append(plugin)
5252

53-
def get_internal_type(self, objecttype):
53+
def get_default_namedtype_name(self, value):
5454
for plugin in self.plugins:
55-
objecttype = plugin.transform_type(objecttype)
56-
return objecttype.internal_type(self)
55+
if not hasattr(plugin, 'get_default_namedtype_name'):
56+
continue
57+
value = plugin.get_default_namedtype_name(value)
58+
return value
5759

58-
def T(self, object_type):
59-
if not object_type:
60+
def T(self, _type):
61+
if not _type:
6062
return
61-
if inspect.isclass(object_type) and issubclass(
62-
object_type, (BaseType, ClassType)) or isinstance(
63-
object_type, BaseType):
64-
if object_type not in self._types:
65-
internal_type = self.get_internal_type(object_type)
66-
self._types[object_type] = internal_type
67-
is_objecttype = inspect.isclass(
68-
object_type) and issubclass(object_type, ClassType)
69-
if is_objecttype:
70-
self.register(object_type)
71-
return self._types[object_type]
63+
is_classtype = inspect.isclass(_type) and issubclass(_type, ClassType)
64+
is_instancetype = isinstance(_type, InstanceType)
65+
if is_classtype or is_instancetype:
66+
if _type not in self._types:
67+
internal_type = _type.internal_type(self)
68+
self._types[_type] = internal_type
69+
if is_classtype:
70+
self.register(_type)
71+
return self._types[_type]
7272
else:
73-
return object_type
73+
return _type
7474

7575
@property
7676
def executor(self):

graphene/core/types/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from .base import BaseType, LazyType, OrderedType
1+
from .base import InstanceType, LazyType, OrderedType
22
from .argument import Argument, ArgumentsGroup, to_arguments
33
from .definitions import List, NonNull
44
# Compatibility import
55
from .objecttype import Interface, ObjectType, Mutation, InputObjectType
66

7-
from .scalars import String, ID, Boolean, Int, Float, Scalar
7+
from .scalars import String, ID, Boolean, Int, Float
88
from .field import Field, InputField
99

1010
__all__ = [
11-
'BaseType',
11+
'InstanceType',
1212
'LazyType',
1313
'OrderedType',
1414
'Argument',
@@ -26,5 +26,4 @@
2626
'ID',
2727
'Boolean',
2828
'Int',
29-
'Float',
30-
'Scalar']
29+
'Float']

graphene/core/types/argument.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ class Argument(NamedType, OrderedType):
1111

1212
def __init__(self, type, description=None, default=None,
1313
name=None, _creation_counter=None):
14-
super(Argument, self).__init__(_creation_counter=_creation_counter)
15-
self.name = name
16-
self.attname = None
14+
super(Argument, self).__init__(name=name, _creation_counter=_creation_counter)
1715
self.type = type
1816
self.description = description
1917
self.default = default
@@ -38,18 +36,18 @@ def to_arguments(*args, **kwargs):
3836
arguments = {}
3937
iter_arguments = chain(kwargs.items(), [(None, a) for a in args])
4038

41-
for attname, arg in iter_arguments:
39+
for default_name, arg in iter_arguments:
4240
if isinstance(arg, Argument):
4341
argument = arg
4442
elif isinstance(arg, ArgumentType):
4543
argument = arg.as_argument()
4644
else:
47-
raise ValueError('Unknown argument %s=%r' % (attname, arg))
45+
raise ValueError('Unknown argument %s=%r' % (default_name, arg))
4846

49-
if attname:
50-
argument.attname = attname
47+
if default_name:
48+
argument.default_name = default_name
5149

52-
name = argument.name or argument.attname
50+
name = argument.name or argument.default_name
5351
assert name, 'Argument in field must have a name'
5452
assert name not in arguments, 'Found more than one Argument with same name {}'.format(name)
5553
arguments[name] = argument

graphene/core/types/base.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
from collections import OrderedDict
2-
from functools import total_ordering, partial
2+
from functools import partial, total_ordering
33

44
import six
55

6-
from ...utils import to_camel_case
76

7+
class InstanceType(object):
88

9-
class BaseType(object):
10-
11-
@classmethod
12-
def internal_type(cls, schema):
13-
return getattr(cls, 'T', None)
9+
def internal_type(self, schema):
10+
raise NotImplementedError("internal_type for type {} is not implemented".format(self.__class__.__name__))
1411

1512

16-
class MountType(BaseType):
13+
class MountType(InstanceType):
1714
parent = None
1815

1916
def mount(self, cls):
@@ -131,20 +128,27 @@ class MountedType(FieldType, ArgumentType):
131128
pass
132129

133130

134-
class NamedType(BaseType):
135-
pass
131+
class NamedType(InstanceType):
132+
def __init__(self, name=None, default_name=None, *args, **kwargs):
133+
self.name = name
134+
self.default_name = None
135+
super(NamedType, self).__init__(*args, **kwargs)
136136

137137

138-
class GroupNamedType(BaseType):
138+
class GroupNamedType(InstanceType):
139+
139140
def __init__(self, *types):
140141
self.types = types
141142

142143
def get_named_type(self, schema, type):
143-
name = type.name or type.attname
144+
name = type.name or schema.get_default_namedtype_name(type.default_name)
144145
return name, schema.T(type)
145146

147+
def iter_types(self, schema):
148+
return map(partial(self.get_named_type, schema), self.types)
149+
146150
def internal_type(self, schema):
147-
return OrderedDict(map(partial(self.get_named_type, schema), self.types))
151+
return OrderedDict(self.iter_types(schema))
148152

149153
def __len__(self):
150154
return len(self.types)

graphene/core/types/field.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..classtypes.mutation import Mutation
1010
from ..exceptions import SkipField
1111
from .argument import ArgumentsGroup, snake_case_args
12-
from .base import LazyType, NamedType, MountType, OrderedType, GroupNamedType
12+
from .base import GroupNamedType, LazyType, MountType, NamedType, OrderedType
1313
from .definitions import NonNull
1414

1515

@@ -19,8 +19,7 @@ def __init__(
1919
self, type, description=None, args=None, name=None, resolver=None,
2020
required=False, default=None, *args_list, **kwargs):
2121
_creation_counter = kwargs.pop('_creation_counter', None)
22-
super(Field, self).__init__(_creation_counter=_creation_counter)
23-
self.name = name
22+
super(Field, self).__init__(name=name, _creation_counter=_creation_counter)
2423
if isinstance(type, six.string_types):
2524
type = LazyType(type)
2625
self.required = required
@@ -37,6 +36,7 @@ def contribute_to_class(self, cls, attname):
3736
cls, (FieldsClassType)), 'Field {} cannot be mounted in {}'.format(
3837
self, cls)
3938
self.attname = attname
39+
self.default_name = attname
4040
self.object_type = cls
4141
self.mount(cls)
4242
if isinstance(self.type, MountType):
@@ -120,7 +120,6 @@ class InputField(NamedType, OrderedType):
120120
def __init__(self, type, description=None, default=None,
121121
name=None, _creation_counter=None, required=False):
122122
super(InputField, self).__init__(_creation_counter=_creation_counter)
123-
self.name = name
124123
if required:
125124
type = NonNull(type)
126125
self.type = type
@@ -132,6 +131,7 @@ def contribute_to_class(self, cls, attname):
132131
cls, (InputObjectType)), 'InputField {} cannot be mounted in {}'.format(
133132
self, cls)
134133
self.attname = attname
134+
self.default_name = attname
135135
self.object_type = cls
136136
self.mount(cls)
137137
if isinstance(self.type, MountType):
@@ -145,11 +145,10 @@ def internal_type(self, schema):
145145

146146

147147
class FieldsGroupType(GroupNamedType):
148-
def internal_type(self, schema):
149-
fields = []
148+
149+
def iter_types(self, schema):
150150
for field in sorted(self.types):
151151
try:
152-
fields.append(self.get_named_type(schema, field))
152+
yield self.get_named_type(schema, field)
153153
except SkipField:
154154
continue
155-
return OrderedDict(fields)

graphene/core/types/scalars.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11
from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID,
2-
GraphQLInt, GraphQLScalarType, GraphQLString)
2+
GraphQLInt, GraphQLString)
33

44
from .base import MountedType
55

66

7-
class String(MountedType):
8-
T = GraphQLString
7+
class ScalarType(MountedType):
98

9+
def internal_type(self, schema):
10+
return self._internal_type
1011

11-
class Int(MountedType):
12-
T = GraphQLInt
1312

13+
class String(ScalarType):
14+
_internal_type = GraphQLString
1415

15-
class Boolean(MountedType):
16-
T = GraphQLBoolean
1716

17+
class Int(ScalarType):
18+
_internal_type = GraphQLInt
1819

19-
class ID(MountedType):
20-
T = GraphQLID
2120

21+
class Boolean(ScalarType):
22+
_internal_type = GraphQLBoolean
2223

23-
class Float(MountedType):
24-
T = GraphQLFloat
2524

25+
class ID(ScalarType):
26+
_internal_type = GraphQLID
2627

27-
class Scalar(MountedType):
2828

29-
@classmethod
30-
def internal_type(cls, schema):
31-
serialize = getattr(cls, 'serialize')
32-
parse_literal = getattr(cls, 'parse_literal')
33-
parse_value = getattr(cls, 'parse_value')
34-
35-
return GraphQLScalarType(
36-
name=cls.__name__,
37-
description=cls.__doc__,
38-
serialize=serialize,
39-
parse_value=parse_value,
40-
parse_literal=parse_literal
41-
)
29+
class Float(ScalarType):
30+
_internal_type = GraphQLFloat

graphene/core/types/tests/test_argument.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_to_arguments():
2727
other_kwarg=String(),
2828
)
2929

30-
assert [a.name or a.attname for a in arguments] == [
30+
assert [a.name or a.default_name for a in arguments] == [
3131
'myArg', 'otherArg', 'my_kwarg', 'other_kwarg']
3232

3333

0 commit comments

Comments
 (0)