-
-
Save olivernybroe/2d2b164851b4fc7d4b62df8f37bfbb2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\GraphQL\Directives; | |
use Illuminate\Database\Eloquent\Builder; | |
use Nuwave\Lighthouse\Schema\Values\ArgumentValue; | |
use Nuwave\Lighthouse\Support\Contracts\ArgMiddleware; | |
use Nuwave\Lighthouse\Support\Traits\HandlesDirectives; | |
use Nuwave\Lighthouse\Support\Traits\HandlesQueryFilter; | |
class ScoutDirective implements ArgMiddleware | |
{ | |
use HandlesDirectives, HandlesQueryFilter; | |
/** | |
* Name of the directive. | |
* | |
* @return string | |
*/ | |
public function name() | |
{ | |
return "search"; | |
} | |
/** | |
* Resolve the field directive. | |
* | |
* @param ArgumentValue $argument | |
* | |
* @return ArgumentValue | |
*/ | |
public function handleArgument(ArgumentValue $argument) | |
{ | |
$arg = $argument->getArgName(); | |
return $this->injectFilter($argument, [ | |
'resolve' => function (Builder $query, $key, array $args) use ($arg) { | |
$class = get_class($query->getModel()); | |
return $class::search(array_get($args, $arg)); | |
}, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment