Skip to content

Commit 08bd3b6

Browse files
authored
Merge branch 'drupal-graphql:8.x-1.x' into query-options
2 parents 9358812 + dfb32d1 commit 08bd3b6

8 files changed

+65
-37
lines changed

docs/search-parameters.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ evaluated against the search backend.
103103
|----------|----------|--------|-------------------------------------------------------------------------------------------------------------------------|
104104
| `operator` | no | `String` | The operator allows us to specify the evaluation expression. All Search API conditions are supported. Some common examples: `=`, `<>`, `>`, `<`, `>=`, `<=`. |
105105
| `name` | yes | `String` | The name defines what field in the index to be evaluated against this condition. |
106-
| `value` | yes | `String` | The value to be valuated against the specified field and operator. | |
106+
| `value` | no | `String` | The value to be valuated against the specified field and operator. Leave it empty if you want to compare with NULL | |
107107

108108
### Example
109109
Returns the all results with type of course.

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
This Drupal module provides an integration between [Drupal GraphQL](https://github.com/drupal-graphql/graphql) and
44
[Search API](https://www.drupal.org/project/search_api) modules.
55

6+
**Please note this module only supports 3.x version of GraphQL module.**
7+
68
It allows developers to perform queries directly to a Search API index by leveraging the Search API module's API.
79

810
Since it uses Search API it's not backend specific and can work with any engine such as Apache Solr or Elastic Search.

src/Plugin/GraphQL/Derivers/SearchAPIDocumentTypeDeriver.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
class SearchAPIDocumentTypeDeriver extends DeriverBase implements ContainerDeriverInterface {
1414

1515
/**
16+
* The entity type manager service.
17+
*
1618
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
1719
*/
1820
protected $entityTypeManager;

src/Plugin/GraphQL/Derivers/SearchAPIFieldDeriver.php

+34-25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Entity\EntityTypeManagerInterface;
77
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
88
use Drupal\graphql_search_api\Utility\SearchAPIHelper;
9+
use Drupal\search_api\Item\FieldInterface;
910
use Symfony\Component\DependencyInjection\ContainerInterface;
1011

1112
/**
@@ -14,6 +15,8 @@
1415
class SearchAPIFieldDeriver extends DeriverBase implements ContainerDeriverInterface {
1516

1617
/**
18+
* The entity type manager service.
19+
*
1720
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
1821
*/
1922
protected $entityTypeManager;
@@ -48,21 +51,27 @@ public function getDerivativeDefinitions($base_plugin_definition) {
4851
$this->derivatives['index_id']['name'] = 'index_id';
4952
$this->derivatives['index_id']['type'] = 'String';
5053

54+
/** @var \Drupal\search_api\IndexInterface $index */
5155
foreach ($indexes as $index_id => $index) {
56+
$parent = str_replace("_", "", ucwords($index_id . "Doc", '_'));
5257

5358
foreach ($index->getFields() as $field_id => $field) {
54-
55-
// Define to which Doc type variant the field belongs to.
56-
$base_plugin_definition['parents'][0] = str_replace("_", "", ucwords($index_id . "Doc", '_'));
57-
58-
// Initialising derivative settings.
59-
$this->derivatives[$field_id] = $base_plugin_definition;
60-
$this->derivatives[$field_id]['id'] = $field_id;
61-
$this->derivatives[$field_id]['name'] = $field_id;
62-
63-
// Set field type.
64-
$this->setFieldType($field, $field_id);
65-
59+
$derivative_id = implode(':', [$parent, $field_id]);
60+
if (isset($this->derivatives[$derivative_id])) {
61+
$base_plugin_definition['parents'][] = $parent;
62+
}
63+
else {
64+
// Define to which Doc type variant the field belongs to.
65+
$base_plugin_definition['parents'] = [$parent];
66+
67+
// Initialising derivative settings.
68+
$this->derivatives[$derivative_id] = $base_plugin_definition;
69+
$this->derivatives[$derivative_id]['id'] = $field_id;
70+
$this->derivatives[$derivative_id]['name'] = $field_id;
71+
72+
// Set field type.
73+
$this->setFieldType($field, $derivative_id);
74+
}
6675
}
6776
}
6877
return $this->derivatives;
@@ -71,56 +80,56 @@ public function getDerivativeDefinitions($base_plugin_definition) {
7180
/**
7281
* This method maps the field types in Search API to GraphQL types.
7382
*
74-
* @field
83+
* @param \Drupal\search_api\Item\FieldInterface $field
7584
* The field to map.
76-
* @field_id
77-
* The id of the field to map.
85+
* @param string $derivative_id
86+
* The id of the field derivative to map.
7887
*/
79-
private function setFieldType($field, $field_id) {
88+
private function setFieldType(FieldInterface $field, $derivative_id) {
8089

8190
// Get field type.
8291
$type = $field->getType();
8392

8493
// We can only check if a field is multivalue if it has a Datasource.
85-
// @Todo This seems inefficient, check when it's being cached
94+
// @todo This seems inefficient, check when it's being cached
8695
$multivalue = SearchAPIHelper::checkMultivalue($field);
8796

8897
// Map the Search API types to GraphQL.
8998
switch ($type) {
9099

91100
case 'text':
92-
$this->derivatives[$field_id]['type'] = 'String';
101+
$this->derivatives[$derivative_id]['type'] = 'String';
93102
break;
94103

95104
case 'string':
96-
$this->derivatives[$field_id]['type'] = 'String';
105+
$this->derivatives[$derivative_id]['type'] = 'String';
97106
break;
98107

99108
case 'boolean':
100-
$this->derivatives[$field_id]['type'] = 'Boolean';
109+
$this->derivatives[$derivative_id]['type'] = 'Boolean';
101110
break;
102111

103112
case 'integer':
104-
$this->derivatives[$field_id]['type'] = 'Int';
113+
$this->derivatives[$derivative_id]['type'] = 'Int';
105114
break;
106115

107116
case 'decimal':
108-
$this->derivatives[$field_id]['type'] = 'Float';
117+
$this->derivatives[$derivative_id]['type'] = 'Float';
109118
break;
110119

111120
case 'date':
112-
$this->derivatives[$field_id]['type'] = 'Timestamp';
121+
$this->derivatives[$derivative_id]['type'] = 'Timestamp';
113122
break;
114123

115124
default:
116-
$this->derivatives[$field_id]['type'] = 'String';
125+
$this->derivatives[$derivative_id]['type'] = 'String';
117126
break;
118127

119128
}
120129

121130
// If field is multivalue we set the type as an array.
122131
if ($multivalue) {
123-
$this->derivatives[$field_id]['type'] = '[' . $this->derivatives[$field_id]['type'] . ']';
132+
$this->derivatives[$derivative_id]['type'] = '[' . $this->derivatives[$derivative_id]['type'] . ']';
124133
}
125134
}
126135

src/Plugin/GraphQL/Fields/SearchAPIFacetValues.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class SearchAPIFacetValues extends FieldPluginBase {
2525
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
2626
if (isset($value['solrFacetValues'])) {
2727
foreach ($value['solrFacetValues'] as $facet_value) {
28-
yield $facet_value;
28+
yield $facet_value;
2929
}
3030
}
3131
}
32+
3233
}

src/Plugin/GraphQL/Fields/SearchAPIField.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class SearchAPIField extends FieldPluginBase {
2222
* {@inheritdoc}
2323
*/
2424
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
25-
2625
$derivative_id = $this->getDerivativeId();
26+
list($parent_type, $field_id) = explode(':', $derivative_id);
2727

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

31-
$field = $value['item'][$derivative_id];
31+
$field = $value['item'][$field_id];
3232

3333
$field_values = $field->getValues();
3434
$field_type = $field->getType();

src/Plugin/GraphQL/Fields/SearchAPISearch.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Drupal\Core\Entity\EntityTypeManagerInterface;
77
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
88
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9-
use Drupal\graphql\GraphQL\Cache\CacheableValue;
109
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
1110
use Drupal\graphql\GraphQL\Execution\ResolveContext;
1211
use Drupal\search_api\SearchApiException;
@@ -41,16 +40,31 @@
4140
class SearchAPISearch extends FieldPluginBase implements ContainerFactoryPluginInterface {
4241

4342
/**
43+
* The entity type manager service.
44+
*
4445
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
4546
*/
4647
protected $entityTypeManager;
4748

4849
/**
50+
* The logger service.
51+
*
4952
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
5053
*/
5154
protected $logger;
5255

56+
/**
57+
* The query object.
58+
*
59+
* @var \Drupal\views\Plugin\views\query\QueryPluginBase
60+
*/
5361
private $query;
62+
63+
/**
64+
* The search index.
65+
*
66+
* @var \Drupal\search_api\IndexInterface
67+
*/
5468
private $index;
5569

5670
/**
@@ -154,7 +168,7 @@ private function addConditionGroup($condition_group_arg) {
154168

155169
// Set default conjunction and tags.
156170
$group_conjunction = 'AND';
157-
$group_tags = array();
171+
$group_tags = [];
158172

159173
// Set conjunction from args.
160174
if (isset($group['conjunction'])) {
@@ -258,10 +272,10 @@ private function setFacets($facets) {
258272
/**
259273
* Sets MLT in the Search API query.
260274
*
261-
* @facets
275+
* @mlt_params
262276
* The MLT params to be added to the query.
263277
*/
264-
private function setMLT($mlt_params) {
278+
private function setMlt($mlt_params) {
265279

266280
// Retrieve this index server details.
267281
$server = $this->index->getServerInstance();
@@ -336,7 +350,7 @@ private function prepareSearchQuery($args) {
336350
}
337351
// Adding more like this parameters to the query.
338352
if ($args['more_like_this']) {
339-
$this->setMLT($args['more_like_this']);
353+
$this->setMlt($args['more_like_this']);
340354
}
341355
}
342356

@@ -354,7 +368,7 @@ private function getSearchResponse($results) {
354368
$search_response = [];
355369

356370
// Loop through each item in the result set.
357-
foreach ($result_items as $id => &$item) {
371+
foreach ($result_items as &$item) {
358372
// Load the response document into the search response array.
359373
$document['item'] = $item;
360374
$document['index_id'] = $this->index->id();

src/Plugin/GraphQL/InputTypes/ConditionInput.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* name = "ConditionInput",
1313
* fields = {
1414
* "name" = "String!",
15-
* "value" = "String!",
15+
* "value" = "String",
1616
* "operator" = "String"
1717
* }
1818
* )

0 commit comments

Comments
 (0)