Skip to content

Commit fae376c

Browse files
committed
Moved arguments to a named group
1 parent afdddaf commit fae376c

File tree

7 files changed

+44
-29
lines changed

7 files changed

+44
-29
lines changed

graphene/core/classtypes/tests/test_mutation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ class Input:
2424
assert list(object_type.get_fields().keys()) == ['name']
2525
assert MyMutation._meta.fields_map['name'].object_type == MyMutation
2626
assert isinstance(MyMutation.arguments, ArgumentsGroup)
27-
assert 'argName' in MyMutation.arguments
27+
assert 'argName' in schema.T(MyMutation.arguments)

graphene/core/schema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ def __init__(self, query=None, mutation=None, subscription=None,
3838
def __repr__(self):
3939
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
4040

41+
def get_internal_type(self, objecttype):
42+
return objecttype.internal_type(self)
43+
4144
def T(self, object_type):
4245
if not object_type:
4346
return
4447
if inspect.isclass(object_type) and issubclass(
4548
object_type, (BaseType, ClassType)) or isinstance(
4649
object_type, BaseType):
4750
if object_type not in self._types:
48-
internal_type = object_type.internal_type(self)
51+
internal_type = self.get_internal_type(object_type)
4952
self._types[object_type] = internal_type
5053
is_objecttype = inspect.isclass(
5154
object_type) and issubclass(object_type, ClassType)

graphene/core/types/argument.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from graphql.core.type import GraphQLArgument
66

77
from ...utils import ProxySnakeDict, to_camel_case
8-
from .base import ArgumentType, BaseType, OrderedType
8+
from .base import ArgumentType, GroupNamedType, NamedType, OrderedType
99

1010

11-
class Argument(OrderedType):
11+
class Argument(NamedType, OrderedType):
1212

1313
def __init__(self, type, description=None, default=None,
1414
name=None, _creation_counter=None):
@@ -27,27 +27,11 @@ def __repr__(self):
2727
return self.name
2828

2929

30-
class ArgumentsGroup(BaseType):
30+
class ArgumentsGroup(GroupNamedType):
3131

3232
def __init__(self, *args, **kwargs):
3333
arguments = to_arguments(*args, **kwargs)
34-
self.arguments = OrderedDict([(arg.name, arg) for arg in arguments])
35-
36-
def internal_type(self, schema):
37-
return OrderedDict([(arg.name, schema.T(arg))
38-
for arg in self.arguments.values()])
39-
40-
def __len__(self):
41-
return len(self.arguments)
42-
43-
def __iter__(self):
44-
return iter(self.arguments)
45-
46-
def __contains__(self, *args):
47-
return self.arguments.__contains__(*args)
48-
49-
def __getitem__(self, *args):
50-
return self.arguments.__getitem__(*args)
34+
super(ArgumentsGroup, self).__init__(*arguments)
5135

5236

5337
def to_arguments(*args, **kwargs):

graphene/core/types/base.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from functools import total_ordering
1+
from collections import OrderedDict
2+
from functools import total_ordering, partial
23

34
import six
45

@@ -126,3 +127,30 @@ def as_inputfield(self):
126127

127128
class MountedType(FieldType, ArgumentType):
128129
pass
130+
131+
132+
class NamedType(BaseType):
133+
pass
134+
135+
136+
class GroupNamedType(BaseType):
137+
def __init__(self, *types):
138+
self.types = types
139+
140+
def get_named_type(self, schema, type):
141+
return type.name or type.attname, schema.T(type)
142+
143+
def internal_type(self, schema):
144+
return OrderedDict(map(partial(self.get_named_type, schema), self.types))
145+
146+
def __len__(self):
147+
return len(self.types)
148+
149+
def __iter__(self):
150+
return iter(self.types)
151+
152+
def __contains__(self, *args):
153+
return self.types.__contains__(*args)
154+
155+
def __getitem__(self, *args):
156+
return self.types.__getitem__(*args)

graphene/core/types/field.py

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

1515

16-
class Field(OrderedType):
16+
class Field(NamedType, OrderedType):
1717

1818
def __init__(
1919
self, type, description=None, args=None, name=None, resolver=None,
@@ -117,7 +117,7 @@ def __hash__(self):
117117
return hash((self.creation_counter, self.object_type))
118118

119119

120-
class InputField(OrderedType):
120+
class InputField(NamedType, OrderedType):
121121

122122
def __init__(self, type, description=None, default=None,
123123
name=None, _creation_counter=None, required=False):

graphene/core/types/tests/test_field.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ class MyObjectType(ObjectType):
9898

9999
def test_field_custom_arguments():
100100
field = Field(None, name='my_customName', p=String())
101+
schema = Schema()
101102

102103
args = field.arguments
103-
assert 'p' in args
104+
assert 'p' in schema.T(args)
104105

105106

106107
def test_inputfield_internal_type():

graphene/relay/tests/test_mutations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class MyResultMutation(graphene.ObjectType):
3434

3535
def test_mutation_arguments():
3636
assert ChangeNumber.arguments
37-
assert list(ChangeNumber.arguments) == ['input']
38-
assert 'input' in ChangeNumber.arguments
37+
assert 'input' in schema.T(ChangeNumber.arguments)
3938
inner_type = ChangeNumber.input_type
4039
client_mutation_id_field = inner_type._meta.fields_map[
4140
'client_mutation_id']

0 commit comments

Comments
 (0)