@@ -13,31 +13,48 @@ defmodule GraphQL.Execution.ExecutionContext do
1313
1414 @ spec new ( GraphQL.Schema . t , GraphQL.Document . t , map , map , String . t ) :: __MODULE__ . t
1515 def new ( schema , document , root_value , variable_values , operation_name ) do
16- Enum . reduce document . definitions , % __MODULE__ {
16+
17+ initial_context = % __MODULE__ {
1718 schema: schema ,
1819 fragments: % { } ,
1920 root_value: root_value ,
2021 operation: nil ,
2122 variable_values: variable_values || % { } ,
2223 errors: [ ]
23- } , fn ( definition , context ) ->
24-
25- case definition do
26- % { kind: :OperationDefinition } ->
27- cond do
28- ! operation_name && context . operation ->
29- report_error ( context , "Must provide operation name if query contains multiple operations." )
30- ! operation_name || definition . name . value === operation_name ->
31- context = % { context | operation: definition }
32- % { context | variable_values: GraphQL.Execution.Variables . extract ( context ) }
33- true -> context
34- end
35- % { kind: :FragmentDefinition } ->
36- put_in ( context . fragments [ definition . name . value ] , definition )
37- end
24+ }
25+
26+ document . definitions
27+ |> Enum . reduce ( initial_context , build_definition_handler ( operation_name ) )
28+ |> validate_operation_exists ( operation_name )
29+ end
30+
31+ defp build_definition_handler ( operation_name ) do
32+ fn ( definition , context ) -> handle_definition ( operation_name , definition , context ) end
33+ end
34+
35+ defp handle_definition ( operation_name , definition = % { kind: :OperationDefinition } , context ) do
36+ multiple_operations_no_operation_name = ! operation_name && context . operation
37+ should_set_operation = ! operation_name || definition . name . value === operation_name
38+ cond do
39+ multiple_operations_no_operation_name ->
40+ report_error ( context , "Must provide operation name if query contains multiple operations." )
41+ should_set_operation ->
42+ context = % { context | operation: definition }
43+ % { context | variable_values: GraphQL.Execution.Variables . extract ( context ) }
44+ true -> context
3845 end
3946 end
4047
48+ defp handle_definition ( _ , definition = % { kind: :FragmentDefinition } , context ) do
49+ put_in ( context . fragments [ definition . name . value ] , definition )
50+ end
51+
52+ defp validate_operation_exists ( context , nil ) , do: context
53+ defp validate_operation_exists ( context = % { operation: nil } , operation_name ) do
54+ report_error ( context , "Operation `#{ operation_name } ` not found in query." )
55+ end
56+ defp validate_operation_exists ( context , _operation_name ) , do: context
57+
4158 @ spec report_error ( __MODULE__ . t , String . t ) :: __MODULE__ . t
4259 def report_error ( context , msg ) do
4360 put_in ( context . errors , [ % { "message" => msg } | context . errors ] )
0 commit comments