1
- from ..language import ast
1
+ import six
2
+
3
+ from ..language import ast , visitor_meta
2
4
from ..type .definition import (
3
5
GraphQLInputObjectType ,
4
6
GraphQLList ,
@@ -16,6 +18,8 @@ def pop(lst):
16
18
lst .pop ()
17
19
18
20
21
+ # noinspection PyPep8Naming
22
+ @six .add_metaclass (visitor_meta .VisitorMeta )
19
23
class TypeInfo (object ):
20
24
def __init__ (self , schema ):
21
25
self ._schema = schema
@@ -48,80 +52,105 @@ def get_directive(self):
48
52
def get_argument (self ):
49
53
return self ._argument
50
54
55
+ def leave (self , node ):
56
+ method = self ._get_leave_handler (type (node ))
57
+ if method :
58
+ return method (self )
59
+
51
60
def enter (self , node ):
52
- schema = self ._schema
53
- type = None
54
- if isinstance (node , ast .SelectionSet ):
55
- named_type = get_named_type (self .get_type ())
56
- composite_type = None
57
- if is_composite_type (named_type ):
58
- composite_type = named_type
59
- self ._parent_type_stack .append (composite_type )
60
- elif isinstance (node , ast .Field ):
61
- parent_type = self .get_parent_type ()
62
- field_def = None
63
- if parent_type :
64
- field_def = get_field_def (schema , parent_type , node )
65
- self ._field_def_stack .append (field_def )
66
- self ._type_stack .append (field_def and field_def .type )
67
- elif isinstance (node , ast .Directive ):
68
- self ._directive = schema .get_directive (node .name .value )
69
- elif isinstance (node , ast .OperationDefinition ):
70
- if node .operation == 'query' :
71
- type = schema .get_query_type ()
72
- elif node .operation == 'mutation' :
73
- type = schema .get_mutation_type ()
74
- self ._type_stack .append (type )
75
- elif isinstance (node , (ast .InlineFragment , ast .FragmentDefinition )):
76
- type_condition_ast = node .type_condition
77
- type = type_from_ast (schema , type_condition_ast ) if type_condition_ast else self .get_type ()
78
- self ._type_stack .append (type )
79
- elif isinstance (node , ast .VariableDefinition ):
80
- self ._input_type_stack .append (type_from_ast (schema , node .type ))
81
- elif isinstance (node , ast .Argument ):
82
- arg_def = None
83
- arg_type = None
84
- field_or_directive = self .get_directive () or self .get_field_def ()
85
- if field_or_directive :
86
- arg_def = [arg for arg in field_or_directive .args if arg .name == node .name .value ]
87
- if arg_def :
88
- arg_def = arg_def [0 ]
89
- arg_type = arg_def .type
90
- else :
91
- arg_def = None
92
- self ._argument = arg_def
93
- self ._input_type_stack .append (arg_type )
94
- elif isinstance (node , ast .ListValue ):
95
- list_type = get_nullable_type (self .get_input_type ())
96
- self ._input_type_stack .append (
97
- list_type .of_type if isinstance (list_type , GraphQLList ) else None
98
- )
99
- elif isinstance (node , ast .ObjectField ):
100
- object_type = get_named_type (self .get_input_type ())
101
- field_type = None
102
- if isinstance (object_type , GraphQLInputObjectType ):
103
- input_field = object_type .get_fields ().get (node .name .value )
104
- field_type = input_field .type if input_field else None
105
- self ._input_type_stack .append (field_type )
61
+ method = self ._get_enter_handler (type (node ))
62
+ if method :
63
+ return method (self , node )
106
64
107
- def leave (self , node ):
108
- if isinstance (node , ast .SelectionSet ):
109
- pop (self ._parent_type_stack )
110
- elif isinstance (node , ast .Field ):
111
- pop (self ._field_def_stack )
112
- pop (self ._type_stack )
113
- elif isinstance (node , ast .Directive ):
114
- self ._directive = None
115
- elif isinstance (node , (
116
- ast .OperationDefinition ,
117
- ast .InlineFragment ,
118
- ast .FragmentDefinition ,
119
- )):
120
- pop (self ._type_stack )
121
- elif isinstance (node , ast .VariableDefinition ):
122
- pop (self ._input_type_stack )
123
- elif isinstance (node , ast .Argument ):
124
- self ._argument = None
125
- pop (self ._input_type_stack )
126
- elif isinstance (node , (ast .ListType , ast .ObjectField )):
127
- pop (self ._input_type_stack )
65
+ def enter_SelectionSet (self , node ):
66
+ named_type = get_named_type (self .get_type ())
67
+ composite_type = None
68
+ if is_composite_type (named_type ):
69
+ composite_type = named_type
70
+ self ._parent_type_stack .append (composite_type )
71
+
72
+ def enter_Field (self , node ):
73
+ parent_type = self .get_parent_type ()
74
+ field_def = None
75
+ if parent_type :
76
+ field_def = get_field_def (self ._schema , parent_type , node )
77
+ self ._field_def_stack .append (field_def )
78
+ self ._type_stack .append (field_def and field_def .type )
79
+
80
+ def enter_Directive (self , node ):
81
+ self ._directive = self ._schema .get_directive (node .name .value )
82
+
83
+ def enter_OperationDefinition (self , node ):
84
+ definition_type = None
85
+ if node .operation == 'query' :
86
+ definition_type = self ._schema .get_query_type ()
87
+ elif node .operation == 'mutation' :
88
+ definition_type = self ._schema .get_mutation_type ()
89
+
90
+ self ._type_stack .append (definition_type )
91
+
92
+ def enter_InlineFragment (self , node ):
93
+ type_condition_ast = node .type_condition
94
+ type = type_from_ast (self ._schema , type_condition_ast ) if type_condition_ast else self .get_type ()
95
+ self ._type_stack .append (type )
96
+
97
+ enter_FragmentDefinition = enter_InlineFragment
98
+
99
+ def enter_VariableDefinition (self , node ):
100
+ self ._input_type_stack .append (type_from_ast (self ._schema , node .type ))
101
+
102
+ def enter_Argument (self , node ):
103
+ arg_def = None
104
+ arg_type = None
105
+ field_or_directive = self .get_directive () or self .get_field_def ()
106
+ if field_or_directive :
107
+ arg_def = [arg for arg in field_or_directive .args if arg .name == node .name .value ]
108
+ if arg_def :
109
+ arg_def = arg_def [0 ]
110
+ arg_type = arg_def .type
111
+ else :
112
+ arg_def = None
113
+ self ._argument = arg_def
114
+ self ._input_type_stack .append (arg_type )
115
+
116
+ def enter_ListValue (self , node ):
117
+ list_type = get_nullable_type (self .get_input_type ())
118
+ self ._input_type_stack .append (
119
+ list_type .of_type if isinstance (list_type , GraphQLList ) else None
120
+ )
121
+
122
+ def enter_ObjectField (self , node ):
123
+ object_type = get_named_type (self .get_input_type ())
124
+ field_type = None
125
+ if isinstance (object_type , GraphQLInputObjectType ):
126
+ input_field = object_type .get_fields ().get (node .name .value )
127
+ field_type = input_field .type if input_field else None
128
+ self ._input_type_stack .append (field_type )
129
+
130
+ def leave_SelectionSet (self ):
131
+ pop (self ._parent_type_stack )
132
+
133
+ def leave_Field (self ):
134
+ pop (self ._field_def_stack )
135
+ pop (self ._type_stack )
136
+
137
+ def leave_Directive (self ):
138
+ self ._directive = None
139
+
140
+ def leave_OperationDefinition (self ):
141
+ pop (self ._type_stack )
142
+
143
+ leave_InlineFragment = leave_OperationDefinition
144
+ leave_FragmentDefinition = leave_OperationDefinition
145
+
146
+ def leave_VariableDefinition (self ):
147
+ pop (self ._input_type_stack )
148
+
149
+ def leave_Argument (self ):
150
+ self ._argument = None
151
+ pop (self ._input_type_stack )
152
+
153
+ def leave_ListType (self ):
154
+ pop (self ._input_type_stack )
155
+
156
+ leave_ObjectField = leave_ListType
0 commit comments