diff --git a/graphql_search_api.module b/graphql_search_api.module new file mode 100644 index 0000000..3c08768 --- /dev/null +++ b/graphql_search_api.module @@ -0,0 +1,24 @@ +entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static($container->get('entity_type.manager')); + } + + /** + * Returns an array of index permissions. + * + * @return array + * The search api index permissions. + */ + public function getPermissions() { + $permissions = []; + + foreach ($this->entityTypeManager->getStorage('search_api_index')->loadMultiple() as $index) { + $permissions += [ + self::getPermissionName($index) => [ + 'title' => $this->t('Execute GraphQL query against @index search index', [ + '@index' => $index->label(), + ]), + 'description' => $this->t('Allows user to execute arbitrary GraphQL queries against the @index Search API index. Therefore contents of the @index Search API index will be available to users with this permission via GraphQL queries.', [ + '@index' => $index->label(), + ]), + ], + ]; + } + + return $permissions; + } + + /** + * Assemble permission name that allows querying Search API index in GraphQL. + * + * @param \Drupal\search_api\IndexInterface $index + * Search API index whose permission name to assemble. + * + * @return string + * Permission name that allows executing GraphQL queries against the + * supplied Search API index. + */ + public static function getPermissionName(IndexInterface $index) { + return "execute graphql requests {$index->id()} index"; + } + +} diff --git a/src/Plugin/GraphQL/Fields/SearchAPISearch.php b/src/Plugin/GraphQL/Fields/SearchAPISearch.php index 23b8148..25cdef4 100644 --- a/src/Plugin/GraphQL/Fields/SearchAPISearch.php +++ b/src/Plugin/GraphQL/Fields/SearchAPISearch.php @@ -81,6 +81,14 @@ public function resolveValues($value, array $args, ResolveContext $context, Reso // Load up the index passed in argument. $this->index = $this->entityTypeManager->getStorage('search_api_index')->load($args['index_id']); + $access = $this->index->access('graphql_search_api_query', NULL, TRUE); + + $resolved_value = new CacheableValue(NULL, [$access]); + if (!$access->isAllowed()) { + yield $resolved_value; + return; + } + // Prepare the query with our arguments. $this->prepareSearchQuery($args); @@ -102,7 +110,8 @@ public function resolveValues($value, array $args, ResolveContext $context, Reso // Set response type. $search_response['type'] = 'SearchAPIResult'; - yield $search_response; + $resolved_value->setValue($search_response); + yield $resolved_value; }