laravel-lighthouselaravel-scoutmeilisearch

How to use Meilisearch filters in Laravel lighthouse @search


How can I use meilisearch filters in laravel lighthouse @search directive

I have for example:

type Query {
  movies(search: String @search): [Movie!]! @paginate
}

I tried a graphql query with search as a variable but it doesn't work:

 {
   "search": "'Avengers', ['filter' => 'release_date > 795484800']"
 }

Eveything is set up correctly in scout because if I use curl with the example from meilisearch docs everything works correctly! But I cannot figure out how to add filter value to the @search directive in lighthouse.

curl \
  -X POST 'http://localhost:7700/indexes/movie_ratings/search' \
  -H 'Content-Type: application/json' \
  --data-binary '{
    "q": "Avengers",
    "filter": "release_date > 795484800"
  }'

Solution

  • First of all laravel 10.x scout only supports where clauses with = not with < or >

    So for the example above I ended up using a local scope that returns a Boolean and I added it to toSearchableArray().

    If everything is setup correctly in scout's meilisearch settings (you added filterableAttributes in the index-settings) you may use that filter in a where clause inside scout builder.

    So the query in lighthouse looks like this:

    type Query {
      movies(search: String @search released: Boolean @released): [Movie!]! @paginate
    }
    

    Where @released is a custom directive (arg builder) that implements ScoutBuilderDirective that adds to the scout builder:

    public function handleScoutBuilder(ScoutBuilder $builder, mixed $value): ScoutBuilder
        {
            return $builder->where('released', true);
        }