From d303c149c06741bf9deb4328a50ce1e0b68d7520 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Fri, 28 Apr 2023 14:06:47 +0200 Subject: [PATCH 01/19] move trait to namespace --- src/Schema/Directives/AggregateDirective.php | 1 + src/Schema/Directives/CountDirective.php | 1 + src/Schema/Directives/RelationDirective.php | 1 + src/Schema/Directives/{ => Traits}/RelationDirectiveHelpers.php | 2 +- src/Schema/Directives/WithRelationDirective.php | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) rename src/Schema/Directives/{ => Traits}/RelationDirectiveHelpers.php (97%) diff --git a/src/Schema/Directives/AggregateDirective.php b/src/Schema/Directives/AggregateDirective.php index 605fc0f0d5..eb4062737b 100644 --- a/src/Schema/Directives/AggregateDirective.php +++ b/src/Schema/Directives/AggregateDirective.php @@ -15,6 +15,7 @@ use Nuwave\Lighthouse\Execution\ModelsLoader\AggregateModelsLoader; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; +use Nuwave\Lighthouse\Schema\Directives\Traits\RelationDirectiveHelpers; use Nuwave\Lighthouse\Schema\Values\FieldValue; use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; use Nuwave\Lighthouse\Support\Contracts\FieldResolver; diff --git a/src/Schema/Directives/CountDirective.php b/src/Schema/Directives/CountDirective.php index eac1d21bac..f1ab9e73cb 100644 --- a/src/Schema/Directives/CountDirective.php +++ b/src/Schema/Directives/CountDirective.php @@ -13,6 +13,7 @@ use Nuwave\Lighthouse\Execution\ModelsLoader\CountModelsLoader; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; +use Nuwave\Lighthouse\Schema\Directives\Traits\RelationDirectiveHelpers; use Nuwave\Lighthouse\Schema\Values\FieldValue; use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; use Nuwave\Lighthouse\Support\Contracts\FieldResolver; diff --git a/src/Schema/Directives/RelationDirective.php b/src/Schema/Directives/RelationDirective.php index b51fc5ac86..a79eff7af8 100644 --- a/src/Schema/Directives/RelationDirective.php +++ b/src/Schema/Directives/RelationDirective.php @@ -21,6 +21,7 @@ use Nuwave\Lighthouse\Pagination\PaginationType; use Nuwave\Lighthouse\Schema\AST\ASTHelper; use Nuwave\Lighthouse\Schema\AST\DocumentAST; +use Nuwave\Lighthouse\Schema\Directives\Traits\RelationDirectiveHelpers; use Nuwave\Lighthouse\Schema\Values\FieldValue; use Nuwave\Lighthouse\Support\Contracts\FieldResolver; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; diff --git a/src/Schema/Directives/RelationDirectiveHelpers.php b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php similarity index 97% rename from src/Schema/Directives/RelationDirectiveHelpers.php rename to src/Schema/Directives/Traits/RelationDirectiveHelpers.php index 4f70e98d90..1f51950b96 100644 --- a/src/Schema/Directives/RelationDirectiveHelpers.php +++ b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php @@ -1,6 +1,6 @@ Date: Fri, 28 Apr 2023 15:08:20 +0200 Subject: [PATCH 02/19] move common builder resolver logic to trait --- src/Pagination/PaginateDirective.php | 18 +++--------- src/Schema/Directives/AllDirective.php | 18 ++++-------- .../Directives/Traits/HasBuilderArgument.php | 29 +++++++++++++++++++ 3 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 src/Schema/Directives/Traits/HasBuilderArgument.php diff --git a/src/Pagination/PaginateDirective.php b/src/Pagination/PaginateDirective.php index d1c2e904b4..12331ad97c 100644 --- a/src/Pagination/PaginateDirective.php +++ b/src/Pagination/PaginateDirective.php @@ -14,6 +14,7 @@ use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; +use Nuwave\Lighthouse\Schema\Directives\Traits\HasBuilderArgument; use Nuwave\Lighthouse\Schema\Values\FieldValue; use Nuwave\Lighthouse\Support\Contracts\ComplexityResolverDirective; use Nuwave\Lighthouse\Support\Contracts\Directive; @@ -23,6 +24,8 @@ class PaginateDirective extends BaseDirective implements FieldResolver, FieldManipulator, ComplexityResolverDirective { + use HasBuilderArgument; + public static function definition(): string { return /** @lang GraphQL */ <<<'GRAPHQL' @@ -152,21 +155,8 @@ public function resolveField(FieldValue $fieldValue): callable return $paginator; } - if ($this->directiveHasArgument('builder')) { - $builderResolver = $this->getResolverFromArgument('builder'); - - $query = $builderResolver($root, $args, $context, $resolveInfo); - - assert( - $query instanceof QueryBuilder || $query instanceof EloquentBuilder || $query instanceof ScoutBuilder || $query instanceof Relation, - "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", - ); - } else { - $query = $this->getModelClass()::query(); - } - $query = $resolveInfo->enhanceBuilder( - $query, + $this->getBuilder($root, $args, $context, $resolveInfo), $this->directiveArgValue('scopes', []), $root, $args, diff --git a/src/Schema/Directives/AllDirective.php b/src/Schema/Directives/AllDirective.php index 962b8ff6bd..9e1947c662 100644 --- a/src/Schema/Directives/AllDirective.php +++ b/src/Schema/Directives/AllDirective.php @@ -5,6 +5,7 @@ use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -12,6 +13,7 @@ use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; +use Nuwave\Lighthouse\Schema\Directives\Traits\HasBuilderArgument; use Nuwave\Lighthouse\Schema\Values\FieldValue; use Nuwave\Lighthouse\Support\Contracts\FieldManipulator; use Nuwave\Lighthouse\Support\Contracts\FieldResolver; @@ -19,6 +21,8 @@ class AllDirective extends BaseDirective implements FieldResolver, FieldManipulator { + use HasBuilderArgument; + public static function definition(): string { return /** @lang GraphQL */ <<<'GRAPHQL' @@ -52,21 +56,9 @@ public static function definition(): string public function resolveField(FieldValue $fieldValue): callable { return function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection { - if ($this->directiveHasArgument('builder')) { - $builderResolver = $this->getResolverFromArgument('builder'); - - $query = $builderResolver($root, $args, $context, $resolveInfo); - assert( - $query instanceof QueryBuilder || $query instanceof EloquentBuilder || $query instanceof ScoutBuilder || $query instanceof Relation, - "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", - ); - } else { - $query = $this->getModelClass()::query(); - } - return $resolveInfo ->enhanceBuilder( - $query, + $this->getBuilder($root, $args, $context, $resolveInfo), $this->directiveArgValue('scopes', []), $root, $args, diff --git a/src/Schema/Directives/Traits/HasBuilderArgument.php b/src/Schema/Directives/Traits/HasBuilderArgument.php new file mode 100644 index 0000000000..c19164bf22 --- /dev/null +++ b/src/Schema/Directives/Traits/HasBuilderArgument.php @@ -0,0 +1,29 @@ +directiveHasArgument('builder')) { + return $this->getModelClass()::query(); + } + + $builderResolver = $this->getResolverFromArgument('builder'); + + $query = $builderResolver($root, $args, $context, $resolveInfo); + + assert( + $query instanceof Builder || $query instanceof ScoutBuilder, + "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", + ); + + return $query; + } +} From 08de9bf0161610fa356c4a4d907e7bc25b5f7392 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Fri, 28 Apr 2023 15:10:44 +0200 Subject: [PATCH 03/19] also include relations in the builder argument for @aggregate --- src/Schema/Directives/AggregateDirective.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Schema/Directives/AggregateDirective.php b/src/Schema/Directives/AggregateDirective.php index eb4062737b..5ffa50a90a 100644 --- a/src/Schema/Directives/AggregateDirective.php +++ b/src/Schema/Directives/AggregateDirective.php @@ -8,7 +8,7 @@ use GraphQL\Language\AST\ObjectTypeDefinitionNode; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder as QueryBuilder; use Nuwave\Lighthouse\Exceptions\DefinitionException; use Nuwave\Lighthouse\Execution\BatchLoader\BatchLoaderRegistry; use Nuwave\Lighthouse\Execution\BatchLoader\RelationBatchLoader; @@ -148,8 +148,8 @@ public function resolveField(FieldValue $fieldValue): callable $query = $builderResolver($root, $args, $context, $resolveInfo); assert( - $query instanceof QueryBuilder || $query instanceof EloquentBuilder, - "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder.", + $query instanceof QueryBuilder, + "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", ); $this->makeBuilderDecorator($root, $args, $context, $resolveInfo)($query); From 70bc192c4fa03e1b569ad2dfa0cbf54f087a1e0f Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Fri, 28 Apr 2023 15:58:22 +0200 Subject: [PATCH 04/19] refactor builder typehints to use the new builder contract, remove unused imports --- src/Auth/WhereAuthDirective.php | 5 ++--- src/Console/PrintSchemaCommand.php | 2 -- src/Exceptions/DefinitionException.php | 1 - src/Execution/ResolveInfo.php | 18 ++++++++---------- src/OrderBy/OrderByDirective.php | 7 +++---- src/Pagination/PaginateDirective.php | 4 ---- src/Pagination/PaginationArgs.php | 11 +++++------ src/Schema/Directives/AggregateDirective.php | 5 ++--- src/Schema/Directives/AllDirective.php | 5 ----- src/Schema/Directives/BuilderDirective.php | 8 +++----- src/Schema/Directives/ComplexityDirective.php | 1 - src/Schema/Directives/EqDirective.php | 8 +++----- src/Schema/Directives/InDirective.php | 6 ++---- src/Schema/Directives/LikeDirective.php | 8 +++----- src/Schema/Directives/NeqDirective.php | 6 ++---- src/Schema/Directives/NotInDirective.php | 6 ++---- src/Schema/Directives/ScopeDirective.php | 6 ++---- .../Traits/RelationDirectiveHelpers.php | 17 ++--------------- .../Directives/WhereBetweenDirective.php | 6 ++---- src/Schema/Directives/WhereDirective.php | 8 +++----- .../Directives/WhereJsonContainsDirective.php | 6 ++---- src/Schema/Directives/WhereKeyDirective.php | 7 +++---- .../Directives/WhereNotBetweenDirective.php | 6 ++---- .../Directives/WhereNotNullDirective.php | 8 +++----- src/Schema/Directives/WhereNullDirective.php | 8 +++----- src/Scout/ScoutEnhancer.php | 7 +++---- src/SoftDeletes/TrashedDirective.php | 6 +++--- src/Support/Contracts/ArgBuilderDirective.php | 6 ++---- .../Contracts/FieldBuilderDirective.php | 6 ++---- src/Validation/RulesGatherer.php | 1 - src/WhereConditions/Operator.php | 12 ++---------- src/WhereConditions/SQLOperator.php | 5 ++--- .../WhereConditionsBaseDirective.php | 6 ++---- .../WhereConditionsDirective.php | 6 ++---- src/WhereConditions/WhereConditionsHandler.php | 4 ++-- .../WhereHasConditionsDirective.php | 5 ++--- .../Schema/Directives/BuilderDirectiveTest.php | 9 ++------- 37 files changed, 81 insertions(+), 165 deletions(-) diff --git a/src/Auth/WhereAuthDirective.php b/src/Auth/WhereAuthDirective.php index 0e645d82f5..ffdf98d62d 100644 --- a/src/Auth/WhereAuthDirective.php +++ b/src/Auth/WhereAuthDirective.php @@ -3,9 +3,8 @@ namespace Nuwave\Lighthouse\Auth; use Illuminate\Contracts\Auth\Factory as AuthFactory; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; use Nuwave\Lighthouse\Support\Contracts\FieldBuilderDirective; @@ -38,7 +37,7 @@ public static function definition(): string GRAPHQL; } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { assert($builder instanceof EloquentBuilder); diff --git a/src/Console/PrintSchemaCommand.php b/src/Console/PrintSchemaCommand.php index 6bd7831374..a4388bfcfe 100644 --- a/src/Console/PrintSchemaCommand.php +++ b/src/Console/PrintSchemaCommand.php @@ -5,9 +5,7 @@ use GraphQL\Type\Introspection; use GraphQL\Utils\SchemaPrinter; use Illuminate\Console\Command; -use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Filesystem\FilesystemManager; -use Illuminate\Support\Facades\Storage; use Nuwave\Lighthouse\Federation\FederationPrinter; use Nuwave\Lighthouse\Schema\AST\ASTCache; use Nuwave\Lighthouse\Schema\SchemaBuilder; diff --git a/src/Exceptions/DefinitionException.php b/src/Exceptions/DefinitionException.php index 8d326377e0..ec2ab51354 100644 --- a/src/Exceptions/DefinitionException.php +++ b/src/Exceptions/DefinitionException.php @@ -2,7 +2,6 @@ namespace Nuwave\Lighthouse\Exceptions; -use Exception; use GraphQL\Error\ClientAware; /** diff --git a/src/Execution/ResolveInfo.php b/src/Execution/ResolveInfo.php index ce42d5b73e..407e2cbb07 100644 --- a/src/Execution/ResolveInfo.php +++ b/src/Execution/ResolveInfo.php @@ -3,9 +3,7 @@ namespace Nuwave\Lighthouse\Execution; use GraphQL\Type\Definition\ResolveInfo as BaseResolveInfo; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Support\Collection; use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet; @@ -39,14 +37,14 @@ public function __construct( * * @template TModel of \Illuminate\Database\Eloquent\Model * - * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation|\Laravel\Scout\Builder $builder + * @param \Illuminate\Contracts\Database\Query\Builder|\Laravel\Scout\Builder $builder * @param array $scopes * @param array $args * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter * - * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation|\Laravel\Scout\Builder + * @return \Illuminate\Contracts\Database\Query\Builder|\Laravel\Scout\Builder */ - public function enhanceBuilder(QueryBuilder|EloquentBuilder|Relation|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $directiveFilter = null): QueryBuilder|EloquentBuilder|Relation|ScoutBuilder + public function enhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $directiveFilter = null): Builder|ScoutBuilder { $argumentSet = $resolveInfo->argumentSet; @@ -73,7 +71,7 @@ public function enhanceBuilder(QueryBuilder|EloquentBuilder|Relation|ScoutBuilde * @param array $args * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter */ - public function wouldEnhanceBuilder(QueryBuilder|EloquentBuilder|Relation|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $directiveFilter = null): bool + public function wouldEnhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $directiveFilter = null): bool { $argumentSet = $resolveInfo->argumentSet; @@ -89,7 +87,7 @@ public function wouldEnhanceBuilder(QueryBuilder|EloquentBuilder|Relation|ScoutB * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter */ - protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, QueryBuilder|EloquentBuilder|Relation &$builder, callable $directiveFilter = null): void + protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, Builder &$builder, callable $directiveFilter = null): void { foreach ($argumentSet->arguments as $argument) { $value = $argument->toPlain(); @@ -125,7 +123,7 @@ static function ($value) use (&$builder, $directiveFilter): void { * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter */ - protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSet, QueryBuilder|EloquentBuilder|Relation &$builder, callable $directiveFilter = null): bool + protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSet, Builder &$builder, callable $directiveFilter = null): bool { foreach ($argumentSet->arguments as $argument) { $filteredDirectives = $argument @@ -167,7 +165,7 @@ protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSe * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param array $args */ - protected static function applyFieldBuilderDirectives(QueryBuilder|EloquentBuilder|Relation &$builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): void + protected static function applyFieldBuilderDirectives(Builder &$builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): void { foreach (self::fieldBuilderDirectives($resolveInfo) as $fieldBuilderDirective) { $builder = $fieldBuilderDirective->handleFieldBuilder($builder, $root, $args, $context, $resolveInfo); diff --git a/src/OrderBy/OrderByDirective.php b/src/OrderBy/OrderByDirective.php index d20dfb837d..6894ab4566 100644 --- a/src/OrderBy/OrderByDirective.php +++ b/src/OrderBy/OrderByDirective.php @@ -7,9 +7,8 @@ use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; use GraphQL\Language\Parser; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Nuwave\Lighthouse\Exceptions\DefinitionException; @@ -112,7 +111,7 @@ enum OrderByDirection { } /** @param array> $value */ - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { foreach ($value as $orderByClause) { $order = Arr::pull($orderByClause, 'order'); @@ -260,7 +259,7 @@ public function manipulateArgDefinition( } } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { return $builder->orderBy( $this->directiveArgValue('column'), diff --git a/src/Pagination/PaginateDirective.php b/src/Pagination/PaginateDirective.php index 12331ad97c..33c647aa98 100644 --- a/src/Pagination/PaginateDirective.php +++ b/src/Pagination/PaginateDirective.php @@ -6,10 +6,6 @@ use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; -use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Cache\CacheDirective; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; diff --git a/src/Pagination/PaginationArgs.php b/src/Pagination/PaginationArgs.php index 43b22f35c2..2d41b3d774 100644 --- a/src/Pagination/PaginationArgs.php +++ b/src/Pagination/PaginationArgs.php @@ -3,10 +3,9 @@ namespace Nuwave\Lighthouse\Pagination; use GraphQL\Error\Error; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Laravel\Scout\Builder as ScoutBuilder; @@ -74,13 +73,13 @@ protected static function calculateCurrentPage(int $first, int $after, int $defa /** * Apply the args to a builder, constructing a paginator. * - * @template TModel of \Illuminate\Database\Eloquent\Model + * @template TModel of Model * - * @param \Illuminate\Database\Query\Builder|\Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder + * @param Builder|ScoutBuilder $builder * * @return Paginator */ - public function applyToBuilder(QueryBuilder|ScoutBuilder|EloquentBuilder|Relation $builder): Paginator + public function applyToBuilder(Builder|ScoutBuilder $builder): Paginator { $methodName = $this->type->isSimple() ? 'simplePaginate' diff --git a/src/Schema/Directives/AggregateDirective.php b/src/Schema/Directives/AggregateDirective.php index 5ffa50a90a..abca3bffc2 100644 --- a/src/Schema/Directives/AggregateDirective.php +++ b/src/Schema/Directives/AggregateDirective.php @@ -6,9 +6,8 @@ use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Contracts\Database\Query\Builder as QueryBuilder; use Nuwave\Lighthouse\Exceptions\DefinitionException; use Nuwave\Lighthouse\Execution\BatchLoader\BatchLoaderRegistry; use Nuwave\Lighthouse\Execution\BatchLoader\RelationBatchLoader; @@ -148,7 +147,7 @@ public function resolveField(FieldValue $fieldValue): callable $query = $builderResolver($root, $args, $context, $resolveInfo); assert( - $query instanceof QueryBuilder, + $query instanceof Builder, "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", ); diff --git a/src/Schema/Directives/AllDirective.php b/src/Schema/Directives/AllDirective.php index 9e1947c662..3b566be04e 100644 --- a/src/Schema/Directives/AllDirective.php +++ b/src/Schema/Directives/AllDirective.php @@ -5,12 +5,7 @@ use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Contracts\Database\Query\Builder; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Support\Collection; -use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Schema\Directives\Traits\HasBuilderArgument; diff --git a/src/Schema/Directives/BuilderDirective.php b/src/Schema/Directives/BuilderDirective.php index e9a40c6e14..00afbf9aa5 100644 --- a/src/Schema/Directives/BuilderDirective.php +++ b/src/Schema/Directives/BuilderDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Scout\ScoutBuilderDirective; @@ -42,7 +40,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { $resolver = $this->resolver(); @@ -56,7 +54,7 @@ public function handleScoutBuilder(ScoutBuilder $builder, mixed $value): ScoutBu return $resolver($builder, $value, $this->definitionNode); } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { $resolver = $this->resolver(); diff --git a/src/Schema/Directives/ComplexityDirective.php b/src/Schema/Directives/ComplexityDirective.php index 5c3e08dd3c..addd007a2d 100644 --- a/src/Schema/Directives/ComplexityDirective.php +++ b/src/Schema/Directives/ComplexityDirective.php @@ -2,7 +2,6 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Nuwave\Lighthouse\Pagination\PaginationManipulator; use Nuwave\Lighthouse\Schema\Values\FieldValue; use Nuwave\Lighthouse\Support\Contracts\ComplexityResolverDirective; use Nuwave\Lighthouse\Support\Utils; diff --git a/src/Schema/Directives/EqDirective.php b/src/Schema/Directives/EqDirective.php index c05340b688..12710e355e 100644 --- a/src/Schema/Directives/EqDirective.php +++ b/src/Schema/Directives/EqDirective.php @@ -5,9 +5,7 @@ use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Exceptions\DefinitionException; use Nuwave\Lighthouse\Execution\ResolveInfo; @@ -49,7 +47,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->where( $this->directiveArgValue('key', $this->nodeName()), @@ -76,7 +74,7 @@ public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefini } } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { return $this->handleBuilder( $builder, diff --git a/src/Schema/Directives/InDirective.php b/src/Schema/Directives/InDirective.php index 9e44c855f7..dd9a614ae9 100644 --- a/src/Schema/Directives/InDirective.php +++ b/src/Schema/Directives/InDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; class InDirective extends BaseDirective implements ArgBuilderDirective @@ -25,7 +23,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->whereIn( $this->directiveArgValue('key', $this->nodeName()), diff --git a/src/Schema/Directives/LikeDirective.php b/src/Schema/Directives/LikeDirective.php index b7748fa5ba..bc66c87512 100644 --- a/src/Schema/Directives/LikeDirective.php +++ b/src/Schema/Directives/LikeDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; use Nuwave\Lighthouse\Support\Contracts\FieldBuilderDirective; @@ -52,7 +50,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { if ($value === null) { return $builder; @@ -70,7 +68,7 @@ public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $v ); } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { return $this->handleBuilder( $builder, diff --git a/src/Schema/Directives/NeqDirective.php b/src/Schema/Directives/NeqDirective.php index c4c513388a..6427f7c468 100644 --- a/src/Schema/Directives/NeqDirective.php +++ b/src/Schema/Directives/NeqDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; class NeqDirective extends BaseDirective implements ArgBuilderDirective @@ -25,7 +23,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->where( $this->directiveArgValue('key', $this->nodeName()), diff --git a/src/Schema/Directives/NotInDirective.php b/src/Schema/Directives/NotInDirective.php index bdf8914221..2b62fc3742 100644 --- a/src/Schema/Directives/NotInDirective.php +++ b/src/Schema/Directives/NotInDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; class NotInDirective extends BaseDirective implements ArgBuilderDirective @@ -25,7 +23,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->whereNotIn( $this->directiveArgValue('key', $this->nodeName()), diff --git a/src/Schema/Directives/ScopeDirective.php b/src/Schema/Directives/ScopeDirective.php index cf98d358ee..2e75a60b22 100644 --- a/src/Schema/Directives/ScopeDirective.php +++ b/src/Schema/Directives/ScopeDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Exceptions\DefinitionException; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; @@ -28,7 +26,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { $scope = $this->directiveArgValue('name', $this->nodeName()); diff --git a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php index 1f51950b96..7aec8fb8c5 100644 --- a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php +++ b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives\Traits; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; @@ -25,20 +23,9 @@ protected function relation(): string return $this->directiveArgValue('relation', $this->nodeName()); } - /** - * @param array $args - * - * @return \Closure(QueryBuilder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model>): void - */ protected function makeBuilderDecorator(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): \Closure { - return function (object $builder) use ($root, $args, $context, $resolveInfo): void { - if ($builder instanceof Relation) { - $builder = $builder->getQuery(); - } - - assert($builder instanceof QueryBuilder || $builder instanceof EloquentBuilder); - + return function (Builder $builder) use ($root, $args, $context, $resolveInfo): void { $resolveInfo->enhanceBuilder( $builder, $this->scopes(), diff --git a/src/Schema/Directives/WhereBetweenDirective.php b/src/Schema/Directives/WhereBetweenDirective.php index 612b9e542d..e79644f6c0 100644 --- a/src/Schema/Directives/WhereBetweenDirective.php +++ b/src/Schema/Directives/WhereBetweenDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; class WhereBetweenDirective extends BaseDirective implements ArgBuilderDirective @@ -28,7 +26,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->whereBetween( $this->directiveArgValue('key', $this->nodeName()), diff --git a/src/Schema/Directives/WhereDirective.php b/src/Schema/Directives/WhereDirective.php index 4fb4c5121a..6c5b39cee7 100644 --- a/src/Schema/Directives/WhereDirective.php +++ b/src/Schema/Directives/WhereDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; use Nuwave\Lighthouse\Support\Contracts\FieldBuilderDirective; @@ -50,7 +48,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { // Allow users to overwrite the default "where" clause, e.g. "whereYear" $clause = $this->directiveArgValue('clause', 'where'); @@ -62,7 +60,7 @@ public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $v ); } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { return $this->handleBuilder( $builder, diff --git a/src/Schema/Directives/WhereJsonContainsDirective.php b/src/Schema/Directives/WhereJsonContainsDirective.php index 9f18f1cfd3..4334acf8b4 100644 --- a/src/Schema/Directives/WhereJsonContainsDirective.php +++ b/src/Schema/Directives/WhereJsonContainsDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; class WhereJsonContainsDirective extends BaseDirective implements ArgBuilderDirective @@ -25,7 +23,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->whereJsonContains( $this->directiveArgValue('key', $this->nodeName()), diff --git a/src/Schema/Directives/WhereKeyDirective.php b/src/Schema/Directives/WhereKeyDirective.php index c6498560a0..a7df66c191 100644 --- a/src/Schema/Directives/WhereKeyDirective.php +++ b/src/Schema/Directives/WhereKeyDirective.php @@ -5,9 +5,8 @@ use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; use Nuwave\Lighthouse\Exceptions\DefinitionException; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Schema\AST\DocumentAST; @@ -39,7 +38,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { if (! $builder instanceof EloquentBuilder) { $notEloquentBuilder = $builder::class; @@ -56,7 +55,7 @@ public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefini } } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { return $this->handleBuilder( $builder, diff --git a/src/Schema/Directives/WhereNotBetweenDirective.php b/src/Schema/Directives/WhereNotBetweenDirective.php index 3ba9223555..57ce3b28cc 100644 --- a/src/Schema/Directives/WhereNotBetweenDirective.php +++ b/src/Schema/Directives/WhereNotBetweenDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; class WhereNotBetweenDirective extends BaseDirective implements ArgBuilderDirective @@ -28,7 +26,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->whereNotBetween( $this->directiveArgValue('key', $this->nodeName()), diff --git a/src/Schema/Directives/WhereNotNullDirective.php b/src/Schema/Directives/WhereNotNullDirective.php index 6f97a55126..c6f18cd929 100644 --- a/src/Schema/Directives/WhereNotNullDirective.php +++ b/src/Schema/Directives/WhereNotNullDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; use Nuwave\Lighthouse\Support\Contracts\FieldBuilderDirective; @@ -34,7 +32,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { if ($value === null) { return $builder; @@ -47,7 +45,7 @@ public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $v ); } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { return $this->handleBuilder( $builder, diff --git a/src/Schema/Directives/WhereNullDirective.php b/src/Schema/Directives/WhereNullDirective.php index d0f6990502..1bc1c7d5eb 100644 --- a/src/Schema/Directives/WhereNullDirective.php +++ b/src/Schema/Directives/WhereNullDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; use Nuwave\Lighthouse\Support\Contracts\FieldBuilderDirective; @@ -34,7 +32,7 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { if ($value === null) { return $builder; @@ -47,7 +45,7 @@ public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $v ); } - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder { return $this->handleBuilder( $builder, diff --git a/src/Scout/ScoutEnhancer.php b/src/Scout/ScoutEnhancer.php index 17a278f5e1..5d7972482b 100644 --- a/src/Scout/ScoutEnhancer.php +++ b/src/Scout/ScoutEnhancer.php @@ -2,9 +2,8 @@ namespace Nuwave\Lighthouse\Scout; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; use Laravel\Scout\Builder as ScoutBuilder; use Laravel\Scout\Searchable; use Nuwave\Lighthouse\Execution\Arguments\Argument; @@ -41,9 +40,9 @@ class ScoutEnhancer public function __construct( protected ArgumentSet $argumentSet, /** - * @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation|\Laravel\Scout\Builder $builder + * @var \Illuminate\Contracts\Database\Query\Builder|\Laravel\Scout\Builder $builder */ - protected QueryBuilder|EloquentBuilder|Relation|ScoutBuilder $builder, + protected Builder|ScoutBuilder $builder, ) { $this->gather($this->argumentSet); } diff --git a/src/SoftDeletes/TrashedDirective.php b/src/SoftDeletes/TrashedDirective.php index dc95329a77..142002a8d9 100644 --- a/src/SoftDeletes/TrashedDirective.php +++ b/src/SoftDeletes/TrashedDirective.php @@ -3,10 +3,10 @@ namespace Nuwave\Lighthouse\SoftDeletes; use GraphQL\Error\Error; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; use Nuwave\Lighthouse\Scout\ScoutBuilderDirective; @@ -26,9 +26,9 @@ public static function definition(): string GRAPHQL; } - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { - if (! $builder instanceof EloquentBuilder) { + if (! $builder instanceof EloquentBuilder && ! $builder instanceof Relation) { $notEloquentBuilder = $builder::class; throw new \Exception("Can not get model from builder of class: {$notEloquentBuilder}"); } diff --git a/src/Support/Contracts/ArgBuilderDirective.php b/src/Support/Contracts/ArgBuilderDirective.php index 8204ee1af8..bca2a0bc2a 100644 --- a/src/Support/Contracts/ArgBuilderDirective.php +++ b/src/Support/Contracts/ArgBuilderDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Support\Contracts; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; interface ArgBuilderDirective extends Directive { @@ -18,5 +16,5 @@ interface ArgBuilderDirective extends Directive * * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> the modified builder */ - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $value): QueryBuilder|EloquentBuilder|Relation; + public function handleBuilder(Builder $builder, mixed $value): Builder; } diff --git a/src/Support/Contracts/FieldBuilderDirective.php b/src/Support/Contracts/FieldBuilderDirective.php index 09d65bb499..7a683589cc 100644 --- a/src/Support/Contracts/FieldBuilderDirective.php +++ b/src/Support/Contracts/FieldBuilderDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\Support\Contracts; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Execution\ResolveInfo; interface FieldBuilderDirective extends Directive @@ -19,5 +17,5 @@ interface FieldBuilderDirective extends Directive * * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> the modified builder */ - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation; + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder; } diff --git a/src/Validation/RulesGatherer.php b/src/Validation/RulesGatherer.php index ad11f2e0d5..d4c29121d5 100644 --- a/src/Validation/RulesGatherer.php +++ b/src/Validation/RulesGatherer.php @@ -2,7 +2,6 @@ namespace Nuwave\Lighthouse\Validation; -use Illuminate\Contracts\Validation\Rule; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Validation\ValidationRuleParser; diff --git a/src/WhereConditions/Operator.php b/src/WhereConditions/Operator.php index 7969f21464..400d107a3c 100644 --- a/src/WhereConditions/Operator.php +++ b/src/WhereConditions/Operator.php @@ -2,8 +2,7 @@ namespace Nuwave\Lighthouse\WhereConditions; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; /** * An Operator handles the database or application specific bits @@ -30,13 +29,6 @@ public function defaultHasOperator(): string; /** * Apply the conditions to the query builder. - * - * @template TModel of \Illuminate\Database\Eloquent\Model - * - * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder - * @param array $whereConditions - * - * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder */ - public function applyConditions(QueryBuilder|EloquentBuilder $builder, array $whereConditions, string $boolean): QueryBuilder|EloquentBuilder; + public function applyConditions(Builder $builder, array $whereConditions, string $boolean): Builder; } diff --git a/src/WhereConditions/SQLOperator.php b/src/WhereConditions/SQLOperator.php index cb6b244e9f..68175eeaf0 100644 --- a/src/WhereConditions/SQLOperator.php +++ b/src/WhereConditions/SQLOperator.php @@ -3,8 +3,7 @@ namespace Nuwave\Lighthouse\WhereConditions; use GraphQL\Error\Error; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; class SQLOperator implements Operator { @@ -73,7 +72,7 @@ public function defaultHasOperator(): string return 'GTE'; } - public function applyConditions(QueryBuilder|EloquentBuilder $builder, array $whereConditions, string $boolean): QueryBuilder|EloquentBuilder + public function applyConditions(Builder $builder, array $whereConditions, string $boolean): Builder { $column = $whereConditions['column']; diff --git a/src/WhereConditions/WhereConditionsBaseDirective.php b/src/WhereConditions/WhereConditionsBaseDirective.php index ca608782e0..88c0dd7f08 100644 --- a/src/WhereConditions/WhereConditionsBaseDirective.php +++ b/src/WhereConditions/WhereConditionsBaseDirective.php @@ -8,9 +8,7 @@ use GraphQL\Language\AST\ObjectTypeDefinitionNode; use GraphQL\Language\Parser; use Illuminate\Container\Container; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Schema\AST\ASTHelper; use Nuwave\Lighthouse\Schema\AST\DocumentAST; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; @@ -26,7 +24,7 @@ abstract class WhereConditionsBaseDirective extends BaseDirective implements Arg * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder the builder used to resolve the field * @param array $value the client given value of the argument */ - protected function handle(QueryBuilder|EloquentBuilder|Relation $builder, array $value): void + protected function handle(Builder $builder, array $value): void { $handler = $this->directiveHasArgument('handler') ? $this->getResolverFromArgument('handler') diff --git a/src/WhereConditions/WhereConditionsDirective.php b/src/WhereConditions/WhereConditionsDirective.php index 16526d14e5..aea773c0ef 100644 --- a/src/WhereConditions/WhereConditionsDirective.php +++ b/src/WhereConditions/WhereConditionsDirective.php @@ -2,9 +2,7 @@ namespace Nuwave\Lighthouse\WhereConditions; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; class WhereConditionsDirective extends WhereConditionsBaseDirective { @@ -46,7 +44,7 @@ public static function definition(): string } /** @param array|null $value */ - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { if ($value === null) { return $builder; diff --git a/src/WhereConditions/WhereConditionsHandler.php b/src/WhereConditions/WhereConditionsHandler.php index 0dd914956c..b431ae53c2 100644 --- a/src/WhereConditions/WhereConditionsHandler.php +++ b/src/WhereConditions/WhereConditionsHandler.php @@ -3,9 +3,9 @@ namespace Nuwave\Lighthouse\WhereConditions; use GraphQL\Error\Error; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Query\Builder as QueryBuilder; class WhereConditionsHandler { @@ -79,7 +79,7 @@ public function handleHasCondition( string $operator, int $amount, ?array $condition = null, - ): QueryBuilder { + ): Builder { return $model ->newQuery() ->whereHas( diff --git a/src/WhereConditions/WhereHasConditionsDirective.php b/src/WhereConditions/WhereHasConditionsDirective.php index af4e29f4cb..ab211a1970 100644 --- a/src/WhereConditions/WhereHasConditionsDirective.php +++ b/src/WhereConditions/WhereHasConditionsDirective.php @@ -2,9 +2,8 @@ namespace Nuwave\Lighthouse\WhereConditions; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Support\Str; class WhereHasConditionsDirective extends WhereConditionsBaseDirective @@ -57,7 +56,7 @@ public static function definition(): string } /** @param array|null $value The client given conditions */ - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { if ($value === null) { return $builder; diff --git a/tests/Integration/Schema/Directives/BuilderDirectiveTest.php b/tests/Integration/Schema/Directives/BuilderDirectiveTest.php index df3679ce88..f790dfcea7 100644 --- a/tests/Integration/Schema/Directives/BuilderDirectiveTest.php +++ b/tests/Integration/Schema/Directives/BuilderDirectiveTest.php @@ -3,7 +3,7 @@ namespace Tests\Integration\Schema\Directives; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; use Tests\DBTestCase; @@ -152,12 +152,7 @@ public function testCallsCustomBuilderMethodOnFieldWithoutValue(): void ')->assertJsonCount(2, 'data.users'); } - /** - * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Tests\Utils\Models\User> $builder - * - * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Tests\Utils\Models\User> - */ - public static function limit(QueryBuilder|EloquentBuilder $builder, ?int $value): QueryBuilder|EloquentBuilder + public static function limit(Builder $builder, ?int $value): Builder { return $builder->limit($value ?? 2); } From 885a084627d0e06ed8f23d74d79ee6af08f61277 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Fri, 28 Apr 2023 16:09:32 +0200 Subject: [PATCH 05/19] fix phpstan issues --- src/Execution/ResolveInfo.php | 2 -- src/Pagination/PaginationArgs.php | 5 +---- src/Schema/Directives/Traits/HasBuilderArgument.php | 4 ++++ src/Schema/Directives/Traits/RelationDirectiveHelpers.php | 3 +++ src/WhereConditions/Operator.php | 2 ++ 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Execution/ResolveInfo.php b/src/Execution/ResolveInfo.php index 407e2cbb07..9bb9d67ed4 100644 --- a/src/Execution/ResolveInfo.php +++ b/src/Execution/ResolveInfo.php @@ -35,8 +35,6 @@ public function __construct( /** * Apply ArgBuilderDirectives and scopes to the builder. * - * @template TModel of \Illuminate\Database\Eloquent\Model - * * @param \Illuminate\Contracts\Database\Query\Builder|\Laravel\Scout\Builder $builder * @param array $scopes * @param array $args diff --git a/src/Pagination/PaginationArgs.php b/src/Pagination/PaginationArgs.php index 2d41b3d774..b9bb6b735b 100644 --- a/src/Pagination/PaginationArgs.php +++ b/src/Pagination/PaginationArgs.php @@ -73,11 +73,8 @@ protected static function calculateCurrentPage(int $first, int $after, int $defa /** * Apply the args to a builder, constructing a paginator. * - * @template TModel of Model - * * @param Builder|ScoutBuilder $builder - * - * @return Paginator + * @return Paginator */ public function applyToBuilder(Builder|ScoutBuilder $builder): Paginator { diff --git a/src/Schema/Directives/Traits/HasBuilderArgument.php b/src/Schema/Directives/Traits/HasBuilderArgument.php index c19164bf22..5976cfe17e 100644 --- a/src/Schema/Directives/Traits/HasBuilderArgument.php +++ b/src/Schema/Directives/Traits/HasBuilderArgument.php @@ -9,6 +9,10 @@ trait HasBuilderArgument { + /** + * @param array $args + * @throws \Nuwave\Lighthouse\Exceptions\DefinitionException + */ private function getBuilder(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder|ScoutBuilder { if (!$this->directiveHasArgument('builder')) { diff --git a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php index 7aec8fb8c5..76adb2d0d5 100644 --- a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php +++ b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php @@ -23,6 +23,9 @@ protected function relation(): string return $this->directiveArgValue('relation', $this->nodeName()); } + /** + * @param array $args + */ protected function makeBuilderDecorator(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): \Closure { return function (Builder $builder) use ($root, $args, $context, $resolveInfo): void { diff --git a/src/WhereConditions/Operator.php b/src/WhereConditions/Operator.php index 400d107a3c..64abfd61ad 100644 --- a/src/WhereConditions/Operator.php +++ b/src/WhereConditions/Operator.php @@ -29,6 +29,8 @@ public function defaultHasOperator(): string; /** * Apply the conditions to the query builder. + * + * @param array $whereConditions */ public function applyConditions(Builder $builder, array $whereConditions, string $boolean): Builder; } From bdd34f27c5b641ee8109fc99ee16ce3bf37e5493 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Fri, 28 Apr 2023 16:38:11 +0200 Subject: [PATCH 06/19] document ArgBuilderDirective to use the common builder interface --- docs/master/custom-directives/argument-directives.md | 6 ++---- src/Support/Contracts/ArgBuilderDirective.php | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/docs/master/custom-directives/argument-directives.md b/docs/master/custom-directives/argument-directives.md index fd4179ce71..07ad8ddb28 100644 --- a/docs/master/custom-directives/argument-directives.md +++ b/docs/master/custom-directives/argument-directives.md @@ -145,9 +145,7 @@ where the `category` column is equal to the value of the `category` argument. So let's take a look at a simplified version of the built-in [@eq](../api-reference/directives.md#eq) directive. ```php -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Database\Query\Builder; use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective; class EqDirective extends BaseDirective implements ArgBuilderDirective @@ -171,7 +169,7 @@ GRAPHQL; /** * Apply a "WHERE = $value" clause. */ - public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation + public function handleBuilder(Builder $builder, $value): Builder { return $builder->where( $this->directiveArgValue('key', $this->nodeName()), diff --git a/src/Support/Contracts/ArgBuilderDirective.php b/src/Support/Contracts/ArgBuilderDirective.php index bca2a0bc2a..7f1aad0c78 100644 --- a/src/Support/Contracts/ArgBuilderDirective.php +++ b/src/Support/Contracts/ArgBuilderDirective.php @@ -11,10 +11,10 @@ interface ArgBuilderDirective extends Directive * * TODO try adding a generic type parameter for the type of model when PHPStan handles it better * - * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder the builder used to resolve the field - * @param mixed $value the client given value of the argument + * @param Builder $builder the builder used to resolve the field + * @param mixed $value the client given value of the argument * - * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> the modified builder + * @return Builder the modified builder */ public function handleBuilder(Builder $builder, mixed $value): Builder; } From cc517016c8e18b216b9adf75d1b1434378809faa Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Fri, 28 Apr 2023 16:38:35 +0200 Subject: [PATCH 07/19] get the eloquent builder from a relation --- src/SoftDeletes/TrashedDirective.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/SoftDeletes/TrashedDirective.php b/src/SoftDeletes/TrashedDirective.php index 142002a8d9..392800b08e 100644 --- a/src/SoftDeletes/TrashedDirective.php +++ b/src/SoftDeletes/TrashedDirective.php @@ -28,7 +28,11 @@ public static function definition(): string public function handleBuilder(Builder $builder, $value): Builder { - if (! $builder instanceof EloquentBuilder && ! $builder instanceof Relation) { + if ($builder instanceof Relation) { + $builder = $builder->getQuery(); + } + + if (! $builder instanceof EloquentBuilder) { $notEloquentBuilder = $builder::class; throw new \Exception("Can not get model from builder of class: {$notEloquentBuilder}"); } From 3c41e2cb05d619bb2dc5e7650a8ec24161d68d8e Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Fri, 28 Apr 2023 16:41:13 +0200 Subject: [PATCH 08/19] document changes --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2588451989..1d299e239e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co ## Unreleased +### Changed + +- Use common Builder interface https://github.com/nuwave/lighthouse/pull/2389 + ## v6.8.0 ### Added From c0da959abad737d4b32f361f0fe8de0f80f2f0b2 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Sat, 29 Apr 2023 21:56:02 +0200 Subject: [PATCH 09/19] revert check for relation --- src/Schema/Directives/Traits/RelationDirectiveHelpers.php | 5 +++++ src/SoftDeletes/TrashedDirective.php | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php index 76adb2d0d5..1c7d0e609b 100644 --- a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php +++ b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php @@ -3,6 +3,7 @@ namespace Nuwave\Lighthouse\Schema\Directives\Traits; use Illuminate\Contracts\Database\Query\Builder; +use Illuminate\Database\Eloquent\Relations\Relation; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; @@ -29,6 +30,10 @@ protected function relation(): string protected function makeBuilderDecorator(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): \Closure { return function (Builder $builder) use ($root, $args, $context, $resolveInfo): void { + if ($builder instanceof Relation) { + $builder = $builder->getQuery(); + } + $resolveInfo->enhanceBuilder( $builder, $this->scopes(), diff --git a/src/SoftDeletes/TrashedDirective.php b/src/SoftDeletes/TrashedDirective.php index 392800b08e..54c01202fc 100644 --- a/src/SoftDeletes/TrashedDirective.php +++ b/src/SoftDeletes/TrashedDirective.php @@ -28,10 +28,6 @@ public static function definition(): string public function handleBuilder(Builder $builder, $value): Builder { - if ($builder instanceof Relation) { - $builder = $builder->getQuery(); - } - if (! $builder instanceof EloquentBuilder) { $notEloquentBuilder = $builder::class; throw new \Exception("Can not get model from builder of class: {$notEloquentBuilder}"); From 9e2bd151c861f45432b0d0a982726014390cc5b9 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Tue, 2 May 2023 21:07:48 +0200 Subject: [PATCH 10/19] run php cs fixer --- src/Console/PrintSchemaCommand.php | 1 + src/Execution/ResolveInfo.php | 9 +++----- src/Pagination/PaginationArgs.php | 1 - src/Schema/Directives/AllDirective.php | 22 +++++++++---------- .../Directives/Traits/HasBuilderArgument.php | 9 +++----- .../Traits/RelationDirectiveHelpers.php | 4 +--- src/Schema/ResolverProvider.php | 2 +- src/SoftDeletes/TrashedDirective.php | 1 - .../Directives/BuilderDirectiveTest.php | 2 +- 9 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/Console/PrintSchemaCommand.php b/src/Console/PrintSchemaCommand.php index a4388bfcfe..a56985c1be 100644 --- a/src/Console/PrintSchemaCommand.php +++ b/src/Console/PrintSchemaCommand.php @@ -57,6 +57,7 @@ public function handle(ASTCache $cache, FilesystemManager $filesystemManager, Sc if (! is_string($disk) && ! is_null($disk)) { $diskType = gettype($disk); $this->error("Expected option disk to be string or null, got: {$diskType}."); + return; } diff --git a/src/Execution/ResolveInfo.php b/src/Execution/ResolveInfo.php index 9bb9d67ed4..23d86ab80e 100644 --- a/src/Execution/ResolveInfo.php +++ b/src/Execution/ResolveInfo.php @@ -35,12 +35,9 @@ public function __construct( /** * Apply ArgBuilderDirectives and scopes to the builder. * - * @param \Illuminate\Contracts\Database\Query\Builder|\Laravel\Scout\Builder $builder * @param array $scopes * @param array $args * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter - * - * @return \Illuminate\Contracts\Database\Query\Builder|\Laravel\Scout\Builder */ public function enhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $directiveFilter = null): Builder|ScoutBuilder { @@ -85,7 +82,7 @@ public function wouldEnhanceBuilder(Builder|ScoutBuilder $builder, array $scopes * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter */ - protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, Builder &$builder, callable $directiveFilter = null): void + protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation &$builder, callable $directiveFilter = null): void { foreach ($argumentSet->arguments as $argument) { $value = $argument->toPlain(); @@ -121,7 +118,7 @@ static function ($value) use (&$builder, $directiveFilter): void { * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter */ - protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSet, Builder &$builder, callable $directiveFilter = null): bool + protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSet, \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation &$builder, callable $directiveFilter = null): bool { foreach ($argumentSet->arguments as $argument) { $filteredDirectives = $argument @@ -163,7 +160,7 @@ protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSe * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param array $args */ - protected static function applyFieldBuilderDirectives(Builder &$builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): void + protected static function applyFieldBuilderDirectives(\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation &$builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): void { foreach (self::fieldBuilderDirectives($resolveInfo) as $fieldBuilderDirective) { $builder = $fieldBuilderDirective->handleFieldBuilder($builder, $root, $args, $context, $resolveInfo); diff --git a/src/Pagination/PaginationArgs.php b/src/Pagination/PaginationArgs.php index b9bb6b735b..fee52f06ef 100644 --- a/src/Pagination/PaginationArgs.php +++ b/src/Pagination/PaginationArgs.php @@ -73,7 +73,6 @@ protected static function calculateCurrentPage(int $first, int $after, int $defa /** * Apply the args to a builder, constructing a paginator. * - * @param Builder|ScoutBuilder $builder * @return Paginator */ public function applyToBuilder(Builder|ScoutBuilder $builder): Paginator diff --git a/src/Schema/Directives/AllDirective.php b/src/Schema/Directives/AllDirective.php index 3b566be04e..1bb6c5e471 100644 --- a/src/Schema/Directives/AllDirective.php +++ b/src/Schema/Directives/AllDirective.php @@ -50,18 +50,16 @@ public static function definition(): string public function resolveField(FieldValue $fieldValue): callable { - return function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection { - return $resolveInfo - ->enhanceBuilder( - $this->getBuilder($root, $args, $context, $resolveInfo), - $this->directiveArgValue('scopes', []), - $root, - $args, - $context, - $resolveInfo, - ) - ->get(); - }; + return fn (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection => $resolveInfo + ->enhanceBuilder( + $this->getBuilder($root, $args, $context, $resolveInfo), + $this->directiveArgValue('scopes', []), + $root, + $args, + $context, + $resolveInfo, + ) + ->get(); } public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType): void diff --git a/src/Schema/Directives/Traits/HasBuilderArgument.php b/src/Schema/Directives/Traits/HasBuilderArgument.php index 5976cfe17e..82920a8a29 100644 --- a/src/Schema/Directives/Traits/HasBuilderArgument.php +++ b/src/Schema/Directives/Traits/HasBuilderArgument.php @@ -1,4 +1,4 @@ - $args - * @throws \Nuwave\Lighthouse\Exceptions\DefinitionException - */ + /** @param array $args */ private function getBuilder(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder|ScoutBuilder { - if (!$this->directiveHasArgument('builder')) { + if (! $this->directiveHasArgument('builder')) { return $this->getModelClass()::query(); } diff --git a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php index 1c7d0e609b..368a86e996 100644 --- a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php +++ b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php @@ -24,9 +24,7 @@ protected function relation(): string return $this->directiveArgValue('relation', $this->nodeName()); } - /** - * @param array $args - */ + /** @param array $args */ protected function makeBuilderDecorator(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): \Closure { return function (Builder $builder) use ($root, $args, $context, $resolveInfo): void { diff --git a/src/Schema/ResolverProvider.php b/src/Schema/ResolverProvider.php index eace20079d..4da836922c 100644 --- a/src/Schema/ResolverProvider.php +++ b/src/Schema/ResolverProvider.php @@ -26,7 +26,7 @@ public function provideResolver(FieldValue $fieldValue): \Closure // Return any non-null value to continue nested field resolution // when the root Query type is returned as part of the result. if (ASTHelper::getUnderlyingTypeName($fieldValue->getField()) === RootType::QUERY) { - return static fn () => true; + return static fn (): bool => true; } return \Closure::fromCallable( diff --git a/src/SoftDeletes/TrashedDirective.php b/src/SoftDeletes/TrashedDirective.php index 54c01202fc..b9e41a7011 100644 --- a/src/SoftDeletes/TrashedDirective.php +++ b/src/SoftDeletes/TrashedDirective.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\Relation; use Laravel\Scout\Builder as ScoutBuilder; use Nuwave\Lighthouse\Schema\Directives\BaseDirective; use Nuwave\Lighthouse\Scout\ScoutBuilderDirective; diff --git a/tests/Integration/Schema/Directives/BuilderDirectiveTest.php b/tests/Integration/Schema/Directives/BuilderDirectiveTest.php index f790dfcea7..7041ee4fd4 100644 --- a/tests/Integration/Schema/Directives/BuilderDirectiveTest.php +++ b/tests/Integration/Schema/Directives/BuilderDirectiveTest.php @@ -2,8 +2,8 @@ namespace Tests\Integration\Schema\Directives; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Contracts\Database\Query\Builder; +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; use Tests\DBTestCase; From cbde5a7f5bb34451adc9663117787589a7ba1291 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Tue, 2 May 2023 21:11:49 +0200 Subject: [PATCH 11/19] rename get to make to distinguish between getter methods --- src/Pagination/PaginateDirective.php | 2 +- src/Schema/Directives/AllDirective.php | 2 +- src/Schema/Directives/Traits/HasBuilderArgument.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Pagination/PaginateDirective.php b/src/Pagination/PaginateDirective.php index 33c647aa98..25eeff34ac 100644 --- a/src/Pagination/PaginateDirective.php +++ b/src/Pagination/PaginateDirective.php @@ -152,7 +152,7 @@ public function resolveField(FieldValue $fieldValue): callable } $query = $resolveInfo->enhanceBuilder( - $this->getBuilder($root, $args, $context, $resolveInfo), + $this->makeBuilder($root, $args, $context, $resolveInfo), $this->directiveArgValue('scopes', []), $root, $args, diff --git a/src/Schema/Directives/AllDirective.php b/src/Schema/Directives/AllDirective.php index 1bb6c5e471..baa37754ea 100644 --- a/src/Schema/Directives/AllDirective.php +++ b/src/Schema/Directives/AllDirective.php @@ -52,7 +52,7 @@ public function resolveField(FieldValue $fieldValue): callable { return fn (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection => $resolveInfo ->enhanceBuilder( - $this->getBuilder($root, $args, $context, $resolveInfo), + $this->makeBuilder($root, $args, $context, $resolveInfo), $this->directiveArgValue('scopes', []), $root, $args, diff --git a/src/Schema/Directives/Traits/HasBuilderArgument.php b/src/Schema/Directives/Traits/HasBuilderArgument.php index 82920a8a29..c9c1e68517 100644 --- a/src/Schema/Directives/Traits/HasBuilderArgument.php +++ b/src/Schema/Directives/Traits/HasBuilderArgument.php @@ -10,7 +10,7 @@ trait HasBuilderArgument { /** @param array $args */ - private function getBuilder(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder|ScoutBuilder + private function makeBuilder(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder|ScoutBuilder { if (! $this->directiveHasArgument('builder')) { return $this->getModelClass()::query(); From 65ac23d7295520a1e6f0e59f46f0e22eedb9fccc Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Tue, 2 May 2023 21:18:32 +0200 Subject: [PATCH 12/19] document builder interface change for v7 upgrade --- UPGRADE.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/UPGRADE.md b/UPGRADE.md index 840c4ce029..4169faafd5 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -24,6 +24,16 @@ It will prevent the following type of HTTP requests: - `GET` requests - `POST` requests that can be created using HTML forms +### Type hinting query builders + +Lighthouse now uses `Illuminate\Contracts\Database\Query\Builder` to type hint database query builders. +If you implement `ArgBuilderDirective` or `FieldBuilderDirective`, you will have to change the signature of the `handleFieldBuilder` method accordingly: + +```diff +- public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation; ++ public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder; +``` + ## v5 to v6 ### `messages` on `@rules` and `@rulesForArray` From 6afce2778d481476aaeb5fb6519d0be25cbc6287 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Tue, 2 May 2023 21:33:45 +0200 Subject: [PATCH 13/19] add spaces to comments for code review purposes --- src/Deprecation/DetectDeprecatedUsage.php | 4 ++-- src/Execution/Arguments/ArgPartitioner.php | 2 +- src/Federation/BatchedEntityResolver.php | 2 +- src/Federation/EntityResolverProvider.php | 2 +- src/GraphQL.php | 4 ++-- src/Schema/AST/ASTHelper.php | 2 +- src/Schema/AST/ExecutableTypeNodeConverter.php | 4 ++-- src/Schema/AST/FallbackTypeNodeConverter.php | 2 +- src/Schema/Directives/BaseDirective.php | 4 ++-- src/Schema/Directives/Traits/HasBuilderArgument.php | 3 ++- src/Schema/Directives/Traits/RelationDirectiveHelpers.php | 2 +- src/Schema/TypeRegistry.php | 4 ++-- src/Support/Contracts/ArgBuilderDirective.php | 4 ++-- src/Validation/Validator.php | 2 +- src/WhereConditions/Operator.php | 2 +- .../Schema/Directives/AggregateDirectiveTest.php | 2 +- .../Subscriptions/Storage/RedisStorageManagerTest.php | 2 +- tests/Unit/Pagination/PaginationTypeTest.php | 2 +- .../Subscriptions/Broadcasters/PusherBroadcasterTest.php | 2 +- tests/Utils/Models/Task.php | 6 +++--- tests/Utils/Models/User.php | 6 +++--- tests/Utils/Models/WithEnum.php | 2 +- 22 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/Deprecation/DetectDeprecatedUsage.php b/src/Deprecation/DetectDeprecatedUsage.php index e78942f974..77bf102b8d 100644 --- a/src/Deprecation/DetectDeprecatedUsage.php +++ b/src/Deprecation/DetectDeprecatedUsage.php @@ -25,13 +25,13 @@ class DetectDeprecatedUsage extends ValidationRule /** @var DeprecationHandler */ protected $deprecationHandler; - /** @param DeprecationHandler $deprecationHandler */ + /** @param DeprecationHandler $deprecationHandler */ public function __construct(callable $deprecationHandler) { $this->deprecationHandler = $deprecationHandler; } - /** @param DeprecationHandler $deprecationHandler */ + /** @param DeprecationHandler $deprecationHandler */ public static function handle(callable $deprecationHandler): void { DocumentValidator::addRule(new static($deprecationHandler)); diff --git a/src/Execution/Arguments/ArgPartitioner.php b/src/Execution/Arguments/ArgPartitioner.php index b5ec1818d7..3cfd836f58 100644 --- a/src/Execution/Arguments/ArgPartitioner.php +++ b/src/Execution/Arguments/ArgPartitioner.php @@ -85,7 +85,7 @@ public static function relationMethods( /** * Attach a nested argument resolver to an argument. * - * @param \ReflectionClass<\Illuminate\Database\Eloquent\Model>|null $model + * @param \ReflectionClass<\Illuminate\Database\Eloquent\Model>|null $model */ protected static function attachNestedArgResolver(string $name, Argument &$argument, ?\ReflectionClass $model): void { diff --git a/src/Federation/BatchedEntityResolver.php b/src/Federation/BatchedEntityResolver.php index 167e7840d7..aa49c56a41 100644 --- a/src/Federation/BatchedEntityResolver.php +++ b/src/Federation/BatchedEntityResolver.php @@ -7,7 +7,7 @@ interface BatchedEntityResolver /** * Resolve multiple entities of a single type in one batch. * - * @param array> $representations + * @param array> $representations * * @return iterable must preserve the count and keys of $representations */ diff --git a/src/Federation/EntityResolverProvider.php b/src/Federation/EntityResolverProvider.php index 2584f4f490..4485e2b93f 100644 --- a/src/Federation/EntityResolverProvider.php +++ b/src/Federation/EntityResolverProvider.php @@ -261,7 +261,7 @@ public function firstSatisfiedKeyFields(Collection $keyFieldsSelections, array $ ?? throw new Error('Representation does not satisfy any set of uniquely identifying keys: ' . \Safe\json_encode($representation)); } - /** @param array $representation */ + /** @param array $representation */ protected function hydrateExternalFields(Model $model, array $representation, ObjectTypeDefinitionNode $definition): void { foreach ($definition->fields as $field) { diff --git a/src/GraphQL.php b/src/GraphQL.php index 96afd811f3..daabc4fb15 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -64,7 +64,7 @@ public function __construct( * * @api * - * @param array|null $variables + * @param array|null $variables * * @return array */ @@ -95,7 +95,7 @@ public function executeQueryString( * you will probably want to call `->toArray($debug)` on it, * with $debug being a combination of flags in @see \GraphQL\Error\DebugFlag * - * @param array|null $variables + * @param array|null $variables * * @return array */ diff --git a/src/Schema/AST/ASTHelper.php b/src/Schema/AST/ASTHelper.php index a314e834cb..b071ed5b81 100644 --- a/src/Schema/AST/ASTHelper.php +++ b/src/Schema/AST/ASTHelper.php @@ -139,7 +139,7 @@ public static function getUnderlyingNamedTypeNode(Node $node): NamedTypeNode /** * Extract a named argument from a given directive node. * - * @param mixed $default is returned if the directive does not have the argument + * @param mixed $default is returned if the directive does not have the argument */ public static function directiveArgValue(DirectiveNode $directive, string $name, mixed $default = null): mixed { diff --git a/src/Schema/AST/ExecutableTypeNodeConverter.php b/src/Schema/AST/ExecutableTypeNodeConverter.php index cb055bff9e..a2d805466a 100644 --- a/src/Schema/AST/ExecutableTypeNodeConverter.php +++ b/src/Schema/AST/ExecutableTypeNodeConverter.php @@ -13,7 +13,7 @@ public function __construct( protected TypeRegistry $typeRegistry, ) {} - /** @param \GraphQL\Type\Definition\Type&\GraphQL\Type\Definition\NullableType $type */ + /** @param \GraphQL\Type\Definition\Type&\GraphQL\Type\Definition\NullableType $type */ protected function nonNull(mixed $type): NonNull { return Type::nonNull($type); @@ -22,7 +22,7 @@ protected function nonNull(mixed $type): NonNull /** * @template T of \GraphQL\Type\Definition\Type * - * @param T|callable():T $type + * @param T|callable():T $type * * @return \GraphQL\Type\Definition\ListOfType */ diff --git a/src/Schema/AST/FallbackTypeNodeConverter.php b/src/Schema/AST/FallbackTypeNodeConverter.php index f6fabfcf40..a36eb80d51 100644 --- a/src/Schema/AST/FallbackTypeNodeConverter.php +++ b/src/Schema/AST/FallbackTypeNodeConverter.php @@ -22,7 +22,7 @@ protected function nonNull(mixed $type): NonNull /** * @template T of Type * - * @param T|callable():T $type + * @param T|callable():T $type * * @return ListOfType */ diff --git a/src/Schema/Directives/BaseDirective.php b/src/Schema/Directives/BaseDirective.php index f76cc50265..84ec3ca41c 100644 --- a/src/Schema/Directives/BaseDirective.php +++ b/src/Schema/Directives/BaseDirective.php @@ -120,7 +120,7 @@ protected function directiveHasArgument(string $name): bool * * @api * - * @param mixed $default Use this over `??` to preserve explicit `null` + * @param mixed $default Use this over `??` to preserve explicit `null` * * @return mixed The argument value or the default */ @@ -257,7 +257,7 @@ protected function namespaceModelClass(string $modelClassCandidate): string * * @api * - * @param array $names + * @param array $names */ protected function validateMutuallyExclusiveArguments(array $names): void { diff --git a/src/Schema/Directives/Traits/HasBuilderArgument.php b/src/Schema/Directives/Traits/HasBuilderArgument.php index c9c1e68517..a014228d9b 100644 --- a/src/Schema/Directives/Traits/HasBuilderArgument.php +++ b/src/Schema/Directives/Traits/HasBuilderArgument.php @@ -4,12 +4,13 @@ use Illuminate\Contracts\Database\Query\Builder; use Laravel\Scout\Builder as ScoutBuilder; +use Nuwave\Lighthouse\Exceptions\DefinitionException; use Nuwave\Lighthouse\Execution\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; trait HasBuilderArgument { - /** @param array $args */ + /** @param array $args */ private function makeBuilder(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder|ScoutBuilder { if (! $this->directiveHasArgument('builder')) { diff --git a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php index 368a86e996..cee825fbda 100644 --- a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php +++ b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php @@ -24,7 +24,7 @@ protected function relation(): string return $this->directiveArgValue('relation', $this->nodeName()); } - /** @param array $args */ + /** @param array $args */ protected function makeBuilderDecorator(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): \Closure { return function (Builder $builder) use ($root, $args, $context, $resolveInfo): void { diff --git a/src/Schema/TypeRegistry.php b/src/Schema/TypeRegistry.php index a806d5b856..160ef1ce23 100644 --- a/src/Schema/TypeRegistry.php +++ b/src/Schema/TypeRegistry.php @@ -168,7 +168,7 @@ public function register(Type $type): self * * @api * - * @param callable(): \GraphQL\Type\Definition\Type&\GraphQL\Type\Definition\NamedType $type + * @param callable(): \GraphQL\Type\Definition\Type&\GraphQL\Type\Definition\NamedType $type */ public function registerLazy(string $name, callable $type): self { @@ -200,7 +200,7 @@ public function overwrite(Type $type): self * * @api * - * @param callable(): \GraphQL\Type\Definition\Type&\GraphQL\Type\Definition\NamedType $type + * @param callable(): \GraphQL\Type\Definition\Type&\GraphQL\Type\Definition\NamedType $type */ public function overwriteLazy(string $name, callable $type): self { diff --git a/src/Support/Contracts/ArgBuilderDirective.php b/src/Support/Contracts/ArgBuilderDirective.php index 7f1aad0c78..8bb1d30271 100644 --- a/src/Support/Contracts/ArgBuilderDirective.php +++ b/src/Support/Contracts/ArgBuilderDirective.php @@ -11,8 +11,8 @@ interface ArgBuilderDirective extends Directive * * TODO try adding a generic type parameter for the type of model when PHPStan handles it better * - * @param Builder $builder the builder used to resolve the field - * @param mixed $value the client given value of the argument + * @param Builder $builder the builder used to resolve the field + * @param mixed $value the client given value of the argument * * @return Builder the modified builder */ diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index cc13605e5d..efa239bd86 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -31,7 +31,7 @@ public function setArgs(ArgumentSet $args): void * Retrieve the value of an argument or the default. * * @param string $key the key of the argument, may use dot notation to get nested values - * @param mixed $default returned in case the argument is not present + * @param mixed $default returned in case the argument is not present */ protected function arg(string $key, mixed $default = null): mixed { diff --git a/src/WhereConditions/Operator.php b/src/WhereConditions/Operator.php index 64abfd61ad..ce2608a2b5 100644 --- a/src/WhereConditions/Operator.php +++ b/src/WhereConditions/Operator.php @@ -30,7 +30,7 @@ public function defaultHasOperator(): string; /** * Apply the conditions to the query builder. * - * @param array $whereConditions + * @param array $whereConditions */ public function applyConditions(Builder $builder, array $whereConditions, string $boolean): Builder; } diff --git a/tests/Integration/Schema/Directives/AggregateDirectiveTest.php b/tests/Integration/Schema/Directives/AggregateDirectiveTest.php index 6a1e7fcae9..ae11436eaa 100644 --- a/tests/Integration/Schema/Directives/AggregateDirectiveTest.php +++ b/tests/Integration/Schema/Directives/AggregateDirectiveTest.php @@ -285,7 +285,7 @@ public function testAggregateWithBuilder(): void ]); } - /** @param array{difficulty: int, exclude: int} $args */ + /** @param array{difficulty: int, exclude: int} $args */ public function builder(mixed $root, array $args): Builder { return DB::table('tasks') diff --git a/tests/Integration/Subscriptions/Storage/RedisStorageManagerTest.php b/tests/Integration/Subscriptions/Storage/RedisStorageManagerTest.php index 9d15702225..0c560215fa 100644 --- a/tests/Integration/Subscriptions/Storage/RedisStorageManagerTest.php +++ b/tests/Integration/Subscriptions/Storage/RedisStorageManagerTest.php @@ -110,7 +110,7 @@ public function testSocketIDStoredOnSubscribe(): void $this->assertSame('1234.1234', $createdSubscriber->socket_id); } - /** @param array $headers */ + /** @param array $headers */ protected function querySubscription(string $topic = /** @lang GraphQL */ 'taskUpdated(id: 123)', array $headers = []): TestResponse { return $this->graphQL(/** @lang GraphQL */ " diff --git a/tests/Unit/Pagination/PaginationTypeTest.php b/tests/Unit/Pagination/PaginationTypeTest.php index 39bc4d6fb3..4741d4c760 100644 --- a/tests/Unit/Pagination/PaginationTypeTest.php +++ b/tests/Unit/Pagination/PaginationTypeTest.php @@ -11,7 +11,7 @@ final class PaginationTypeTest extends TestCase /** * @dataProvider invalidPaginationTypes * - * @param string $type An invalid type + * @param string $type An invalid type */ public function testThrowsExceptionForUnsupportedTypes(string $type): void { diff --git a/tests/Unit/Subscriptions/Broadcasters/PusherBroadcasterTest.php b/tests/Unit/Subscriptions/Broadcasters/PusherBroadcasterTest.php index 514c7201fa..e2d36063ef 100644 --- a/tests/Unit/Subscriptions/Broadcasters/PusherBroadcasterTest.php +++ b/tests/Unit/Subscriptions/Broadcasters/PusherBroadcasterTest.php @@ -53,7 +53,7 @@ public function testPusherNeverUsesLoggerInterface(): void $this->broadcast($subscriber); } - /** @param \Nuwave\Lighthouse\Subscriptions\Subscriber&\PHPUnit\Framework\MockObject\MockObject $subscriber */ + /** @param \Nuwave\Lighthouse\Subscriptions\Subscriber&\PHPUnit\Framework\MockObject\MockObject $subscriber */ private function broadcast(object $subscriber): void { $broadcastManager = $this->app->make(BroadcastManager::class); diff --git a/tests/Utils/Models/Task.php b/tests/Utils/Models/Task.php index 2575ab3413..2bab3da234 100644 --- a/tests/Utils/Models/Task.php +++ b/tests/Utils/Models/Task.php @@ -97,7 +97,7 @@ public function user(): BelongsTo } /** - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * * @return \Illuminate\Database\Eloquent\Builder */ @@ -107,7 +107,7 @@ public function scopeCompleted(EloquentBuilder $query): EloquentBuilder } /** - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param array $args * * @return \Illuminate\Database\Eloquent\Builder @@ -118,7 +118,7 @@ public function scopeFoo(EloquentBuilder $query, array $args): EloquentBuilder } /** - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param iterable $tags * * @return \Illuminate\Database\Eloquent\Builder diff --git a/tests/Utils/Models/User.php b/tests/Utils/Models/User.php index e8a14b23f8..ac18cbae92 100644 --- a/tests/Utils/Models/User.php +++ b/tests/Utils/Models/User.php @@ -120,8 +120,8 @@ public function team(): BelongsTo } /** - * @param \Illuminate\Database\Eloquent\Builder $query - * @param array{company: string} $args + * @param \Illuminate\Database\Eloquent\Builder $query + * @param array{company: string} $args * * @return \Illuminate\Database\Eloquent\Builder */ @@ -132,7 +132,7 @@ public function scopeCompanyName(EloquentBuilder $query, array $args): EloquentB } /** - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * * @return \Illuminate\Database\Eloquent\Builder */ diff --git a/tests/Utils/Models/WithEnum.php b/tests/Utils/Models/WithEnum.php index a9fd32b6a5..6b56e489ed 100644 --- a/tests/Utils/Models/WithEnum.php +++ b/tests/Utils/Models/WithEnum.php @@ -29,7 +29,7 @@ final class WithEnum extends Model ]; /** - * @param \Illuminate\Database\Eloquent\Builder $builder + * @param \Illuminate\Database\Eloquent\Builder $builder * * @return \Illuminate\Database\Eloquent\Builder */ From 4b1721dcb1ddb78aeecacb398ff261ca30c5f17a Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Tue, 2 May 2023 21:39:45 +0200 Subject: [PATCH 14/19] rename query to builder --- src/Schema/Directives/AggregateDirective.php | 8 ++++---- src/Schema/Directives/Traits/HasBuilderArgument.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Schema/Directives/AggregateDirective.php b/src/Schema/Directives/AggregateDirective.php index abca3bffc2..bcdb6186f2 100644 --- a/src/Schema/Directives/AggregateDirective.php +++ b/src/Schema/Directives/AggregateDirective.php @@ -144,16 +144,16 @@ public function resolveField(FieldValue $fieldValue): callable $builderResolver = $this->getResolverFromArgument('builder'); return function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($builderResolver) { - $query = $builderResolver($root, $args, $context, $resolveInfo); + $builder = $builderResolver($root, $args, $context, $resolveInfo); assert( - $query instanceof Builder, + $builder instanceof Builder, "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", ); - $this->makeBuilderDecorator($root, $args, $context, $resolveInfo)($query); + $this->makeBuilderDecorator($root, $args, $context, $resolveInfo)($builder); - return $query->{$this->function()}($this->column()); + return $builder->{$this->function()}($this->column()); }; } diff --git a/src/Schema/Directives/Traits/HasBuilderArgument.php b/src/Schema/Directives/Traits/HasBuilderArgument.php index a014228d9b..d06a293e04 100644 --- a/src/Schema/Directives/Traits/HasBuilderArgument.php +++ b/src/Schema/Directives/Traits/HasBuilderArgument.php @@ -19,13 +19,13 @@ private function makeBuilder(mixed $root, array $args, GraphQLContext $context, $builderResolver = $this->getResolverFromArgument('builder'); - $query = $builderResolver($root, $args, $context, $resolveInfo); + $builder = $builderResolver($root, $args, $context, $resolveInfo); assert( - $query instanceof Builder || $query instanceof ScoutBuilder, + $builder instanceof Builder || $builder instanceof ScoutBuilder, "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", ); - return $query; + return $builder; } } From 90656b4c7c7f9b44264ecafbd2f18ce575bf47b5 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Tue, 2 May 2023 21:45:14 +0200 Subject: [PATCH 15/19] improve error method --- src/Schema/Directives/Traits/HasBuilderArgument.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Directives/Traits/HasBuilderArgument.php b/src/Schema/Directives/Traits/HasBuilderArgument.php index d06a293e04..aeef05e32a 100644 --- a/src/Schema/Directives/Traits/HasBuilderArgument.php +++ b/src/Schema/Directives/Traits/HasBuilderArgument.php @@ -23,7 +23,7 @@ private function makeBuilder(mixed $root, array $args, GraphQLContext $context, assert( $builder instanceof Builder || $builder instanceof ScoutBuilder, - "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Builder or Relation.", + "The method referenced by the builder argument of the @{$this->name()} directive on {$this->nodeName()} must return a Scout Builder, Query Builder or Relation.", ); return $builder; From adc752634d2abbfa3d4364a516ecfb95e0aa3779 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Wed, 3 May 2023 10:52:22 +0200 Subject: [PATCH 16/19] use common query builder interface --- src/Execution/ResolveInfo.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Execution/ResolveInfo.php b/src/Execution/ResolveInfo.php index 23d86ab80e..dad35be381 100644 --- a/src/Execution/ResolveInfo.php +++ b/src/Execution/ResolveInfo.php @@ -79,10 +79,10 @@ public function wouldEnhanceBuilder(Builder|ScoutBuilder $builder, array $scopes /** * Recursively apply the ArgBuilderDirectives onto the builder. * - * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder + * @param \Illuminate\Contracts\Database\Query\Builder $builder * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter */ - protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation &$builder, callable $directiveFilter = null): void + protected static function applyArgBuilderDirectives(ArgumentSet $argumentSet, Builder &$builder, callable $directiveFilter = null): void { foreach ($argumentSet->arguments as $argument) { $value = $argument->toPlain(); From 6020bfb615ea510876fb164820b49f9c0e7f6a41 Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Wed, 3 May 2023 11:06:02 +0200 Subject: [PATCH 17/19] use fqcn --- src/Pagination/PaginationArgs.php | 2 +- src/Support/Contracts/ArgBuilderDirective.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Pagination/PaginationArgs.php b/src/Pagination/PaginationArgs.php index fee52f06ef..4c3da24ba6 100644 --- a/src/Pagination/PaginationArgs.php +++ b/src/Pagination/PaginationArgs.php @@ -73,7 +73,7 @@ protected static function calculateCurrentPage(int $first, int $after, int $defa /** * Apply the args to a builder, constructing a paginator. * - * @return Paginator + * @return \Illuminate\Contracts\Pagination\Paginator<\Illuminate\Database\Eloquent\Model> */ public function applyToBuilder(Builder|ScoutBuilder $builder): Paginator { diff --git a/src/Support/Contracts/ArgBuilderDirective.php b/src/Support/Contracts/ArgBuilderDirective.php index 8bb1d30271..dd9b3f40df 100644 --- a/src/Support/Contracts/ArgBuilderDirective.php +++ b/src/Support/Contracts/ArgBuilderDirective.php @@ -11,10 +11,10 @@ interface ArgBuilderDirective extends Directive * * TODO try adding a generic type parameter for the type of model when PHPStan handles it better * - * @param Builder $builder the builder used to resolve the field + * @param \Illuminate\Contracts\Database\Query\Builder $builder the builder used to resolve the field * @param mixed $value the client given value of the argument * - * @return Builder the modified builder + * @return \Illuminate\Contracts\Database\Query\Builder the modified builder */ public function handleBuilder(Builder $builder, mixed $value): Builder; } From 14729043ef77d1a3c4698f02aa8d530cf2d9039d Mon Sep 17 00:00:00 2001 From: Konstantin Duczek Date: Wed, 3 May 2023 11:07:47 +0200 Subject: [PATCH 18/19] document imports as well --- UPGRADE.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/UPGRADE.md b/UPGRADE.md index 4169faafd5..8edb692950 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -30,6 +30,11 @@ Lighthouse now uses `Illuminate\Contracts\Database\Query\Builder` to type hint d If you implement `ArgBuilderDirective` or `FieldBuilderDirective`, you will have to change the signature of the `handleFieldBuilder` method accordingly: ```diff +- use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +- use Illuminate\Database\Eloquent\Relations\Relation; +- use Illuminate\Database\Query\Builder as QueryBuilder; ++ use Illuminate\Contracts\Database\Query\Builder; + - public function handleFieldBuilder(QueryBuilder|EloquentBuilder|Relation $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): QueryBuilder|EloquentBuilder|Relation; + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder; ``` From 5ae83021fb0e494f8ba16388d86474cc13b1c7a6 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Wed, 3 May 2023 11:24:35 +0200 Subject: [PATCH 19/19] Do not pass `ResolveInfo` to itself in `ResolveInfo::enhanceBuilder()` --- CHANGELOG.md | 1 + UPGRADE.md | 15 ++++++++++ src/Auth/CanDirective.php | 2 -- src/Execution/ResolveInfo.php | 30 +++++++++---------- src/Pagination/PaginateDirective.php | 1 - src/Schema/Directives/AllDirective.php | 1 - src/Schema/Directives/FindDirective.php | 1 - src/Schema/Directives/FirstDirective.php | 1 - .../ModifyModelExistenceDirective.php | 4 +-- .../Traits/RelationDirectiveHelpers.php | 1 - 10 files changed, 32 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d299e239e..ef97216162 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ You can find and compare releases at the [GitHub release page](https://github.co ### Changed - Use common Builder interface https://github.com/nuwave/lighthouse/pull/2389 +- Do not pass `ResolveInfo` to itself in `ResolveInfo::enhanceBuilder()` https://github.com/nuwave/lighthouse/pull/2389 ## v6.8.0 diff --git a/UPGRADE.md b/UPGRADE.md index 8edb692950..945551b918 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -39,6 +39,21 @@ If you implement `ArgBuilderDirective` or `FieldBuilderDirective`, you will have + public function handleFieldBuilder(Builder $builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder; ``` +### Do not pass `ResolveInfo` to itself in `ResolveInfo::enhanceBuilder()` + +`ResolveInfo::enhanceBuilder()` no longer expects to be passed an instance of `ResolveInfo`, +as it can access it via `$this`. + +```diff +use Nuwave\Lighthouse\Execution\ResolveInfo; +use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; + +// Some resolver function or directive middleware +function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) { +- $resolveInfo->enhanceBuilder($builder, $scopes, $root, $args, $context, $resolveInfo, $directiveFilter); ++ $resolveInfo->enhanceBuilder($builder, $scopes, $root, $args, $context, $directiveFilter); +``` + ## v5 to v6 ### `messages` on `@rules` and `@rulesForArray` diff --git a/src/Auth/CanDirective.php b/src/Auth/CanDirective.php index ec5f0f9b2d..b2dcad28bc 100644 --- a/src/Auth/CanDirective.php +++ b/src/Auth/CanDirective.php @@ -157,7 +157,6 @@ protected function modelsToCheck(mixed $root, array $args, GraphQLContext $conte $root, $args, $context, - $resolveInfo, ) ->get(); } @@ -194,7 +193,6 @@ protected function modelsToCheck(mixed $root, array $args, GraphQLContext $conte $root, $args, $context, - $resolveInfo, Utils::instanceofMatcher(TrashedDirective::class), ); assert($enhancedBuilder instanceof EloquentBuilder); diff --git a/src/Execution/ResolveInfo.php b/src/Execution/ResolveInfo.php index dad35be381..fdb29cc9ec 100644 --- a/src/Execution/ResolveInfo.php +++ b/src/Execution/ResolveInfo.php @@ -38,18 +38,18 @@ public function __construct( * @param array $scopes * @param array $args * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter + * + * @api */ - public function enhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $directiveFilter = null): Builder|ScoutBuilder + public function enhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, callable $directiveFilter = null): Builder|ScoutBuilder { - $argumentSet = $resolveInfo->argumentSet; - - $scoutEnhancer = new ScoutEnhancer($argumentSet, $builder); + $scoutEnhancer = new ScoutEnhancer($this->argumentSet, $builder); if ($scoutEnhancer->canEnhanceBuilder()) { return $scoutEnhancer->enhanceBuilder(); } - self::applyArgBuilderDirectives($argumentSet, $builder, $directiveFilter); - self::applyFieldBuilderDirectives($builder, $root, $args, $context, $resolveInfo); + self::applyArgBuilderDirectives($this->argumentSet, $builder, $directiveFilter); + self::applyFieldBuilderDirectives($builder, $root, $args, $context, $this); foreach ($scopes as $scope) { $builder->{$scope}($args); @@ -65,14 +65,14 @@ public function enhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mix * @param array $scopes * @param array $args * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter + * + * @api */ - public function wouldEnhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $directiveFilter = null): bool + public function wouldEnhanceBuilder(Builder|ScoutBuilder $builder, array $scopes, mixed $root, array $args, GraphQLContext $context, callable $directiveFilter = null): bool { - $argumentSet = $resolveInfo->argumentSet; - - return (new ScoutEnhancer($argumentSet, $builder))->wouldEnhanceBuilder() - || self::wouldApplyArgBuilderDirectives($argumentSet, $builder, $directiveFilter) - || self::wouldApplyFieldBuilderDirectives($resolveInfo) + return (new ScoutEnhancer($this->argumentSet, $builder))->wouldEnhanceBuilder() + || self::wouldApplyArgBuilderDirectives($this->argumentSet, $builder, $directiveFilter) + || self::wouldApplyFieldBuilderDirectives($this) || $scopes !== []; } @@ -115,10 +115,9 @@ static function ($value) use (&$builder, $directiveFilter): void { /** * Would there be any ArgBuilderDirectives to apply to the builder? * - * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param (callable(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter */ - protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSet, \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation &$builder, callable $directiveFilter = null): bool + protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSet, Builder &$builder, callable $directiveFilter = null): bool { foreach ($argumentSet->arguments as $argument) { $filteredDirectives = $argument @@ -157,10 +156,9 @@ protected static function wouldApplyArgBuilderDirectives(ArgumentSet $argumentSe /** * Apply the FieldBuilderDirectives onto the builder. * - * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $builder * @param array $args */ - protected static function applyFieldBuilderDirectives(\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation &$builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): void + protected static function applyFieldBuilderDirectives(Builder &$builder, mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): void { foreach (self::fieldBuilderDirectives($resolveInfo) as $fieldBuilderDirective) { $builder = $fieldBuilderDirective->handleFieldBuilder($builder, $root, $args, $context, $resolveInfo); diff --git a/src/Pagination/PaginateDirective.php b/src/Pagination/PaginateDirective.php index 25eeff34ac..175a16d61c 100644 --- a/src/Pagination/PaginateDirective.php +++ b/src/Pagination/PaginateDirective.php @@ -157,7 +157,6 @@ public function resolveField(FieldValue $fieldValue): callable $root, $args, $context, - $resolveInfo, ); $paginationArgs = PaginationArgs::extractArgs($args, $this->paginationType(), $this->paginateMaxCount()); diff --git a/src/Schema/Directives/AllDirective.php b/src/Schema/Directives/AllDirective.php index baa37754ea..2a306f39bc 100644 --- a/src/Schema/Directives/AllDirective.php +++ b/src/Schema/Directives/AllDirective.php @@ -57,7 +57,6 @@ public function resolveField(FieldValue $fieldValue): callable $root, $args, $context, - $resolveInfo, ) ->get(); } diff --git a/src/Schema/Directives/FindDirective.php b/src/Schema/Directives/FindDirective.php index fc42a8e67e..9b62f53c8f 100644 --- a/src/Schema/Directives/FindDirective.php +++ b/src/Schema/Directives/FindDirective.php @@ -42,7 +42,6 @@ public function resolveField(FieldValue $fieldValue): callable $root, $args, $context, - $resolveInfo, ) ->get(); diff --git a/src/Schema/Directives/FirstDirective.php b/src/Schema/Directives/FirstDirective.php index 4d3d166a17..5b6f64fb22 100644 --- a/src/Schema/Directives/FirstDirective.php +++ b/src/Schema/Directives/FirstDirective.php @@ -41,7 +41,6 @@ public function resolveField(FieldValue $fieldValue): callable $root, $args, $context, - $resolveInfo, ); assert($builder instanceof EloquentBuilder); diff --git a/src/Schema/Directives/ModifyModelExistenceDirective.php b/src/Schema/Directives/ModifyModelExistenceDirective.php index 6b9fe37aea..5859017413 100644 --- a/src/Schema/Directives/ModifyModelExistenceDirective.php +++ b/src/Schema/Directives/ModifyModelExistenceDirective.php @@ -34,11 +34,11 @@ public function resolveField(FieldValue $fieldValue): callable return function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($modelClass, $scopes, $expectsList): EloquentCollection|Model|null { $builder = $modelClass::query(); - if (! $resolveInfo->wouldEnhanceBuilder($builder, $scopes, $root, $args, $context, $resolveInfo)) { + if (! $resolveInfo->wouldEnhanceBuilder($builder, $scopes, $root, $args, $context)) { throw self::wouldModifyAll(); } - $builder = $resolveInfo->enhanceBuilder($builder, $scopes, $root, $args, $context, $resolveInfo); + $builder = $resolveInfo->enhanceBuilder($builder, $scopes, $root, $args, $context); assert($builder instanceof EloquentBuilder); $modelOrModels = $this->enhanceBuilder($builder)->get(); diff --git a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php index cee825fbda..7caf25a7f9 100644 --- a/src/Schema/Directives/Traits/RelationDirectiveHelpers.php +++ b/src/Schema/Directives/Traits/RelationDirectiveHelpers.php @@ -38,7 +38,6 @@ protected function makeBuilderDecorator(mixed $root, array $args, GraphQLContext $root, $args, $context, - $resolveInfo, ); }; }