11<?php
22namespace GraphQL ;
33
4+ use GraphQL \Executor \ExecutionResult ;
45use GraphQL \Executor \Executor ;
56use GraphQL \Language \Parser ;
67use GraphQL \Language \Source ;
@@ -17,19 +18,32 @@ class GraphQL
1718 * @return array
1819 */
1920 public static function execute (Schema $ schema , $ requestString , $ rootValue = null , $ variableValues = null , $ operationName = null )
21+ {
22+ return self ::executeAndReturnResult ($ schema , $ requestString , $ rootValue , $ variableValues , $ operationName )->toArray ();
23+ }
24+
25+ /**
26+ * @param Schema $schema
27+ * @param $requestString
28+ * @param null $rootValue
29+ * @param null $variableValues
30+ * @param null $operationName
31+ * @return array|ExecutionResult
32+ */
33+ public static function executeAndReturnResult (Schema $ schema , $ requestString , $ rootValue = null , $ variableValues = null , $ operationName = null )
2034 {
2135 try {
2236 $ source = new Source ($ requestString ?: '' , 'GraphQL request ' );
2337 $ documentAST = Parser::parse ($ source );
2438 $ validationErrors = DocumentValidator::validate ($ schema , $ documentAST );
2539
2640 if (!empty ($ validationErrors )) {
27- return [ ' errors ' => array_map ([ ' GraphQL\Error ' , ' formatError ' ], $ validationErrors )] ;
41+ return new ExecutionResult ( null , $ validationErrors );
2842 } else {
29- return Executor::execute ($ schema , $ documentAST , $ rootValue , $ variableValues , $ operationName )-> toArray () ;
43+ return Executor::execute ($ schema , $ documentAST , $ rootValue , $ variableValues , $ operationName );
3044 }
3145 } catch (Error $ e ) {
32- return [ ' errors ' => [Error:: formatError ( $ e )]] ;
46+ return new ExecutionResult ( null , [ $ e ]) ;
3347 }
3448 }
3549}
0 commit comments