@@ -19,22 +19,18 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
19
19
20
20
defimpl Visitor do
21
21
22
- defmacrop stack_push ( stack_name , value ) do
23
- quote do
24
- old_type_info = var! ( accumulator ) [ :type_info ]
25
- new_type_info = % TypeInfo { old_type_info |
26
- unquote ( stack_name ) => Stack . push ( old_type_info . unquote ( stack_name ) , unquote ( value ) ) }
27
- put_in ( var! ( accumulator ) [ :type_info ] , new_type_info )
28
- end
22
+ def stack_push ( accumulator , stack_name , value ) do
23
+ old_type_info = accumulator [ :type_info ]
24
+ stack = Stack . push ( Map . get ( old_type_info , stack_name ) , value )
25
+ new_type_info = Map . merge ( old_type_info , % { stack_name => stack } )
26
+ put_in ( accumulator [ :type_info ] , new_type_info )
29
27
end
30
28
31
- defmacrop stack_pop ( stack_name ) do
32
- quote do
33
- old_type_info = var! ( accumulator ) [ :type_info ]
34
- new_type_info = % TypeInfo { old_type_info |
35
- unquote ( stack_name ) => Stack . pop ( old_type_info . unquote ( stack_name ) ) }
36
- put_in ( var! ( accumulator ) [ :type_info ] , new_type_info )
37
- end
29
+ def stack_pop ( accumulator , stack_name ) do
30
+ old_type_info = accumulator [ :type_info ]
31
+ stack = Stack . pop ( Map . get ( old_type_info , stack_name ) )
32
+ new_type_info = Map . merge ( old_type_info , % { stack_name => stack } )
33
+ put_in ( accumulator [ :type_info ] , new_type_info )
38
34
end
39
35
40
36
def set_directive ( accumulator , directive ) do
@@ -57,9 +53,9 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
57
53
type = TypeInfo . type ( accumulator [ :type_info ] )
58
54
named_type = TypeInfo . named_type ( type )
59
55
if Type . is_composite_type? ( named_type ) do
60
- stack_push ( :parent_type_stack , named_type )
56
+ stack_push ( accumulator , :parent_type_stack , named_type )
61
57
else
62
- stack_push ( :parent_type_stack , nil )
58
+ stack_push ( accumulator , :parent_type_stack , nil )
63
59
end
64
60
:Field ->
65
61
parent_type = TypeInfo . parent_type ( accumulator [ :type_info ] )
@@ -70,11 +66,11 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
70
66
node
71
67
)
72
68
field_def_type = if field_def , do: field_def . type , else: nil
73
- accumulator = stack_push ( :field_def_stack , field_def )
74
- stack_push ( :type_stack , field_def_type )
69
+ accumulator = stack_push ( accumulator , :field_def_stack , field_def )
70
+ stack_push ( accumulator , :type_stack , field_def_type )
75
71
else
76
- accumulator = stack_push ( :field_def_stack , nil )
77
- stack_push ( :type_stack , nil )
72
+ accumulator = stack_push ( accumulator , :field_def_stack , nil )
73
+ stack_push ( accumulator , :type_stack , nil )
78
74
end
79
75
:Directive ->
80
76
# add this once we add directive validations
@@ -92,17 +88,17 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
92
88
:mutation -> accumulator [ :type_info ] . schema . mutation
93
89
_ -> raise "node operation #{ node . operation } not handled"
94
90
end
95
- stack_push ( :type_stack , type )
91
+ stack_push ( accumulator , :type_stack , type )
96
92
kind when kind in [ :InlineFragment , :FragmentDefinition ] ->
97
93
output_type = if Map . has_key? ( node , :typeCondition ) do
98
94
Schema . type_from_ast ( node . typeCondition , accumulator [ :type_info ] . schema )
99
95
else
100
96
TypeInfo . type ( accumulator [ :type_info ] )
101
97
end
102
- stack_push ( :type_stack , output_type )
98
+ stack_push ( accumulator , :type_stack , output_type )
103
99
:VariableDefinition ->
104
100
input_type = Schema . type_from_ast ( node . type , accumulator [ :type_info ] . schema )
105
- stack_push ( :input_type_stack , input_type )
101
+ stack_push ( accumulator , :input_type_stack , input_type )
106
102
:Argument ->
107
103
field_or_directive = TypeInfo . directive ( accumulator [ :type_info ] ) ||
108
104
TypeInfo . field_def ( accumulator [ :type_info ] )
@@ -112,18 +108,18 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
112
108
fn ( arg ) -> arg == node . name . value end
113
109
)
114
110
accumulator = set_argument ( accumulator , arg_def )
115
- stack_push ( :input_type_stack , ( if arg_def && Map . has_key? ( arg_def , :type ) , do: arg_def . type , else: nil ) )
111
+ stack_push ( accumulator , :input_type_stack , ( if arg_def && Map . has_key? ( arg_def , :type ) , do: arg_def . type , else: nil ) )
116
112
else
117
113
accumulator = set_argument ( accumulator , nil )
118
- stack_push ( :input_type_stack , nil )
114
+ stack_push ( accumulator , :input_type_stack , nil )
119
115
end
120
116
:List ->
121
117
input_type = TypeInfo . input_type ( accumulator [ :type_info ] )
122
118
list_type = TypeInfo . named_type ( input_type )
123
119
if % Type.List { } === list_type do
124
- stack_push ( :input_type_stack , list_type . ofType )
120
+ stack_push ( accumulator , :input_type_stack , list_type . ofType )
125
121
else
126
- stack_push ( :input_type_stack , nil )
122
+ stack_push ( accumulator , :input_type_stack , nil )
127
123
end
128
124
:ObjectField ->
129
125
input_type = TypeInfo . input_type ( accumulator [ :type_info ] )
@@ -135,9 +131,9 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
135
131
node
136
132
)
137
133
field_type = if input_field , do: input_field . type , else: nil
138
- stack_push ( :input_type_stack , field_type )
134
+ stack_push ( accumulator , :input_type_stack , field_type )
139
135
else
140
- stack_push ( :input_type_stack , nil )
136
+ stack_push ( accumulator , :input_type_stack , nil )
141
137
end
142
138
_ ->
143
139
accumulator
@@ -148,18 +144,18 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
148
144
def leave ( _visitor , node , accumulator ) do
149
145
case node . kind do
150
146
:SelectionSet ->
151
- stack_pop ( :parent_type_stack )
147
+ stack_pop ( accumulator , :parent_type_stack )
152
148
:Field ->
153
- accumulator = stack_pop ( :field_def_stack )
154
- stack_pop ( :type_stack )
149
+ accumulator = stack_pop ( accumulator , :field_def_stack )
150
+ stack_pop ( accumulator , :type_stack )
155
151
:Directive ->
156
152
set_directive ( accumulator , nil )
157
153
kind when kind in [ :OperationDefinition , :InlineFragment , :FragmentDefinition ] ->
158
- stack_pop ( :type_stack )
154
+ stack_pop ( accumulator , :type_stack )
159
155
:Argument ->
160
156
set_argument ( accumulator , nil )
161
157
kind when kind in [ :List , :ObjectField , :VariableDefinition ] ->
162
- stack_pop ( :input_type_stack )
158
+ stack_pop ( accumulator , :input_type_stack )
163
159
_ ->
164
160
accumulator
165
161
end
0 commit comments