Skip to content

Commit

Permalink
feat(graphql)!: Universal builder property resolver (#120)
Browse files Browse the repository at this point in the history
Closes: #87
  • Loading branch information
LastDragon-ru authored Jan 13, 2024
2 parents e260593 + 122f29b commit f5934ef
Show file tree
Hide file tree
Showing 88 changed files with 2,339 additions and 789 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
4 changes: 3 additions & 1 deletion 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 All @@ -89,5 +91,5 @@ This section is actual only if you are extending the package. Please review and
* [ ] To get `BuilderInfo` instance within Operator the `LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context` should be used instead of `LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator`:

```php
$context->get(\LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulation\AstManipulation::class)?->builderInfo
$context->get(\LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulationBuilderInfo::class)?->builderInfo
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use LastDragon_ru\LaraASP\GraphQL\Builder\BuilderInfo;

class AstManipulation {
class AstManipulationBuilderInfo {
public function __construct(
public readonly BuilderInfo $builderInfo,
public readonly BuilderInfo $value,
) {
// empty
}
Expand Down
12 changes: 12 additions & 0 deletions packages/graphql/src/Builder/Contracts/BuilderPropertyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\GraphQL\Builder\Contracts;

use LastDragon_ru\LaraASP\GraphQL\Builder\Property;

/**
* Convert {@see Property} into builder property.
*/
interface BuilderPropertyResolver {
public function getProperty(object $builder, Property $property): string;
}
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
29 changes: 29 additions & 0 deletions packages/graphql/src/Builder/Defaults/BuilderPropertyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

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;

use function implode;

/**
* @internal
*/
final class BuilderPropertyResolver implements BuilderPropertyResolverContract {
public function __construct(
private readonly ?ScoutFieldResolver $resolver = null,
) {
// empty
}

#[Override]
public function getProperty(object $builder, Property $property): string {
return $builder instanceof ScoutBuilder && $this->resolver
? $this->resolver->getField($builder->model, $property)
: implode('.', $property->getPath());
}
}
8 changes: 3 additions & 5 deletions packages/graphql/src/Builder/Directives/HandlerDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Laravel\Scout\Builder as ScoutBuilder;
use LastDragon_ru\LaraASP\GraphQL\Builder\BuilderInfoDetector;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulation;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulationBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context as ContextContract;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
Expand Down Expand Up @@ -45,7 +45,7 @@
use function reset;

/**
* @see AstManipulation
* @see AstManipulationBuilderInfo
*/
abstract class HandlerDirective extends BaseDirective implements Handler {
use WithManipulator;
Expand Down Expand Up @@ -223,9 +223,7 @@ public function manipulateArgDefinition(

// Argument
$context = (new Context())->override([
AstManipulation::class => new AstManipulation(
builderInfo: $builder,
),
AstManipulationBuilderInfo::class => new AstManipulationBuilderInfo($builder),
]);
$source = $this->getFieldArgumentSource($manipulator, $parentType, $parentField, $argDefinition);
$type = $this->getArgDefinitionType($manipulator, $documentAST, $source, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LastDragon_ru\LaraASP\GraphQL\Builder\Directives;

use GraphQL\Language\DirectiveLocation;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scope;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
Expand All @@ -13,7 +14,9 @@
use function is_a;

abstract class OperatorDirective extends BaseDirective implements Operator {
public function __construct() {
public function __construct(
protected readonly BuilderPropertyResolver $resolver,
) {
// empty
}

Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/Builder/Manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use GraphQL\Type\Definition\Type;
use Illuminate\Container\Container;
use Illuminate\Support\Str;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulation;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulationBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scope;
Expand Down Expand Up @@ -147,7 +147,7 @@ public function getTypeOperators(string $scope, string $type, Context $context,
}

// Builder?
$builder = $context->get(AstManipulation::class)?->builderInfo->getBuilder();
$builder = $context->get(AstManipulationBuilderInfo::class)?->value->getBuilder();

if (!$builder) {
return [];
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/src/Builder/ManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\ObjectType;
use Illuminate\Container\Container;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulation;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulationBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context as ContextContract;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
Expand Down Expand Up @@ -193,8 +193,8 @@ public function getScope(): string {

// Manipulator
$context = (new Context())->override([
AstManipulation::class => new AstManipulation(
builderInfo: new BuilderInfo($builder::class, $builder::class),
AstManipulationBuilderInfo::class => new AstManipulationBuilderInfo(
new BuilderInfo($builder::class, $builder::class),
),
]);
$document = Container::getInstance()->make(ASTBuilder::class)->documentAST();
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql/src/Builder/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use function array_slice;
use function array_values;
use function end;
use function explode;
use function implode;

class Property implements Stringable {
Expand Down Expand Up @@ -37,7 +36,7 @@ public function getPath(): array {
}

public function getChild(string $name): static {
return new static(...$this->path, ...explode(static::Separator, $name));
return new static(...$this->path, ...[$name]);
}

public function getParent(): static {
Expand Down
24 changes: 0 additions & 24 deletions packages/graphql/src/Builder/Scout/DefaultFieldResolver.php

This file was deleted.

6 changes: 3 additions & 3 deletions packages/graphql/src/Builder/Types/InputObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use GraphQL\Language\BlockString;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulation;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contexts\AstManipulationBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scope;
Expand Down Expand Up @@ -166,7 +166,7 @@ protected function getFieldDefinition(
Context $context,
): ?InputValueDefinitionNode {
// Builder?
$builder = $context->get(AstManipulation::class)?->builderInfo->getBuilder();
$builder = $context->get(AstManipulationBuilderInfo::class)?->value->getBuilder();

if (!$builder) {
return null;
Expand Down Expand Up @@ -222,7 +222,7 @@ protected function getFieldDirectiveOperator(
Context $context,
): ?Operator {
// Builder?
$builder = $context->get(AstManipulation::class)?->builderInfo->getBuilder();
$builder = $context->get(AstManipulationBuilderInfo::class)?->value->getBuilder();

if (!$builder) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Provider\WithConfig;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scout\FieldResolver as ScoutFieldResolverContract;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderPropertyResolver as BuilderPropertyResolverContract;
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 @@ -67,7 +67,7 @@ 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);
}

protected function registerOperators(): void {
Expand Down
Loading

0 comments on commit f5934ef

Please sign in to comment.