mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 19:43:56 +03:00
Improved plugin structure based on @adamcharnock suggestions
This commit is contained in:
parent
730de3f289
commit
cd5d9b8eea
|
@ -50,10 +50,12 @@ class Schema(object):
|
|||
plugin.contribute_to_schema(self)
|
||||
self.plugins.append(plugin)
|
||||
|
||||
def get_internal_type(self, objecttype):
|
||||
def get_default_namedtype_name(self, value):
|
||||
for plugin in self.plugins:
|
||||
objecttype = plugin.transform_type(objecttype)
|
||||
return objecttype.internal_type(self)
|
||||
if not hasattr(plugin, 'get_default_namedtype_name'):
|
||||
continue
|
||||
value = plugin.get_default_namedtype_name(value)
|
||||
return value
|
||||
|
||||
def T(self, _type):
|
||||
if not _type:
|
||||
|
@ -62,7 +64,7 @@ class Schema(object):
|
|||
is_instancetype = isinstance(_type, InstanceType)
|
||||
if is_classtype or is_instancetype:
|
||||
if _type not in self._types:
|
||||
internal_type = self.get_internal_type(_type)
|
||||
internal_type = _type.internal_type(self)
|
||||
self._types[_type] = internal_type
|
||||
if is_classtype:
|
||||
self.register(_type)
|
||||
|
|
|
@ -11,9 +11,7 @@ class Argument(NamedType, OrderedType):
|
|||
|
||||
def __init__(self, type, description=None, default=None,
|
||||
name=None, _creation_counter=None):
|
||||
super(Argument, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
self.attname = None
|
||||
super(Argument, self).__init__(name=name, _creation_counter=_creation_counter)
|
||||
self.type = type
|
||||
self.description = description
|
||||
self.default = default
|
||||
|
@ -38,18 +36,18 @@ def to_arguments(*args, **kwargs):
|
|||
arguments = {}
|
||||
iter_arguments = chain(kwargs.items(), [(None, a) for a in args])
|
||||
|
||||
for attname, arg in iter_arguments:
|
||||
for default_name, 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' % (attname, arg))
|
||||
raise ValueError('Unknown argument %s=%r' % (default_name, arg))
|
||||
|
||||
if attname:
|
||||
argument.attname = attname
|
||||
if default_name:
|
||||
argument.default_name = default_name
|
||||
|
||||
name = argument.name or argument.attname
|
||||
name = argument.name or argument.default_name
|
||||
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
|
||||
|
|
|
@ -129,7 +129,10 @@ class MountedType(FieldType, ArgumentType):
|
|||
|
||||
|
||||
class NamedType(InstanceType):
|
||||
pass
|
||||
def __init__(self, name=None, default_name=None, *args, **kwargs):
|
||||
self.name = name
|
||||
self.default_name = None
|
||||
super(NamedType, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class GroupNamedType(InstanceType):
|
||||
|
@ -138,7 +141,7 @@ class GroupNamedType(InstanceType):
|
|||
self.types = types
|
||||
|
||||
def get_named_type(self, schema, type):
|
||||
name = type.name or type.attname
|
||||
name = type.name or schema.get_default_namedtype_name(type.default_name)
|
||||
return name, schema.T(type)
|
||||
|
||||
def iter_types(self, schema):
|
||||
|
|
|
@ -19,8 +19,7 @@ class Field(NamedType, OrderedType):
|
|||
self, type, description=None, args=None, name=None, resolver=None,
|
||||
required=False, default=None, *args_list, **kwargs):
|
||||
_creation_counter = kwargs.pop('_creation_counter', None)
|
||||
super(Field, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
super(Field, self).__init__(name=name, _creation_counter=_creation_counter)
|
||||
if isinstance(type, six.string_types):
|
||||
type = LazyType(type)
|
||||
self.required = required
|
||||
|
@ -37,6 +36,7 @@ class Field(NamedType, OrderedType):
|
|||
cls, (FieldsClassType)), 'Field {} cannot be mounted in {}'.format(
|
||||
self, cls)
|
||||
self.attname = attname
|
||||
self.default_name = attname
|
||||
self.object_type = cls
|
||||
self.mount(cls)
|
||||
if isinstance(self.type, MountType):
|
||||
|
@ -120,7 +120,6 @@ class InputField(NamedType, OrderedType):
|
|||
def __init__(self, type, description=None, default=None,
|
||||
name=None, _creation_counter=None, required=False):
|
||||
super(InputField, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
if required:
|
||||
type = NonNull(type)
|
||||
self.type = type
|
||||
|
@ -132,6 +131,7 @@ class InputField(NamedType, OrderedType):
|
|||
cls, (InputObjectType)), 'InputField {} cannot be mounted in {}'.format(
|
||||
self, cls)
|
||||
self.attname = attname
|
||||
self.default_name = attname
|
||||
self.object_type = cls
|
||||
self.mount(cls)
|
||||
if isinstance(self.type, MountType):
|
||||
|
|
|
@ -27,7 +27,7 @@ def test_to_arguments():
|
|||
other_kwarg=String(),
|
||||
)
|
||||
|
||||
assert [a.name or a.attname for a in arguments] == [
|
||||
assert [a.name or a.default_name for a in arguments] == [
|
||||
'myArg', 'otherArg', 'my_kwarg', 'other_kwarg']
|
||||
|
||||
|
||||
|
|
|
@ -1,22 +1,8 @@
|
|||
from ..core.types.base import GroupNamedType
|
||||
from ..utils import memoize, to_camel_case
|
||||
from ..utils import to_camel_case
|
||||
from .base import Plugin
|
||||
|
||||
|
||||
def camelcase_named_type(schema, type):
|
||||
name = type.name or to_camel_case(type.attname)
|
||||
return name, schema.T(type)
|
||||
|
||||
|
||||
class CamelCase(Plugin):
|
||||
|
||||
@memoize
|
||||
def transform_group(self, _type):
|
||||
new_type = _type.__class__(*_type.types)
|
||||
setattr(new_type, 'get_named_type', camelcase_named_type)
|
||||
return new_type
|
||||
|
||||
def transform_type(self, _type):
|
||||
if isinstance(_type, GroupNamedType):
|
||||
return self.transform_group(_type)
|
||||
return _type
|
||||
def get_default_namedtype_name(self, value):
|
||||
return to_camel_case(value)
|
||||
|
|
Loading…
Reference in New Issue
Block a user