mirror of
https://github.com/graphql-python/graphene.git
synced 2025-05-01 15:03:40 +03:00
Moved arguments to a named group
This commit is contained in:
parent
afdddafb3d
commit
fae376cbb0
|
@ -24,4 +24,4 @@ def test_mutation():
|
||||||
assert list(object_type.get_fields().keys()) == ['name']
|
assert list(object_type.get_fields().keys()) == ['name']
|
||||||
assert MyMutation._meta.fields_map['name'].object_type == MyMutation
|
assert MyMutation._meta.fields_map['name'].object_type == MyMutation
|
||||||
assert isinstance(MyMutation.arguments, ArgumentsGroup)
|
assert isinstance(MyMutation.arguments, ArgumentsGroup)
|
||||||
assert 'argName' in MyMutation.arguments
|
assert 'argName' in schema.T(MyMutation.arguments)
|
||||||
|
|
|
@ -38,6 +38,9 @@ class Schema(object):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
|
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
|
||||||
|
|
||||||
|
def get_internal_type(self, objecttype):
|
||||||
|
return objecttype.internal_type(self)
|
||||||
|
|
||||||
def T(self, object_type):
|
def T(self, object_type):
|
||||||
if not object_type:
|
if not object_type:
|
||||||
return
|
return
|
||||||
|
@ -45,7 +48,7 @@ class Schema(object):
|
||||||
object_type, (BaseType, ClassType)) or isinstance(
|
object_type, (BaseType, ClassType)) or isinstance(
|
||||||
object_type, BaseType):
|
object_type, BaseType):
|
||||||
if object_type not in self._types:
|
if object_type not in self._types:
|
||||||
internal_type = object_type.internal_type(self)
|
internal_type = self.get_internal_type(object_type)
|
||||||
self._types[object_type] = internal_type
|
self._types[object_type] = internal_type
|
||||||
is_objecttype = inspect.isclass(
|
is_objecttype = inspect.isclass(
|
||||||
object_type) and issubclass(object_type, ClassType)
|
object_type) and issubclass(object_type, ClassType)
|
||||||
|
|
|
@ -5,10 +5,10 @@ from itertools import chain
|
||||||
from graphql.core.type import GraphQLArgument
|
from graphql.core.type import GraphQLArgument
|
||||||
|
|
||||||
from ...utils import ProxySnakeDict, to_camel_case
|
from ...utils import ProxySnakeDict, to_camel_case
|
||||||
from .base import ArgumentType, BaseType, OrderedType
|
from .base import ArgumentType, GroupNamedType, NamedType, OrderedType
|
||||||
|
|
||||||
|
|
||||||
class Argument(OrderedType):
|
class Argument(NamedType, OrderedType):
|
||||||
|
|
||||||
def __init__(self, type, description=None, default=None,
|
def __init__(self, type, description=None, default=None,
|
||||||
name=None, _creation_counter=None):
|
name=None, _creation_counter=None):
|
||||||
|
@ -27,27 +27,11 @@ class Argument(OrderedType):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class ArgumentsGroup(BaseType):
|
class ArgumentsGroup(GroupNamedType):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
arguments = to_arguments(*args, **kwargs)
|
arguments = to_arguments(*args, **kwargs)
|
||||||
self.arguments = OrderedDict([(arg.name, arg) for arg in arguments])
|
super(ArgumentsGroup, self).__init__(*arguments)
|
||||||
|
|
||||||
def internal_type(self, schema):
|
|
||||||
return OrderedDict([(arg.name, schema.T(arg))
|
|
||||||
for arg in self.arguments.values()])
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.arguments)
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return iter(self.arguments)
|
|
||||||
|
|
||||||
def __contains__(self, *args):
|
|
||||||
return self.arguments.__contains__(*args)
|
|
||||||
|
|
||||||
def __getitem__(self, *args):
|
|
||||||
return self.arguments.__getitem__(*args)
|
|
||||||
|
|
||||||
|
|
||||||
def to_arguments(*args, **kwargs):
|
def to_arguments(*args, **kwargs):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from functools import total_ordering
|
from collections import OrderedDict
|
||||||
|
from functools import total_ordering, partial
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
@ -126,3 +127,30 @@ class FieldType(MirroredType):
|
||||||
|
|
||||||
class MountedType(FieldType, ArgumentType):
|
class MountedType(FieldType, ArgumentType):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NamedType(BaseType):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class GroupNamedType(BaseType):
|
||||||
|
def __init__(self, *types):
|
||||||
|
self.types = types
|
||||||
|
|
||||||
|
def get_named_type(self, schema, type):
|
||||||
|
return type.name or type.attname, schema.T(type)
|
||||||
|
|
||||||
|
def internal_type(self, schema):
|
||||||
|
return OrderedDict(map(partial(self.get_named_type, schema), self.types))
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.types)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self.types)
|
||||||
|
|
||||||
|
def __contains__(self, *args):
|
||||||
|
return self.types.__contains__(*args)
|
||||||
|
|
||||||
|
def __getitem__(self, *args):
|
||||||
|
return self.types.__getitem__(*args)
|
||||||
|
|
|
@ -9,11 +9,11 @@ from ..classtypes.base import FieldsClassType
|
||||||
from ..classtypes.inputobjecttype import InputObjectType
|
from ..classtypes.inputobjecttype import InputObjectType
|
||||||
from ..classtypes.mutation import Mutation
|
from ..classtypes.mutation import Mutation
|
||||||
from .argument import ArgumentsGroup, snake_case_args
|
from .argument import ArgumentsGroup, snake_case_args
|
||||||
from .base import LazyType, MountType, OrderedType
|
from .base import LazyType, NamedType, MountType, OrderedType
|
||||||
from .definitions import NonNull
|
from .definitions import NonNull
|
||||||
|
|
||||||
|
|
||||||
class Field(OrderedType):
|
class Field(NamedType, OrderedType):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, type, description=None, args=None, name=None, resolver=None,
|
self, type, description=None, args=None, name=None, resolver=None,
|
||||||
|
@ -117,7 +117,7 @@ class Field(OrderedType):
|
||||||
return hash((self.creation_counter, self.object_type))
|
return hash((self.creation_counter, self.object_type))
|
||||||
|
|
||||||
|
|
||||||
class InputField(OrderedType):
|
class InputField(NamedType, OrderedType):
|
||||||
|
|
||||||
def __init__(self, type, description=None, default=None,
|
def __init__(self, type, description=None, default=None,
|
||||||
name=None, _creation_counter=None, required=False):
|
name=None, _creation_counter=None, required=False):
|
||||||
|
|
|
@ -98,9 +98,10 @@ def test_field_string_reference():
|
||||||
|
|
||||||
def test_field_custom_arguments():
|
def test_field_custom_arguments():
|
||||||
field = Field(None, name='my_customName', p=String())
|
field = Field(None, name='my_customName', p=String())
|
||||||
|
schema = Schema()
|
||||||
|
|
||||||
args = field.arguments
|
args = field.arguments
|
||||||
assert 'p' in args
|
assert 'p' in schema.T(args)
|
||||||
|
|
||||||
|
|
||||||
def test_inputfield_internal_type():
|
def test_inputfield_internal_type():
|
||||||
|
|
|
@ -34,8 +34,7 @@ schema = Schema(query=Query, mutation=MyResultMutation)
|
||||||
|
|
||||||
def test_mutation_arguments():
|
def test_mutation_arguments():
|
||||||
assert ChangeNumber.arguments
|
assert ChangeNumber.arguments
|
||||||
assert list(ChangeNumber.arguments) == ['input']
|
assert 'input' in schema.T(ChangeNumber.arguments)
|
||||||
assert 'input' in ChangeNumber.arguments
|
|
||||||
inner_type = ChangeNumber.input_type
|
inner_type = ChangeNumber.input_type
|
||||||
client_mutation_id_field = inner_type._meta.fields_map[
|
client_mutation_id_field = inner_type._meta.fields_map[
|
||||||
'client_mutation_id']
|
'client_mutation_id']
|
||||||
|
|
Loading…
Reference in New Issue
Block a user