@@ -12,6 +12,7 @@ defmodule GraphQL.Execution.Executor do
1212 alias GraphQL.Type.CompositeType
1313 alias GraphQL.Type.AbstractType
1414 alias GraphQL.Lang.AST.Nodes
15+ alias GraphQL.Util.ArrayMap
1516
1617 @ type result_data :: { :ok , Map }
1718
@@ -45,17 +46,33 @@ defmodule GraphQL.Execution.Executor do
4546 case operation . operation do
4647 :query ->
4748 { context , result } = execute_fields ( context , type , root_value , fields )
48- { :ok , result , context . errors }
49+ { :ok , expand_array_maps ( result ) , context . errors }
4950 :mutation ->
5051 { context , result } = execute_fields_serially ( context , type , root_value , fields )
51- { :ok , result , context . errors }
52+ { :ok , expand_array_maps ( result ) , context . errors }
5253 :subscription ->
5354 { :error , "Subscriptions not currently supported" }
5455 _ ->
5556 { :error , "Can only execute queries, mutations and subscriptions" }
5657 end
5758 end
5859
60+ defp expand_array_maps ( result ) when is_list ( result ) do
61+ Enum . map ( result , & expand_array_maps / 1 )
62+ end
63+ defp expand_array_maps ( % ArrayMap { } = result ) do
64+ Enum . reduce ( Enum . sort ( Map . keys ( result . map ) ) , [ ] , fn ( index , acc ) ->
65+ [ expand_array_maps ( Map . get ( result . map , index ) ) ] ++ acc
66+ end ) |> Enum . reverse
67+ end
68+ defp expand_array_maps ( result ) when is_map ( result ) do
69+ Enum . reduce ( result , % { } , fn ( { k , v } , acc ) ->
70+ Map . put ( acc , expand_array_maps ( k ) , expand_array_maps ( v ) )
71+ end )
72+ end
73+ defp expand_array_maps ( result ) , do: result
74+
75+
5976 defp collect_selections ( context , runtime_type , selection_set , field_fragment_map \\ % { fields: % { } , fragments: % { } } ) do
6077 Enum . reduce selection_set [ :selections ] , { context , field_fragment_map } , fn ( selection , { context , field_fragment_map } ) ->
6178 collect_selection ( context , runtime_type , selection , field_fragment_map )
@@ -193,11 +210,11 @@ defmodule GraphQL.Execution.Executor do
193210
194211 @ spec complete_value ( % List { } , ExecutionContext . t , GraphQL.Document . t , any , any ) :: map
195212 defp complete_value ( % List { ofType: list_type } , context , field_asts , info , result ) do
196- { context , result } = Enum . reduce result , { context , [ ] } , fn ( item , { context , acc } ) ->
213+ { context , value , _ } = Enum . reduce result , { context , % ArrayMap { } , 0 } , fn ( item , { context , acc , count } ) ->
197214 { context , value } = complete_value ( unwrap_type ( list_type ) , context , field_asts , info , item )
198- { context , [ value ] ++ acc }
215+ { context , ArrayMap . put ( acc , count , value ) , count + 1 }
199216 end
200- { context , Enum . reverse ( result ) }
217+ { context , value }
201218 end
202219
203220 defp complete_value ( return_type , context , field_asts , info , result ) when is_atom ( return_type ) do
0 commit comments