Skip to content

Commit b634d69

Browse files
authored
Merge pull request #294 from wp-graphql/fix/queryid-not-outputting-keys
fix: queryid not returning X-GraphQL-Keys headers
2 parents 7300dce + 8f3336d commit b634d69

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/Document.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -195,57 +195,62 @@ public function graphql_mutation_insert( $post_object, $filtered_input, $input,
195195

196196
/**
197197
* Process request looking for when queryid and query are present.
198-
* Save the query and remove it from the request
198+
* Save the query and remove it from the request.
199199
*
200200
* @param array $parsed_body_params Request parameters.
201-
* @param array $request_context An array containing the both body and query params
201+
* @param array $request_context An array containing both body and query params.
202202
*
203203
* @return array Updated $parsed_body_params Request parameters.
204204
* @throws RequestError
205205
*/
206206
public function graphql_query_contains_query_id_cb( $parsed_body_params, $request_context ) {
207207

208-
// if both query and queryId are set
209-
// we should attempt to save the query document (per APQ)
210-
if ( ! empty( $parsed_body_params['query'] ) && ! empty( $parsed_body_params['queryId'] ) ) {
211-
// save the query
212-
// The query string already coming from 'graphql_request_data' already has the query string unslashed.
213-
$this->save( $parsed_body_params['queryId'], $parsed_body_params['query'] );
208+
// Normalize keys to handle both `queryId` and `queryid`.
209+
$query_id_key = isset( $parsed_body_params['queryId'] ) ? 'queryId' : ( isset( $parsed_body_params['queryid'] ) ? 'queryid' : null );
214210

215-
// remove it from process body params so graphql-php operation proceeds without conflict.
211+
// If both query and queryId/queryid are set
212+
if ( ! empty( $parsed_body_params['query'] ) && ! empty( $query_id_key ) ) {
213+
// Save the query
214+
$this->save( $parsed_body_params[ $query_id_key ], $parsed_body_params['query'] );
215+
216+
// Remove it from processed body params so graphql-php operation proceeds without conflict.
216217
unset( $parsed_body_params['query'] );
217218
}
218219

219-
// if the query is empty, but the queryId is set
220-
if ( empty( $parsed_body_params['query'] ) && ! empty( $parsed_body_params['queryId'] ) ) {
221-
$query_string = $this->get( $parsed_body_params['queryId'] );
220+
// If the query is empty, but queryId/queryid is set
221+
if ( empty( $parsed_body_params['query'] ) && ! empty( $query_id_key ) ) {
222+
$query_string = $this->get( $parsed_body_params[ $query_id_key ] );
222223
if ( ! empty( $query_string ) ) {
223224
$parsed_body_params['query'] = $query_string;
224-
$parsed_body_params['originalQueryId'] = $parsed_body_params['queryId'];
225-
unset( $parsed_body_params['queryId'] );
225+
$parsed_body_params['originalQueryId'] = $parsed_body_params[ $query_id_key ];
226+
unset( $parsed_body_params[ $query_id_key ] );
226227
}
227228
}
228229

229230
return $parsed_body_params;
230231
}
231232

232233
/**
233-
* During invoking 'graphql()', not as an http request, if queryId is present, look it up and return the query string
234+
* During invoking 'graphql()', not as an HTTP request, if queryId is present, look it up and return the query string.
234235
*
235236
* @param string $query The graphql query string.
236-
* @param mixed|array|\GraphQL\Server\OperationParams $params The graphql request params, containing queryId
237+
* @param mixed|array|\GraphQL\Server\OperationParams $params The graphql request params, potentially containing queryId.
237238
*
238239
* @return string|null
239240
*/
240241
public function graphql_execute_query_params_cb( $query, $params ) {
241242
$query_id = null;
242243
if ( empty( $query ) ) {
243-
//phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
244+
// Check both camelCase and lowercase query ID
244245
if ( isset( $params->queryId ) ) {
245246
//phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
246247
$query_id = $params->queryId;
247-
} elseif ( is_array( $params ) && isset( $params['queryId'] ) ) {
248-
$query_id = $params['queryId'];
248+
} elseif ( is_array( $params ) ) {
249+
if ( isset( $params['queryid'] ) ) {
250+
$query_id = $params['queryid'];
251+
} elseif ( isset( $params['queryId'] ) ) {
252+
$query_id = $params['queryId'];
253+
}
249254
}
250255

251256
if ( ! empty( $query_id ) ) {

0 commit comments

Comments
 (0)