mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Improved messaging for Argument transformation. Fixed #364
This commit is contained in:
parent
e26bbdd937
commit
473f97c7b8
|
@ -28,9 +28,14 @@ class Argument(OrderedType):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def to_arguments(args, extra_args):
|
def to_arguments(args, extra_args=None):
|
||||||
from .unmountedtype import UnmountedType
|
from .unmountedtype import UnmountedType
|
||||||
|
from .field import Field
|
||||||
|
from .inputfield import InputField
|
||||||
|
if extra_args:
|
||||||
extra_args = sorted(extra_args.items(), key=lambda f: f[1])
|
extra_args = sorted(extra_args.items(), key=lambda f: f[1])
|
||||||
|
else:
|
||||||
|
extra_args = []
|
||||||
iter_arguments = chain(args.items(), extra_args)
|
iter_arguments = chain(args.items(), extra_args)
|
||||||
arguments = OrderedDict()
|
arguments = OrderedDict()
|
||||||
for default_name, arg in iter_arguments:
|
for default_name, arg in iter_arguments:
|
||||||
|
@ -44,6 +49,13 @@ def to_arguments(args, extra_args):
|
||||||
if isinstance(arg, UnmountedType):
|
if isinstance(arg, UnmountedType):
|
||||||
arg = arg.Argument()
|
arg = arg.Argument()
|
||||||
|
|
||||||
|
if isinstance(arg, (InputField, Field)):
|
||||||
|
raise ValueError('Expected {} to be Argument, but received {}. Try using Argument({}).'.format(
|
||||||
|
default_name,
|
||||||
|
type(arg).__name__,
|
||||||
|
arg.type
|
||||||
|
))
|
||||||
|
|
||||||
if not isinstance(arg, Argument):
|
if not isinstance(arg, Argument):
|
||||||
raise ValueError('Unknown argument "{}".'.format(default_name))
|
raise ValueError('Unknown argument "{}".'.format(default_name))
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from ..argument import Argument
|
from ..argument import Argument, to_arguments
|
||||||
|
from ..field import Field
|
||||||
|
from ..inputfield import InputField
|
||||||
from ..structures import NonNull
|
from ..structures import NonNull
|
||||||
from ..scalars import String
|
from ..scalars import String
|
||||||
|
|
||||||
|
@ -24,3 +26,38 @@ def test_argument_comparasion():
|
||||||
def test_argument_required():
|
def test_argument_required():
|
||||||
arg = Argument(String, required=True)
|
arg = Argument(String, required=True)
|
||||||
assert arg.type == NonNull(String)
|
assert arg.type == NonNull(String)
|
||||||
|
|
||||||
|
|
||||||
|
def test_to_arguments():
|
||||||
|
args = {
|
||||||
|
'arg_string': Argument(String),
|
||||||
|
'unmounted_arg': String(required=True)
|
||||||
|
}
|
||||||
|
|
||||||
|
my_args = to_arguments(args)
|
||||||
|
assert my_args == {
|
||||||
|
'arg_string': Argument(String),
|
||||||
|
'unmounted_arg': Argument(String, required=True)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_to_arguments_raises_if_field():
|
||||||
|
args = {
|
||||||
|
'arg_string': Field(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
with pytest.raises(ValueError) as exc_info:
|
||||||
|
to_arguments(args)
|
||||||
|
|
||||||
|
assert str(exc_info.value) == 'Expected arg_string to be Argument, but received Field. Try using Argument(String).'
|
||||||
|
|
||||||
|
|
||||||
|
def test_to_arguments_raises_if_inputfield():
|
||||||
|
args = {
|
||||||
|
'arg_string': InputField(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
with pytest.raises(ValueError) as exc_info:
|
||||||
|
to_arguments(args)
|
||||||
|
|
||||||
|
assert str(exc_info.value) == 'Expected arg_string to be Argument, but received InputField. Try using Argument(String).'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user