Skip to content

Instantly share code, notes, and snippets.

@olivernybroe
Last active April 27, 2018 16:41
Show Gist options
  • Save olivernybroe/2d2b164851b4fc7d4b62df8f37bfbb2a to your computer and use it in GitHub Desktop.
Save olivernybroe/2d2b164851b4fc7d4b62df8f37bfbb2a to your computer and use it in GitHub Desktop.
<?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