Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Shopify
ShopifyClient::process_graphql_response
Process the GraphQL API response.
Method of the class: ShopifyClient{}
No Hooks.
Returns
Object|\WP_Error. Decoded response data or WP_Error.
Usage
// private - for code of main (parent) class only $result = $this->process_graphql_response( $response );
- $response(array|\WP_Error) (required)
- The HTTP response.
ShopifyClient::process_graphql_response() ShopifyClient::process graphql response code WC 10.8.1
private function process_graphql_response( $response ) {
if ( is_wp_error( $response ) ) {
return new \WP_Error( 'api_error', 'GraphQL request failed: ' . $response->get_error_message() );
}
$response_code = wp_remote_retrieve_response_code( $response );
$response_body = wp_remote_retrieve_body( $response );
if ( $response_code >= 300 ) {
$error_details = json_decode( $response_body );
$error_message = isset( $error_details->errors ) ? wp_json_encode( $error_details->errors ) : $response_body;
return new \WP_Error(
'api_error',
"GraphQL request failed with status code {$response_code}: " . $error_message
);
}
$data = json_decode( $response_body );
if ( json_last_error() !== JSON_ERROR_NONE ) {
return new \WP_Error( 'api_error', 'Failed to decode GraphQL JSON response: ' . json_last_error_msg() );
}
// Check for GraphQL-specific errors.
if ( ! empty( $data->errors ) ) {
return new \WP_Error( 'graphql_error', 'GraphQL API returned errors: ' . wp_json_encode( $data->errors ) );
}
if ( empty( $data->data ) ) {
return new \WP_Error( 'api_error', 'GraphQL response missing "data" field.' );
}
return $data->data;
}