Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolveValues will error out if query has a fatal error #49

Open
kevinquillen opened this issue Sep 21, 2021 · 0 comments
Open

resolveValues will error out if query has a fatal error #49

kevinquillen opened this issue Sep 21, 2021 · 0 comments

Comments

@kevinquillen
Copy link

If $results errors or is NULL:

  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {

    // Load up the index passed in argument.
    $this->index = $this->entityTypeManager->getStorage('search_api_index')->load($args['index_id']);

    // Prepare the query with our arguments.
    $this->prepareSearchQuery($args);

    // Execute search.
    try {
      $results = $this->query->execute();
    }
    // Handle error, check exception type -> SearchApiException ?
    catch (\Exception $exception) {
      $this->logger->get('graphql_search_api')->error($exception->getMessage());
    }

    // Get search response from results.
    $search_response = $this->getSearchResponse($results);

    // Add the result count to the response.
    $search_response['result_count'] = $results->getResultCount();

    // Set response type.
    $search_response['type'] = 'SearchAPIResult';

    yield $search_response;

  }

I get a PHP notice:

"Notice: Undefined variable: results in Drupal\graphql_search_api\Plugin\GraphQL\Fields\SearchAPISearch->resolveValues() (line 97 of modules/contrib/graphql_search_api/src/Plugin/GraphQL/Fields/SearchAPISearch.php)."

In the browser I get a JS 500 error about an unexpected token.

This turns out to be (a rather common case) if you are running Solr and the Solr results are stale and return items that aren't in Drupal (like say, syncing a prod database to dev) and Drupal cannot load the item to display in a result. This is a little hard to track down if you aren't familiar with Search API and Solr.

I think the 3 lines pertaining to $search_response should be moved into the try block, and the catch block should have:

    $search_response = [];
    $search_response['result_count'] = 0;
    $search_response['type'] = 'SearchAPIResult';

This way at least the client doesn't break down and "empty results" message configured with Search will work.

@kevinquillen kevinquillen changed the title resolveValues will error out if query returns no results resolveValues will error out if query has a fatal error Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant