Skip to content

Commit dece375

Browse files
authored
Merge pull request #23 from drupal-graphql/load-response-doc-refactor
Load response doc refactor
2 parents cfb2637 + 1805ec9 commit dece375

File tree

3 files changed

+49
-106
lines changed

3 files changed

+49
-106
lines changed

src/Plugin/GraphQL/Fields/SearchAPIFacets.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,22 @@ class SearchAPIFacets extends FieldPluginBase {
2424
*/
2525
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
2626
if (isset($value['facets'])) {
27-
foreach ($value['facets'] as $facet) {
28-
yield $facet;
27+
foreach ($value['facets'] as $facet_id => $facet) {
28+
29+
// Prepare a facet response.
30+
$response_facet['type'] = 'SearchAPIFacet';
31+
$response_facet['name'] = $facet_id;
32+
33+
// Loop through the facet values and load them into the response.
34+
foreach ($facet as $facet_value) {
35+
$response_facet_value = [];
36+
$response_facet_value['type'] = 'SearchAPIFacetValue';
37+
$response_facet_value['filter'] = trim($facet_value['filter'], '"');
38+
$response_facet_value['count'] = $facet_value['count'];
39+
$response_facet['solrFacetValues'][] = $response_facet_value;
40+
}
41+
42+
yield $response_facet;
2943
}
3044
}
3145
}

src/Plugin/GraphQL/Fields/SearchAPIField.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,37 @@ public function resolveValues($value, array $args, ResolveContext $context, Reso
2626
$derivative_id = $this->getDerivativeId();
2727

2828
// Not all documents have values for all fields so we need to check.
29-
if (isset($value[$derivative_id])) {
29+
if (isset($value['item'][$derivative_id])) {
3030

31-
// Checking if the value of this derivative is a list or single value so
32-
// we can parse accordingly.
33-
if (is_array($value[$derivative_id])) {
34-
foreach ($value[$derivative_id] as $value_item) {
35-
yield $value_item;
31+
$field = $value['item'][$derivative_id];
32+
33+
$field_values = $field->getValues();
34+
$field_type = $field->getType();
35+
$value = NULL;
36+
37+
// Fulltext multivalue fields have a different format.
38+
if ($field_type == 'text') {
39+
// Create a new array with text values instead of objects.
40+
foreach ($field_values as $field_value) {
41+
$value[] = $field_value->getText();
3642
}
3743
}
44+
// For other types of fields we can just grab contents from the array.
3845
else {
39-
yield $value[$derivative_id];
46+
$value = $field_values;
47+
}
48+
// Load the value in the response document.
49+
if (!is_null($value)) {
50+
// Checking if the value of this derivative is a list or single value so
51+
// we can parse accordingly.
52+
if (is_array($value)) {
53+
foreach ($value as $value_item) {
54+
yield $value_item;
55+
}
56+
}
57+
else {
58+
yield $value;
59+
}
4060
}
4161
}
4262
}

src/Plugin/GraphQL/Fields/SearchAPISearch.php

Lines changed: 6 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -341,111 +341,20 @@ private function getSearchResponse($results) {
341341
// Loop through each item in the result set.
342342
foreach ($result_items as $id => &$item) {
343343
// Load the response document into the search response array.
344-
$search_response['SearchAPIDocument'][] = $this->loadResponseDocument($item);
344+
$document['item'] = $item->getFields();
345+
$document['index_id'] = $this->index->id();;
346+
$document['type'] = str_replace("_", "", ucwords($this->index->id() . "Doc", '_'));
347+
348+
$search_response['SearchAPIDocument'][] = $document;
345349
}
346350

347351
// Extract facets from the result set.
348352
$facets = $results->getExtraData('search_api_facets');
349353

350354
if ($facets) {
351-
// Loop through each facet in the result set.
352-
foreach ($facets as $facet_id => $facet_values) {
353-
// Load the response facet in the response array.
354-
$search_response['facets'][] = $this->loadResponseFacet($facet_id, $facet_values);
355-
}
355+
$search_response['facets'] = $facets;
356356
}
357357
return $search_response;
358358
}
359359

360-
/**
361-
* Loads a Facet into a structured response array.
362-
*
363-
* @facet_id
364-
* The id of the facet to be added.
365-
* @facet_values
366-
* The values for the facet to be added.
367-
*/
368-
private function loadResponseFacet($facet_id, $facet_values) {
369-
370-
// Initialise variables.
371-
$response_facet = [];
372-
373-
// Config the facet response.
374-
$response_facet['type'] = 'SearchAPIFacet';
375-
$response_facet['name'] = $facet_id;
376-
377-
// Loop through the facet values and load them into the response.
378-
foreach ($facet_values as $facet_value) {
379-
$response_facet_value = [];
380-
$response_facet_value['type'] = 'SearchAPIFacetValue';
381-
$response_facet_value['filter'] = trim($facet_value['filter'], '"');
382-
$response_facet_value['count'] = $facet_value['count'];
383-
$response_facet['solrFacetValues'][] = $response_facet_value;
384-
}
385-
386-
return $response_facet;
387-
}
388-
389-
/**
390-
* Loads a Document into a structured response array.
391-
*
392-
* @result_item
393-
* The result item that contains the document information.
394-
*/
395-
private function loadResponseDocument($result_item) {
396-
397-
// Initialise a response document.
398-
$response_document = [];
399-
400-
// Loop through all fields in the result item.
401-
foreach ($result_item->getFields() as $field_id => $field) {
402-
403-
// Initialise a values.
404-
$value = NULL;
405-
$field_values = $field->getValues();
406-
$field_type = $field->getType();
407-
$field_cardinality = count($field_values);
408-
409-
// Fulltext multivalue fields have a different format.
410-
if ($field_type == 'text') {
411-
// Multivalue fields.
412-
if ($field_cardinality > 1) {
413-
// Create a new array with text values instead of objects.
414-
foreach ($field_values as $field_value) {
415-
$value[] = $field_value->getText();
416-
}
417-
}
418-
// Singlevalue fields.
419-
elseif (!empty($field_values)) {
420-
$value = $field_values[0]->getText();
421-
}
422-
}
423-
// For other types of fields we can just grab contents from the array.
424-
else {
425-
// Multivalue fields.
426-
if ($field_cardinality > 1) {
427-
$value = $field_values;
428-
}
429-
// Single value fields.
430-
else {
431-
if (!empty($field_values)) {
432-
$value = $field_values[0];
433-
}
434-
}
435-
}
436-
// Load the value in the response document.
437-
if (!empty($value)) {
438-
$response_document[$field_id] = $value;
439-
}
440-
}
441-
442-
// Set the index id in the response document.
443-
$response_document['index_id'] = $this->index->id();
444-
445-
// Append the response to the correct index document.
446-
$response_document['type'] = str_replace("_", "", ucwords($this->index->id() . "Doc", '_'));
447-
448-
return $response_document;
449-
}
450-
451360
}

0 commit comments

Comments
 (0)