Skip to content

Commit 473f97c

Browse files
committed
Improved messaging for Argument transformation. Fixed #364
1 parent e26bbdd commit 473f97c

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

graphene/types/argument.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ def __eq__(self, other):
2828
)
2929

3030

31-
def to_arguments(args, extra_args):
31+
def to_arguments(args, extra_args=None):
3232
from .unmountedtype import UnmountedType
33-
extra_args = sorted(extra_args.items(), key=lambda f: f[1])
33+
from .field import Field
34+
from .inputfield import InputField
35+
if extra_args:
36+
extra_args = sorted(extra_args.items(), key=lambda f: f[1])
37+
else:
38+
extra_args = []
3439
iter_arguments = chain(args.items(), extra_args)
3540
arguments = OrderedDict()
3641
for default_name, arg in iter_arguments:
@@ -44,6 +49,13 @@ def to_arguments(args, extra_args):
4449
if isinstance(arg, UnmountedType):
4550
arg = arg.Argument()
4651

52+
if isinstance(arg, (InputField, Field)):
53+
raise ValueError('Expected {} to be Argument, but received {}. Try using Argument({}).'.format(
54+
default_name,
55+
type(arg).__name__,
56+
arg.type
57+
))
58+
4759
if not isinstance(arg, Argument):
4860
raise ValueError('Unknown argument "{}".'.format(default_name))
4961

graphene/types/tests/test_argument.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22

3-
from ..argument import Argument
3+
from ..argument import Argument, to_arguments
4+
from ..field import Field
5+
from ..inputfield import InputField
46
from ..structures import NonNull
57
from ..scalars import String
68

@@ -24,3 +26,38 @@ def test_argument_comparasion():
2426
def test_argument_required():
2527
arg = Argument(String, required=True)
2628
assert arg.type == NonNull(String)
29+
30+
31+
def test_to_arguments():
32+
args = {
33+
'arg_string': Argument(String),
34+
'unmounted_arg': String(required=True)
35+
}
36+
37+
my_args = to_arguments(args)
38+
assert my_args == {
39+
'arg_string': Argument(String),
40+
'unmounted_arg': Argument(String, required=True)
41+
}
42+
43+
44+
def test_to_arguments_raises_if_field():
45+
args = {
46+
'arg_string': Field(String),
47+
}
48+
49+
with pytest.raises(ValueError) as exc_info:
50+
to_arguments(args)
51+
52+
assert str(exc_info.value) == 'Expected arg_string to be Argument, but received Field. Try using Argument(String).'
53+
54+
55+
def test_to_arguments_raises_if_inputfield():
56+
args = {
57+
'arg_string': InputField(String),
58+
}
59+
60+
with pytest.raises(ValueError) as exc_info:
61+
to_arguments(args)
62+
63+
assert str(exc_info.value) == 'Expected arg_string to be Argument, but received InputField. Try using Argument(String).'

0 commit comments

Comments
 (0)