Skip to content

Commit

Permalink
deprecate(graphql)!: `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts…
Browse files Browse the repository at this point in the history
…\Scout\FieldResolver`, use `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver` instead.
  • Loading branch information
LastDragon-ru committed Jan 13, 2024
1 parent 2547523 commit 122f29b
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 84 deletions.
10 changes: 7 additions & 3 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ Represents [JSON](https://json.org) string.
# Scout
[Scout](https://laravel.com/docs/scout) is also supported 🤩. By default `@searchBy`/`@sortBy` will convert nested/related properties into dot string: eg `{user: {name: asc}}` will be converted into `user.name`. You can redefine this behavior by [`FieldResolver`](./src/Builder/Contracts/Scout/FieldResolver.php):
[Scout](https://laravel.com/docs/scout) is also supported 🤩. You just need to add [`@search`](https://lighthouse-php.com/master/api-reference/directives.html#search) directive to an argument.
# Builder property name
By default `@searchBy`/`@sortBy` will convert nested/related properties into dot string: eg `{user: {name: asc}}` will be converted into `user.name`. You can redefine this behavior by [`BuilderPropertyResolver`](./src/Builder/Contracts/BuilderPropertyResolver.php):
```php
// AppProvider
$this->app->bind(
LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scout\FieldResolver::class,
MyScoutColumnResolver::class,
LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver::class,
MyBuilderPropertyResolver::class,
);
```
Expand Down
2 changes: 2 additions & 0 deletions packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)
];
```

* [ ] If you are using `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scout\FieldResolver`, use `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver` instead. 🤝

## API

This section is actual only if you are extending the package. Please review and update (listed the most significant changes only):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
namespace LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scout;

use Illuminate\Database\Eloquent\Model;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver;
use LastDragon_ru\LaraASP\GraphQL\Builder\Property;

/**
* Convert nested property into Scout field.
*
* @deprecated 5.4.0 Please use {@see BuilderPropertyResolver} instead.
*
* @see BuilderPropertyResolver
*/
interface FieldResolver {
public function getField(Model $model, Property $property): string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace LastDragon_ru\LaraASP\GraphQL\Builder\Defaults;

use Laravel\Scout\Builder as ScoutBuilder;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver as BuilderPropertyResolverContract;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scout\FieldResolver as ScoutFieldResolver;
use LastDragon_ru\LaraASP\GraphQL\Builder\Property;
use Override;

Expand All @@ -12,12 +14,16 @@
* @internal
*/
final class BuilderPropertyResolver implements BuilderPropertyResolverContract {
public function __construct() {
public function __construct(
private readonly ?ScoutFieldResolver $resolver = null,
) {
// empty
}

#[Override]
public function getProperty(object $builder, Property $property): string {
return implode('.', $property->getPath());
return $builder instanceof ScoutBuilder && $this->resolver
? $this->resolver->getField($builder->model, $property)
: implode('.', $property->getPath());
}
}
24 changes: 0 additions & 24 deletions packages/graphql/src/Builder/Scout/DefaultFieldResolver.php

This file was deleted.

3 changes: 0 additions & 3 deletions packages/graphql/src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Provider\WithConfig;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver as BuilderPropertyResolverContract;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scout\FieldResolver as ScoutFieldResolverContract;
use LastDragon_ru\LaraASP\GraphQL\Builder\Defaults\BuilderPropertyResolver;
use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Scout\DefaultFieldResolver as ScoutFieldResolver;
use LastDragon_ru\LaraASP\GraphQL\Printer\DirectiveResolver;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators as SearchByOperators;
Expand Down Expand Up @@ -69,7 +67,6 @@ static function (): array {
protected function registerBindings(): void {
$this->app->scopedIf(SorterFactoryContract::class, SorterFactory::class);
$this->app->scopedIf(StreamFactoryContract::class, StreamFactory::class);
$this->app->scopedIf(ScoutFieldResolverContract::class, ScoutFieldResolver::class);
$this->app->scopedIf(BuilderPropertyResolverContract::class, BuilderPropertyResolver::class);
}

Expand Down
69 changes: 64 additions & 5 deletions packages/graphql/src/SearchBy/Directives/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider;
use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider;
use LastDragon_ru\LaraASP\Testing\Providers\MergeDataProvider;
use Mockery\MockInterface;
use Nuwave\Lighthouse\Execution\Arguments\Argument;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Nuwave\Lighthouse\Schema\TypeRegistry;
Expand Down Expand Up @@ -284,22 +285,37 @@ public function testHandleBuilder(
/**
* @dataProvider dataProviderHandleScoutBuilder
*
* @param array<string, mixed>|Exception $expected
* @param Closure(static): ScoutBuilder $builderFactory
* @param Closure():FieldResolver|null $fieldResolver
* @param array<string, mixed>|Exception $expected
* @param Closure(static): ScoutBuilder $builderFactory
* @param Closure(object, Property): string|null $resolver
* @param Closure():FieldResolver|null $fieldResolver
*/
public function testHandleScoutBuilder(
array|Exception $expected,
Closure $builderFactory,
mixed $value,
Closure $fieldResolver = null,
?Closure $resolver,
?Closure $fieldResolver,
): void {
$builder = $builderFactory($this);
$directive = $this->getExposeBuilderDirective($builder);

Container::getInstance()->make(DirectiveLocator::class)
->setResolved('search', SearchDirective::class);

if ($resolver) {
$this->override(
BuilderPropertyResolver::class,
static function (MockInterface $mock) use ($resolver): void {
$mock
->shouldReceive('getProperty')
->atLeast()
->once()
->andReturnUsing($resolver);
},
);
}

if ($fieldResolver) {
$this->override(FieldResolver::class, $fieldResolver);
}
Expand Down Expand Up @@ -674,6 +690,7 @@ public static function dataProviderHandleScoutBuilder(): array {
// empty
],
null,
null,
],
'empty operators' => [
new ConditionEmpty(),
Expand All @@ -683,6 +700,7 @@ public static function dataProviderHandleScoutBuilder(): array {
],
],
null,
null,
],
'too many properties' => [
new ConditionTooManyProperties(['a', 'b']),
Expand All @@ -695,6 +713,7 @@ public static function dataProviderHandleScoutBuilder(): array {
],
],
null,
null,
],
'too many operators' => [
new ConditionTooManyOperators(['equal', 'in']),
Expand All @@ -705,13 +724,15 @@ public static function dataProviderHandleScoutBuilder(): array {
],
],
null,
null,
],
'null' => [
[
// empty
],
null,
null,
null,
],
'default field resolver' => [
[
Expand Down Expand Up @@ -745,8 +766,9 @@ public static function dataProviderHandleScoutBuilder(): array {
],
],
null,
null,
],
'custom field resolver' => [
'resolver (deprecated)' => [
[
'wheres' => [
'properties/a' => 1,
Expand Down Expand Up @@ -777,6 +799,7 @@ public static function dataProviderHandleScoutBuilder(): array {
],
],
],
null,
static function (): FieldResolver {
return new class() implements FieldResolver {
/**
Expand All @@ -792,6 +815,42 @@ public function getField(
};
},
],
'resolver' => [
[
'wheres' => [
'a' => 1,
'c__a' => 2,
],
'whereIns' => [
'renamed.field' => ['a', 'b', 'c'],
],
],
[
'allOf' => [
[
'a' => [
'equal' => 1,
],
],
[
'b' => [
'in' => ['a', 'b', 'c'],
],
],
[
'c' => [
'a' => [
'equal' => 2,
],
],
],
],
],
static function (object $builder, Property $property): string {
return implode('__', $property->getPath());
},
null,
],
]),
))->getData();
}
Expand Down
6 changes: 2 additions & 4 deletions packages/graphql/src/SearchBy/Operators/Comparison/Equal.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ public function call(
Argument $argument,
Context $context,
): object {
$property = $property->getParent();
$property = $this->resolver->getProperty($builder, $property->getParent());
$value = $argument->toPlain();

if ($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder) {
$builder->where($this->resolver->getProperty($builder, $property), '=', $value);
$builder->where($property, '=', $value);
} elseif ($builder instanceof ScoutBuilder) {
$property = $this->getFieldResolver()->getField($builder->model, $property);

$builder->where($property, $value);
} else {
throw new OperatorUnsupportedBuilder($this, $builder);
Expand Down
20 changes: 18 additions & 2 deletions packages/graphql/src/SearchBy/Operators/Comparison/EqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function dataProviderCallScout(): array {
return (new CompositeDataProvider(
new ScoutBuilderDataProvider(),
new ArrayDataProvider([
'property' => [
'property' => [
[
'wheres' => [
'path.to.property' => 'abc',
Expand All @@ -167,7 +167,7 @@ static function (self $test): Argument {
null,
null,
],
'property with resolver' => [
'resolver (deprecated)' => [
[
'wheres' => [
'properties/path/to/property' => 'abc',
Expand All @@ -188,6 +188,22 @@ public function getField(Model $model, Property $property): string {
};
},
],
'resolver' => [
[
'wheres' => [
'path__to__property' => 'abc',
],
],
new Property('path', 'to', 'property', 'operator name should be ignored'),
static function (self $test): Argument {
return $test->getGraphQLArgument('String!', 'abc');
},
null,
static function (object $builder, Property $property): string {
return implode('__', $property->getPath());
},
null,
],
]),
))->getData();
}
Expand Down
6 changes: 2 additions & 4 deletions packages/graphql/src/SearchBy/Operators/Comparison/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ public function call(
Argument $argument,
Context $context,
): object {
$property = $property->getParent();
$property = $this->resolver->getProperty($builder, $property->getParent());
$value = (array) $argument->toPlain();

if ($builder instanceof EloquentBuilder || $builder instanceof QueryBuilder) {
$builder->whereIn($this->resolver->getProperty($builder, $property), $value);
$builder->whereIn($property, $value);
} elseif ($builder instanceof ScoutBuilder) {
$property = $this->getFieldResolver()->getField($builder->model, $property);

$builder->whereIn($property, $value);
} else {
throw new OperatorUnsupportedBuilder($this, $builder);
Expand Down
20 changes: 18 additions & 2 deletions packages/graphql/src/SearchBy/Operators/Comparison/InTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function dataProviderCallScout(): array {
return (new CompositeDataProvider(
new ScoutBuilderDataProvider(),
new ArrayDataProvider([
'property' => [
'property' => [
[
'whereIns' => [
'path.to.property' => [1, 2, 3],
Expand All @@ -167,7 +167,7 @@ static function (self $test): Argument {
null,
null,
],
'property with resolver' => [
'resolver (deprecated)' => [
[
'whereIns' => [
'properties/path/to/property' => [1, 2, 3],
Expand All @@ -191,6 +191,22 @@ public function getField(Model $model, Property $property): string {
};
},
],
'resolver' => [
[
'whereIns' => [
'path__to__property' => [1, 2, 3],
],
],
new Property('path', 'to', 'property', 'operator name should be ignored'),
static function (self $test): Argument {
return $test->getGraphQLArgument('[Int!]!', [1, 2, 3]);
},
null,
static function (object $builder, Property $property): string {
return implode('__', $property->getPath());
},
null,
],
]),
))->getData();
}
Expand Down
Loading

0 comments on commit 122f29b

Please sign in to comment.