lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Empty `@search` prevents `ScoutBuilderDirective` from executing

Open Nextra opened this issue 1 year ago • 10 comments

Describe the bug

Empty values for @search arguments are a valid search use case. Search engines like Meilisearch will return a (more or less useful) set of results even for empty queries. Currently in lighthouse however, the ScoutEnhancer changes its behavior when the search string is null or ''. This is due to these code pieces interacting:

https://github.com/nuwave/lighthouse/blob/889c7bc8015acb3eab7b5071c7faf5eaacd2a203/src/Scout/ScoutEnhancer.php#L98-L100

https://github.com/nuwave/lighthouse/blob/889c7bc8015acb3eab7b5071c7faf5eaacd2a203/src/Scout/ScoutEnhancer.php#L51-L54

https://github.com/nuwave/lighthouse/blob/889c7bc8015acb3eab7b5071c7faf5eaacd2a203/src/Scout/ScoutEnhancer.php#L62-L65

First, the search argument is not added to the list of search arguments if the value is empty. Then, when the enhancer is probed, it claims there is nothing to be done with the ScoutBuilder.

I don't quite understand the rest of the control flow enough, but the search itself will be executed correctly. The field will be resolved using a ScoutBuilder, and the empty query will be handled by the responsible search engine as expected. However, when other ScoutBuilderDirective are placed on the argument, they will not be allowed to handle the builder while the search query value is empty. I think ScoutBuilderDirective should be able to add additional filters regardless of the search value.

Expected behavior/Solution

Any ScoutBuilderDirective that wishes to enhance the scout builder should be executed even for empty @search values.

Removing the is_string check shown above seems to fix the behavior at first glance, but I don't know if this has deeper repercussions.

Steps to reproduce

  1. Define a query field with an argument that uses @search to enable scout
  2. Build any ScoutBuilderDirective that, for example, places a simple $builder->where('foo', 'bar') on the builder
  3. Observe the builder directive not executing for null or '' search strings.

Lighthouse Version

v6.22.0

Nextra avatar Nov 09 '23 23:11 Nextra