@@ -59,22 +59,28 @@ public function addValidationRule(ValidationRule $validationRule)
59
59
}
60
60
61
61
/**
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
63
69
*/
64
- public function listen ()
70
+ public function handle ( string $ query = null , array $ variables = null , string $ operationName = null ): array
65
71
{
66
72
// 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 ();
70
76
71
77
if ($ query === null ) {
72
78
// no query found -> error
73
- $ this -> returnData ( [
79
+ return [
74
80
"errors " => Errors::prettyPrintErrors (
75
81
[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. " )]
76
82
)
77
- ]) ;
83
+ ];
78
84
} else {
79
85
// try to parse the query
80
86
try {
@@ -84,10 +90,9 @@ public function listen()
84
90
// check if is valid
85
91
if (!$ this ->parser ->queryIsValid ()) {
86
92
// if invalid -> show errors
87
- $ this -> returnData ( [
93
+ return [
88
94
"errors " => Errors::prettyPrintErrors ($ this ->parser ->getErrors ())
89
- ]);
90
- return ;
95
+ ];
91
96
}
92
97
93
98
// validate query
@@ -96,39 +101,44 @@ public function listen()
96
101
// check if is valid
97
102
if (!$ this ->validator ->documentIsValid ()) {
98
103
// if invalid -> show errors
99
- $ this -> returnData ( [
104
+ return [
100
105
"errors " => Errors::prettyPrintErrors ($ this ->validator ->getErrors ())
101
- ]);
102
- return ;
106
+ ];
103
107
}
104
108
105
-
106
109
// 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 );
109
111
110
112
} catch (Error $ error ) {
111
113
// 500 error -> error
112
- $ this -> returnData ( [
114
+ return [
113
115
"errors " => Errors::prettyPrintErrors (
114
116
[new InternalServerError ("An unexpected error occurred during execution " . ($ this ->displayInternalServerErrorReason ? ": " . $ error ->getMessage () . ". Trace: " . $ error ->getTraceAsString () : ". " ))]
115
117
)
116
- ]) ;
118
+ ];
117
119
} catch (Exception $ exception ) {
118
120
// Unexpected exception -> error
119
- $ this -> returnData ( [
121
+ return [
120
122
"errors " => Errors::prettyPrintErrors (
121
123
[new InternalServerError ("An unexpected exception occurred during execution. " . ($ this ->displayInternalServerErrorReason ? "\n" . $ exception ->getMessage () . "\n" . $ exception ->getTraceAsString () : "" ))]
122
124
)
123
- ]) ;
125
+ ];
124
126
}
125
127
}
126
128
}
127
129
130
+ /**
131
+ * Handles a request and prints the result.
132
+ */
133
+ public function listen ()
134
+ {
135
+ $ this ->returnData ($ this ->handle ());
136
+ }
137
+
128
138
/**
129
139
* @param $data
130
140
*/
131
- public function returnData ($ data )
141
+ private function returnData ($ data )
132
142
{
133
143
echo json_encode ($ data );
134
144
}
@@ -160,7 +170,7 @@ private function getQuery(): ?string
160
170
private function getOperationName (): ?string
161
171
{
162
172
// 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 () : [] ;
164
174
if (array_key_exists ("Content-Type " , $ headers ) and $ headers ["Content-Type " ] === "application/json " ) {
165
175
// raw json string in http body
166
176
$ phpInput = json_decode (file_get_contents ("php://input " ), true );
@@ -179,7 +189,7 @@ private function getOperationName(): ?string
179
189
private function getVariables (): array
180
190
{
181
191
// 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 () : [] ;
183
193
if (array_key_exists ("Content-Type " , $ headers ) and $ headers ["Content-Type " ] === "application/json " ) {
184
194
// raw json string in http body
185
195
$ phpInput = json_decode (file_get_contents ("php://input " ), true );
0 commit comments