@@ -59,22 +59,28 @@ public function addValidationRule(ValidationRule $validationRule)
5959 }
6060
6161 /**
62- * Looks for a query and variables, and tries to parse and resolve it against the schema.
62+ * Handles a request and returns the result. Internally (if not provided via arguments)
63+ * it looks for a query and variables, and tries to parse and resolve it against the schema.
64+ *
65+ * @param string|null $query
66+ * @param array|null $variables
67+ * @param string|null $operationName
68+ * @return array
6369 */
64- public function listen ()
70+ public function handle ( string $ query = null , array $ variables = null , string $ operationName = null ): array
6571 {
6672 // obtain query and variables
67- $ variables = $ this ->getVariables ();
68- $ operationName = $ this ->getOperationName ();
69- $ query = $ this ->getQuery ();
73+ $ variables = $ variables ?? $ this ->getVariables ();
74+ $ operationName = $ operationName ?? $ this ->getOperationName ();
75+ $ query = $ query ?? $ this ->getQuery ();
7076
7177 if ($ query === null ) {
7278 // no query found -> error
73- $ this -> returnData ( [
79+ return [
7480 "errors " => Errors::prettyPrintErrors (
7581 [new InternalServerError ("No query could be found. Please ensure, that your query is sent via raw POST data, json encoded and accessible via the \"query \" key. " )]
7682 )
77- ]) ;
83+ ];
7884 } else {
7985 // try to parse the query
8086 try {
@@ -84,10 +90,9 @@ public function listen()
8490 // check if is valid
8591 if (!$ this ->parser ->queryIsValid ()) {
8692 // if invalid -> show errors
87- $ this -> returnData ( [
93+ return [
8894 "errors " => Errors::prettyPrintErrors ($ this ->parser ->getErrors ())
89- ]);
90- return ;
95+ ];
9196 }
9297
9398 // validate query
@@ -96,39 +101,44 @@ public function listen()
96101 // check if is valid
97102 if (!$ this ->validator ->documentIsValid ()) {
98103 // if invalid -> show errors
99- $ this -> returnData ( [
104+ return [
100105 "errors " => Errors::prettyPrintErrors ($ this ->validator ->getErrors ())
101- ]);
102- return ;
106+ ];
103107 }
104108
105-
106109 // execute query
107- $ result = $ this ->executor ->execute ($ this ->schema , $ this ->parser ->getParsedDocument (), null , null , $ variables , $ operationName );
108- $ this ->returnData ($ result );
110+ return $ this ->executor ->execute ($ this ->schema , $ this ->parser ->getParsedDocument (), null , null , $ variables , $ operationName );
109111
110112 } catch (Error $ error ) {
111113 // 500 error -> error
112- $ this -> returnData ( [
114+ return [
113115 "errors " => Errors::prettyPrintErrors (
114116 [new InternalServerError ("An unexpected error occurred during execution " . ($ this ->displayInternalServerErrorReason ? ": " . $ error ->getMessage () . ". Trace: " . $ error ->getTraceAsString () : ". " ))]
115117 )
116- ]) ;
118+ ];
117119 } catch (Exception $ exception ) {
118120 // Unexpected exception -> error
119- $ this -> returnData ( [
121+ return [
120122 "errors " => Errors::prettyPrintErrors (
121123 [new InternalServerError ("An unexpected exception occurred during execution. " . ($ this ->displayInternalServerErrorReason ? "\n" . $ exception ->getMessage () . "\n" . $ exception ->getTraceAsString () : "" ))]
122124 )
123- ]) ;
125+ ];
124126 }
125127 }
126128 }
127129
130+ /**
131+ * Handles a request and prints the result.
132+ */
133+ public function listen ()
134+ {
135+ $ this ->returnData ($ this ->handle ());
136+ }
137+
128138 /**
129139 * @param $data
130140 */
131- public function returnData ($ data )
141+ private function returnData ($ data )
132142 {
133143 echo json_encode ($ data );
134144 }
@@ -160,7 +170,7 @@ private function getQuery(): ?string
160170 private function getOperationName (): ?string
161171 {
162172 // check if query is sent as raw http body in request as "application/json" or via post fields as "multipart/form-data"
163- $ headers = apache_request_headers () ;
173+ $ headers = function_exists ( " getallheaders " ) ? getallheaders () : [] ;
164174 if (array_key_exists ("Content-Type " , $ headers ) and $ headers ["Content-Type " ] === "application/json " ) {
165175 // raw json string in http body
166176 $ phpInput = json_decode (file_get_contents ("php://input " ), true );
@@ -179,7 +189,7 @@ private function getOperationName(): ?string
179189 private function getVariables (): array
180190 {
181191 // check if variables is sent as raw http body in request as "application/json" or via post fields as "multipart/form-data"
182- $ headers = apache_request_headers () ;
192+ $ headers = function_exists ( " getallheaders " ) ? getallheaders () : [] ;
183193 if (array_key_exists ("Content-Type " , $ headers ) and $ headers ["Content-Type " ] === "application/json " ) {
184194 // raw json string in http body
185195 $ phpInput = json_decode (file_get_contents ("php://input " ), true );
0 commit comments