Skip to content

Commit 5978baf

Browse files
committedMar 9, 2021
Add query options argument
1 parent 80610d6 commit 5978baf

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎src/Plugin/GraphQL/Fields/SearchAPISearch.php

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\graphql\GraphQL\Cache\CacheableValue;
1010
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
1111
use Drupal\graphql\GraphQL\Execution\ResolveContext;
12+
use Drupal\search_api\SearchApiException;
1213
use GraphQL\Type\Definition\ResolveInfo;
1314
use Symfony\Component\DependencyInjection\ContainerInterface;
1415

@@ -32,6 +33,7 @@
3233
* "sort" = "[SortInput]",
3334
* "facets" = "[FacetInput]",
3435
* "more_like_this" = "MLTInput",
36+
* "options" = "[SearchApiQueryOptions]",
3537
* "solr_params" = "[SolrParameterInput]",
3638
* },
3739
* )
@@ -319,6 +321,19 @@ private function prepareSearchQuery($args) {
319321
if ($args['facets']) {
320322
$this->setFacets($args['facets']);
321323
}
324+
// Adding options to the query.
325+
if ($args['options']) {
326+
foreach ($args['options'] as $option) {
327+
if($option['json']) {
328+
$value = json_decode($option['json'], true);
329+
} elseif($option['value']) {
330+
$value = $option['value'];
331+
} else {
332+
throw new SearchApiException("Option '{$option['key']}' must have value in 'value' or 'json' argument.");
333+
}
334+
$this->query->setOption($option['key'], $value);
335+
}
336+
}
322337
// Adding more like this parameters to the query.
323338
if ($args['more_like_this']) {
324339
$this->setMLT($args['more_like_this']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Drupal\graphql_search_api\Plugin\GraphQL\InputTypes;
4+
5+
use Drupal\graphql\Plugin\GraphQL\InputTypes\InputTypePluginBase;
6+
7+
/**
8+
* Search API Query Options input type.
9+
*
10+
* @GraphQLInputType(
11+
* id = "SearchApiQueryOptions",
12+
* name = "SearchApiQueryOptions",
13+
* description = @Translation("Allow passing Search API query options.
14+
* For simple string use ""value"" field, for array - use ""json"" field as JSON string"),
15+
* fields = {
16+
* "key" = "String!",
17+
* "value" = "String",
18+
* "json" = "String",
19+
* }
20+
* )
21+
*/
22+
class SearchApiQueryOptions extends InputTypePluginBase {
23+
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.