1
- from collections import OrderedDict
2
1
from functools import wraps
3
2
from itertools import chain
4
3
5
4
from graphql .core .type import GraphQLArgument
6
5
7
- from ...utils import ProxySnakeDict , to_camel_case
8
- from .base import ArgumentType , BaseType , OrderedType
6
+ from ...utils import ProxySnakeDict
7
+ from .base import ArgumentType , GroupNamedType , NamedType , OrderedType
9
8
10
9
11
- class Argument (OrderedType ):
10
+ class Argument (NamedType , OrderedType ):
12
11
13
12
def __init__ (self , type , description = None , default = None ,
14
13
name = None , _creation_counter = None ):
15
14
super (Argument , self ).__init__ (_creation_counter = _creation_counter )
16
15
self .name = name
16
+ self .attname = None
17
17
self .type = type
18
18
self .description = description
19
19
self .default = default
@@ -27,47 +27,32 @@ def __repr__(self):
27
27
return self .name
28
28
29
29
30
- class ArgumentsGroup (BaseType ):
30
+ class ArgumentsGroup (GroupNamedType ):
31
31
32
32
def __init__ (self , * args , ** kwargs ):
33
33
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 )
51
35
52
36
53
37
def to_arguments (* args , ** kwargs ):
54
38
arguments = {}
55
39
iter_arguments = chain (kwargs .items (), [(None , a ) for a in args ])
56
40
57
- for name , arg in iter_arguments :
41
+ for attname , arg in iter_arguments :
58
42
if isinstance (arg , Argument ):
59
43
argument = arg
60
44
elif isinstance (arg , ArgumentType ):
61
45
argument = arg .as_argument ()
62
46
else :
63
- raise ValueError ('Unknown argument %s=%r' % (name , arg ))
64
-
65
- if name :
66
- argument .name = to_camel_case (name )
67
- assert argument .name , 'Argument in field must have a name'
68
- assert argument .name not in arguments , 'Found more than one Argument with same name {}' .format (
69
- argument .name )
70
- arguments [argument .name ] = argument
47
+ raise ValueError ('Unknown argument %s=%r' % (attname , arg ))
48
+
49
+ if attname :
50
+ argument .attname = attname
51
+
52
+ name = argument .name or argument .attname
53
+ assert name , 'Argument in field must have a name'
54
+ assert name not in arguments , 'Found more than one Argument with same name {}' .format (name )
55
+ arguments [name ] = argument
71
56
72
57
return sorted (arguments .values ())
73
58
0 commit comments