Skip to content

Commit ec3f292

Browse files
committed
Refactored arguments and fields logic
1 parent 21dffa4 commit ec3f292

File tree

7 files changed

+28
-28
lines changed

7 files changed

+28
-28
lines changed

graphene/core/classtypes/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,25 @@ class FieldsClassTypeMeta(ClassTypeMeta):
9292

9393
def extend_fields(cls, bases):
9494
new_fields = cls._meta.local_fields
95-
field_names = {f.name: f for f in new_fields}
95+
field_names = {f.attname: f for f in new_fields}
9696

9797
for base in bases:
9898
if not isinstance(base, FieldsClassTypeMeta):
9999
continue
100100

101101
parent_fields = base._meta.local_fields
102102
for field in parent_fields:
103-
if field.name in field_names and field.type.__class__ != field_names[
104-
field.name].type.__class__:
103+
if field.attname in field_names and field.type.__class__ != field_names[
104+
field.attname].type.__class__:
105105
raise Exception(
106106
'Local field %r in class %r (%r) clashes '
107107
'with field with similar name from '
108108
'Interface %s (%r)' % (
109-
field.name,
109+
field.attname,
110110
cls.__name__,
111111
field.__class__,
112112
base.__name__,
113-
field_names[field.name].__class__)
113+
field_names[field.attname].__class__)
114114
)
115115
new_field = copy.copy(field)
116116
cls.add_to_class(field.attname, new_field)

graphene/core/tests/test_old_fields.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ def test_field_type():
3434
assert schema.T(f).type == GraphQLString
3535

3636

37-
def test_field_name_automatic_camelcase():
37+
def test_field_name():
3838
f = Field(GraphQLString)
3939
f.contribute_to_class(MyOt, 'field_name')
40-
assert f.name == 'fieldName'
40+
assert f.name is None
41+
assert f.attname == 'field_name'
4142

4243

4344
def test_field_name_use_name_if_exists():

graphene/core/types/argument.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from collections import OrderedDict
21
from functools import wraps
32
from itertools import chain
43

54
from graphql.core.type import GraphQLArgument
65

7-
from ...utils import ProxySnakeDict, to_camel_case
6+
from ...utils import ProxySnakeDict
87
from .base import ArgumentType, GroupNamedType, NamedType, OrderedType
98

109

@@ -14,6 +13,7 @@ def __init__(self, type, description=None, default=None,
1413
name=None, _creation_counter=None):
1514
super(Argument, self).__init__(_creation_counter=_creation_counter)
1615
self.name = name
16+
self.attname = None
1717
self.type = type
1818
self.description = description
1919
self.default = default
@@ -38,20 +38,21 @@ def to_arguments(*args, **kwargs):
3838
arguments = {}
3939
iter_arguments = chain(kwargs.items(), [(None, a) for a in args])
4040

41-
for name, arg in iter_arguments:
41+
for attname, arg in iter_arguments:
4242
if isinstance(arg, Argument):
4343
argument = arg
4444
elif isinstance(arg, ArgumentType):
4545
argument = arg.as_argument()
4646
else:
47-
raise ValueError('Unknown argument %s=%r' % (name, arg))
48-
49-
if name:
50-
argument.name = to_camel_case(name)
51-
assert argument.name, 'Argument in field must have a name'
52-
assert argument.name not in arguments, 'Found more than one Argument with same name {}'.format(
53-
argument.name)
54-
arguments[argument.name] = argument
47+
raise ValueError('Unknown argument %s=%r' % (attname, arg))
48+
49+
if attname:
50+
argument.attname = attname
51+
52+
name = argument.name or argument.attname
53+
assert name, 'Argument in field must have a name'
54+
assert name not in arguments, 'Found more than one Argument with same name {}'.format(name)
55+
arguments[name] = argument
5556

5657
return sorted(arguments.values())
5758

graphene/core/types/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import six
55

6+
from ...utils import to_camel_case
7+
68

79
class BaseType(object):
810

@@ -138,7 +140,8 @@ def __init__(self, *types):
138140
self.types = types
139141

140142
def get_named_type(self, schema, type):
141-
return type.name or type.attname, schema.T(type)
143+
name = type.name or to_camel_case(type.attname)
144+
return name, schema.T(type)
142145

143146
def internal_type(self, schema):
144147
return OrderedDict(map(partial(self.get_named_type, schema), self.types))

graphene/core/types/field.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import six
55
from graphql.core.type import GraphQLField, GraphQLInputObjectField
66

7-
from ...utils import to_camel_case
87
from ..classtypes.base import FieldsClassType
98
from ..classtypes.inputobjecttype import InputObjectType
109
from ..classtypes.mutation import Mutation
@@ -37,8 +36,6 @@ def contribute_to_class(self, cls, attname):
3736
assert issubclass(
3837
cls, (FieldsClassType)), 'Field {} cannot be mounted in {}'.format(
3938
self, cls)
40-
if not self.name:
41-
self.name = to_camel_case(attname)
4239
self.attname = attname
4340
self.object_type = cls
4441
self.mount(cls)
@@ -134,8 +131,6 @@ def contribute_to_class(self, cls, attname):
134131
assert issubclass(
135132
cls, (InputObjectType)), 'InputField {} cannot be mounted in {}'.format(
136133
self, cls)
137-
if not self.name:
138-
self.name = to_camel_case(attname)
139134
self.attname = attname
140135
self.object_type = cls
141136
self.mount(cls)

graphene/core/types/tests/test_argument.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def test_to_arguments():
2727
other_kwarg=String(),
2828
)
2929

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

3333

3434
def test_to_arguments_no_name():

graphene/core/types/tests/test_field.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Query(ObjectType):
2020
schema = Schema(query=Query)
2121

2222
type = schema.T(field)
23-
assert field.name == 'myField'
23+
assert field.name is None
2424
assert field.attname == 'my_field'
2525
assert isinstance(type, GraphQLField)
2626
assert type.description == 'My argument'
@@ -116,7 +116,7 @@ class Query(ObjectType):
116116
schema = Schema(query=MyObjectType)
117117

118118
type = schema.T(field)
119-
assert field.name == 'myField'
119+
assert field.name is None
120120
assert field.attname == 'my_field'
121121
assert isinstance(type, GraphQLInputObjectField)
122122
assert type.description == 'My input field'

0 commit comments

Comments
 (0)