Skip to content

Commit bc3d176

Browse files
committed
Improved Mutations Input args
1 parent 2648a23 commit bc3d176

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

graphene/core/fields.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def internal_field(self, schema):
124124
','.join(extra_args.keys())
125125
))
126126

127+
args = self.args
128+
129+
object_type = self.get_object_type(schema)
130+
if object_type and object_type._meta.mutation:
131+
assert not self.args, 'Arguments provided for mutations are defined in Input class in Mutation'
132+
args = object_type.input_type.fields_as_arguments(schema)
133+
127134
internal_type = self.internal_type(schema)
128135
if not internal_type:
129136
raise Exception("Internal type for field %s is None" % self)
@@ -141,7 +148,7 @@ def resolver(*args):
141148
return GraphQLField(
142149
internal_type,
143150
description=description,
144-
args=self.args,
151+
args=args,
145152
resolver=resolver,
146153
)
147154

graphene/core/types.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from graphql.core.type import (
77
GraphQLObjectType,
8-
GraphQLInterfaceType
8+
GraphQLInterfaceType,
9+
GraphQLArgument
910
)
1011

1112
from graphene import signals
@@ -58,6 +59,10 @@ def __new__(cls, name, bases, attrs):
5859

5960
if new_class._meta.mutation:
6061
assert hasattr(new_class, 'mutate'), "All mutations must implement mutate method"
62+
Input = getattr(new_class, 'Input', None)
63+
if Input:
64+
input_type = type('{}Input'.format(new_class._meta.type_name), (Input, ObjectType), Input.__dict__)
65+
setattr(new_class, 'input_type', input_type)
6166

6267
new_class.add_extra_fields()
6368

@@ -141,6 +146,11 @@ def __getattr__(self, name):
141146
if self.instance:
142147
return getattr(self.instance, name)
143148

149+
@classmethod
150+
def fields_as_arguments(cls, schema):
151+
return OrderedDict([(f.field_name, GraphQLArgument(f.internal_type(schema)))
152+
for f in cls._meta.fields])
153+
144154
@classmethod
145155
def resolve_objecttype(cls, schema, instance, *_):
146156
return instance

tests/core/test_mutations.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class Query(graphene.ObjectType):
1212
class ChangeNumber(graphene.Mutation):
1313
'''Result mutation'''
1414
class Input:
15-
id = graphene.IntField(required=True)
15+
to = graphene.IntField()
1616

1717
result = graphene.StringField()
1818

1919
@classmethod
2020
def mutate(cls, instance, args, info):
2121
global my_id
22-
my_id = my_id + 1
22+
my_id = args.get('to', my_id + 1)
2323
return ChangeNumber(result=my_id)
2424

2525

@@ -30,7 +30,13 @@ class MyResultMutation(graphene.ObjectType):
3030
schema = Schema(query=Query, mutation=MyResultMutation)
3131

3232

33-
def test_mutate():
33+
def test_mutation_input():
34+
assert ChangeNumber.input_type
35+
assert ChangeNumber.input_type._meta.type_name == 'ChangeNumberInput'
36+
assert list(ChangeNumber.input_type._meta.fields_map.keys()) == ['to']
37+
38+
39+
def test_execute_mutations():
3440
query = '''
3541
mutation M{
3642
first: changeNumber {
@@ -39,6 +45,9 @@ def test_mutate():
3945
second: changeNumber {
4046
result
4147
}
48+
third: changeNumber(to: 5) {
49+
result
50+
}
4251
}
4352
'''
4453
expected = {
@@ -47,6 +56,9 @@ def test_mutate():
4756
},
4857
'second': {
4958
'result': '2',
59+
},
60+
'third': {
61+
'result': '5',
5062
}
5163
}
5264
result = schema.execute(query, root=object())

0 commit comments

Comments
 (0)