From ec3f29259389b94b294eb74efa0422d06c5727e5 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sun, 6 Dec 2015 01:51:03 -0800 Subject: [PATCH] Refactored arguments and fields logic --- graphene/core/classtypes/base.py | 10 +++++----- graphene/core/tests/test_old_fields.py | 5 +++-- graphene/core/types/argument.py | 21 +++++++++++---------- graphene/core/types/base.py | 5 ++++- graphene/core/types/field.py | 5 ----- graphene/core/types/tests/test_argument.py | 4 ++-- graphene/core/types/tests/test_field.py | 4 ++-- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/graphene/core/classtypes/base.py b/graphene/core/classtypes/base.py index 9ab51427..9dafff0e 100644 --- a/graphene/core/classtypes/base.py +++ b/graphene/core/classtypes/base.py @@ -92,7 +92,7 @@ class FieldsClassTypeMeta(ClassTypeMeta): def extend_fields(cls, bases): new_fields = cls._meta.local_fields - field_names = {f.name: f for f in new_fields} + field_names = {f.attname: f for f in new_fields} for base in bases: if not isinstance(base, FieldsClassTypeMeta): @@ -100,17 +100,17 @@ class FieldsClassTypeMeta(ClassTypeMeta): parent_fields = base._meta.local_fields for field in parent_fields: - if field.name in field_names and field.type.__class__ != field_names[ - field.name].type.__class__: + if field.attname in field_names and field.type.__class__ != field_names[ + field.attname].type.__class__: raise Exception( 'Local field %r in class %r (%r) clashes ' 'with field with similar name from ' 'Interface %s (%r)' % ( - field.name, + field.attname, cls.__name__, field.__class__, base.__name__, - field_names[field.name].__class__) + field_names[field.attname].__class__) ) new_field = copy.copy(field) cls.add_to_class(field.attname, new_field) diff --git a/graphene/core/tests/test_old_fields.py b/graphene/core/tests/test_old_fields.py index 95bf9aab..3f24aedf 100644 --- a/graphene/core/tests/test_old_fields.py +++ b/graphene/core/tests/test_old_fields.py @@ -34,10 +34,11 @@ def test_field_type(): assert schema.T(f).type == GraphQLString -def test_field_name_automatic_camelcase(): +def test_field_name(): f = Field(GraphQLString) f.contribute_to_class(MyOt, 'field_name') - assert f.name == 'fieldName' + assert f.name is None + assert f.attname == 'field_name' def test_field_name_use_name_if_exists(): diff --git a/graphene/core/types/argument.py b/graphene/core/types/argument.py index 0ef7686a..a8fe343e 100644 --- a/graphene/core/types/argument.py +++ b/graphene/core/types/argument.py @@ -1,10 +1,9 @@ -from collections import OrderedDict from functools import wraps from itertools import chain from graphql.core.type import GraphQLArgument -from ...utils import ProxySnakeDict, to_camel_case +from ...utils import ProxySnakeDict from .base import ArgumentType, GroupNamedType, NamedType, OrderedType @@ -14,6 +13,7 @@ class Argument(NamedType, OrderedType): name=None, _creation_counter=None): super(Argument, self).__init__(_creation_counter=_creation_counter) self.name = name + self.attname = None self.type = type self.description = description self.default = default @@ -38,20 +38,21 @@ def to_arguments(*args, **kwargs): arguments = {} iter_arguments = chain(kwargs.items(), [(None, a) for a in args]) - for name, arg in iter_arguments: + for attname, arg in iter_arguments: if isinstance(arg, Argument): argument = arg elif isinstance(arg, ArgumentType): argument = arg.as_argument() else: - raise ValueError('Unknown argument %s=%r' % (name, arg)) + raise ValueError('Unknown argument %s=%r' % (attname, arg)) - if name: - argument.name = to_camel_case(name) - assert argument.name, 'Argument in field must have a name' - assert argument.name not in arguments, 'Found more than one Argument with same name {}'.format( - argument.name) - arguments[argument.name] = argument + if attname: + argument.attname = attname + + name = argument.name or argument.attname + assert name, 'Argument in field must have a name' + assert name not in arguments, 'Found more than one Argument with same name {}'.format(name) + arguments[name] = argument return sorted(arguments.values()) diff --git a/graphene/core/types/base.py b/graphene/core/types/base.py index 920963b6..96523c7b 100644 --- a/graphene/core/types/base.py +++ b/graphene/core/types/base.py @@ -3,6 +3,8 @@ from functools import total_ordering, partial import six +from ...utils import to_camel_case + class BaseType(object): @@ -138,7 +140,8 @@ class GroupNamedType(BaseType): self.types = types def get_named_type(self, schema, type): - return type.name or type.attname, schema.T(type) + name = type.name or to_camel_case(type.attname) + return name, schema.T(type) def internal_type(self, schema): return OrderedDict(map(partial(self.get_named_type, schema), self.types)) diff --git a/graphene/core/types/field.py b/graphene/core/types/field.py index 9f2c3f01..cfe168f7 100644 --- a/graphene/core/types/field.py +++ b/graphene/core/types/field.py @@ -4,7 +4,6 @@ from functools import wraps import six from graphql.core.type import GraphQLField, GraphQLInputObjectField -from ...utils import to_camel_case from ..classtypes.base import FieldsClassType from ..classtypes.inputobjecttype import InputObjectType from ..classtypes.mutation import Mutation @@ -37,8 +36,6 @@ class Field(NamedType, OrderedType): assert issubclass( cls, (FieldsClassType)), 'Field {} cannot be mounted in {}'.format( self, cls) - if not self.name: - self.name = to_camel_case(attname) self.attname = attname self.object_type = cls self.mount(cls) @@ -134,8 +131,6 @@ class InputField(NamedType, OrderedType): assert issubclass( cls, (InputObjectType)), 'InputField {} cannot be mounted in {}'.format( self, cls) - if not self.name: - self.name = to_camel_case(attname) self.attname = attname self.object_type = cls self.mount(cls) diff --git a/graphene/core/types/tests/test_argument.py b/graphene/core/types/tests/test_argument.py index 26bbb310..cf07a7d3 100644 --- a/graphene/core/types/tests/test_argument.py +++ b/graphene/core/types/tests/test_argument.py @@ -27,8 +27,8 @@ def test_to_arguments(): other_kwarg=String(), ) - assert [a.name for a in arguments] == [ - 'myArg', 'otherArg', 'myKwarg', 'otherKwarg'] + assert [a.name or a.attname for a in arguments] == [ + 'myArg', 'otherArg', 'my_kwarg', 'other_kwarg'] def test_to_arguments_no_name(): diff --git a/graphene/core/types/tests/test_field.py b/graphene/core/types/tests/test_field.py index bb0bcf2c..7bb23ca9 100644 --- a/graphene/core/types/tests/test_field.py +++ b/graphene/core/types/tests/test_field.py @@ -20,7 +20,7 @@ def test_field_internal_type(): schema = Schema(query=Query) type = schema.T(field) - assert field.name == 'myField' + assert field.name is None assert field.attname == 'my_field' assert isinstance(type, GraphQLField) assert type.description == 'My argument' @@ -116,7 +116,7 @@ def test_inputfield_internal_type(): schema = Schema(query=MyObjectType) type = schema.T(field) - assert field.name == 'myField' + assert field.name is None assert field.attname == 'my_field' assert isinstance(type, GraphQLInputObjectField) assert type.description == 'My input field'