11<?php namespace Rebing \GraphQL ;
22
3+ use GraphQL \Error \Debug ;
34use GraphQL \Error \Error ;
45use Rebing \GraphQL \Error \ValidationError ;
56use GraphQL \GraphQL as GraphQLBase ;
67use GraphQL \Type \Schema ;
8+ use GraphQL \Error \FormattedError ;
79use GraphQL \Type \Definition \ObjectType ;
810use Rebing \GraphQL \Exception \SchemaNotFound ;
911use Rebing \GraphQL \Support \PaginationType ;
12+ use Illuminate \Contracts \Debug \ExceptionHandler ;
1013
1114class GraphQL {
1215 protected $ app ;
@@ -84,21 +87,7 @@ public function schema($schema = null)
8487 */
8588 public function query ($ query , $ params = [], $ opts = [])
8689 {
87- $ executionResult = $ this ->queryAndReturnResult ($ query , $ params , $ opts );
88-
89- $ data = [
90- 'data ' => $ executionResult ->data ,
91- ];
92-
93- // Add errors
94- if ( ! empty ($ executionResult ->errors ))
95- {
96- $ errorFormatter = config ('graphql.error_formatter ' , ['\Rebing\GraphQL ' , 'formatError ' ]);
97-
98- $ data ['errors ' ] = array_map ($ errorFormatter , $ executionResult ->errors );
99- }
100-
101- return $ data ;
90+ return $ this ->queryAndReturnResult ($ query , $ params , $ opts )->toArray ();
10291 }
10392
10493 public function queryAndReturnResult ($ query , $ params = [], $ opts = [])
@@ -109,7 +98,12 @@ public function queryAndReturnResult($query, $params = [], $opts = [])
10998
11099 $ schema = $ this ->schema ($ schemaName );
111100
112- $ result = GraphQLBase::executeQuery ($ schema , $ query , null , $ context , $ params , $ operationName );
101+ $ errorFormatter = config ('graphql.error_formatter ' , [static ::class, 'formatError ' ]);
102+ $ errorsHandler = config ('grahpql.errors_handler ' , [static ::class, 'handleErrors ' ]);
103+
104+ $ result = GraphQLBase::executeQuery ($ schema , $ query , null , $ context , $ params , $ operationName )
105+ ->setErrorsHandler ($ errorsHandler )
106+ ->setErrorFormatter ($ errorFormatter );
113107 return $ result ;
114108 }
115109
@@ -282,18 +276,9 @@ public function paginate($typeName, $customName = null)
282276
283277 public static function formatError (Error $ e )
284278 {
285- $ error = [
286- 'message ' => $ e ->getMessage ()
287- ];
288-
289- $ locations = $ e ->getLocations ();
290- if (!empty ($ locations ))
291- {
292- $ error ['locations ' ] = array_map (function ($ loc )
293- {
294- return $ loc ->toArray ();
295- }, $ locations );
296- }
279+ $ debug = config ('app.debug ' ) ? (Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE ) : 0 ;
280+ $ formatter = FormattedError::prepareFormatter (null , $ debug );
281+ $ error = $ formatter ($ e );
297282
298283 $ previous = $ e ->getPrevious ();
299284 if ($ previous && $ previous instanceof ValidationError)
@@ -304,6 +289,20 @@ public static function formatError(Error $e)
304289 return $ error ;
305290 }
306291
292+ public static function handleErrors (array $ errors , callable $ formatter )
293+ {
294+ $ handler = app ()->make (ExceptionHandler::class);
295+ foreach ($ errors as $ error ) {
296+ // Try to unwrap exception
297+ $ error = $ error ->getPrevious () ?: $ error ;
298+ if ($ error instanceof \GraphQL \Error \Error) {
299+ continue ;
300+ }
301+ $ handler ->report ($ error );
302+ }
303+ return array_map ($ formatter , $ errors );
304+ }
305+
307306 /**
308307 * Check if the schema expects a nest URI name and return the formatted version
309308 * Eg. 'user/me'
0 commit comments