Skip to content

Commit

Permalink
Package graphql migrated to object-based config.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Nov 15, 2024
1 parent 6d9b78a commit 4f0eddc
Show file tree
Hide file tree
Showing 37 changed files with 817 additions and 474 deletions.
10 changes: 9 additions & 1 deletion packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ For Implicit type, the following rules are applied (in this order; concrete dire
* Otherwise - include
* Ignored (if supported)? - exclude
When converting the field, some of the original directives will be copied into the newly generated field. For the Explicit type, all directives except operators of other directives will be copied. For Implicit type, you can use [`builder.allowed_directives`](defaults/config.php) setting to control. Be aware of directive locations - the package doesn't perform any checks to ensure that the copied directive allowed on `INPUT_FIELD_DEFINITION`, it just copies it as is.
When converting the field, some of the original directives will be copied into the newly generated field. For the Explicit type, all directives except operators of other directives will be copied. For Implicit type, you can use [`Config::$allowedDirectives`][code-links/2537184413e9d748] setting to control. Be aware of directive locations - the package doesn't perform any checks to ensure that the copied directive allowed on `INPUT_FIELD_DEFINITION`, it just copies it as is.
# Builder field/column name
Expand Down Expand Up @@ -605,3 +605,11 @@ Please follow [Upgrade Guide](UPGRADE.md).
This package is the part of Awesome Set of Packages for Laravel. Please use the [main repository](https://github.com/LastDragon-ru/lara-asp) to [report issues](https://github.com/LastDragon-ru/lara-asp/issues), send [pull requests](https://github.com/LastDragon-ru/lara-asp/pulls), or [ask questions](https://github.com/LastDragon-ru/lara-asp/discussions).

[//]: # (end: preprocess/c4ba75080f5a48b7)

[//]: # (start: code-links)
[//]: # (warning: Generated automatically. Do not edit.)

[code-links/2537184413e9d748]: src/Builder/Config.php#L11-L29
"\LastDragon_ru\LaraASP\GraphQL\Builder\Config::$allowedDirectives"

[//]: # (end: code-links)
4 changes: 3 additions & 1 deletion packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)

[//]: # (end: preprocess/9679e76379216855)

* [ ] Package config now uses objects instead of an array, it is recommended to migrate to the new format. 🤝

* [ ] The [`JsonStringType`][code-links/9ad31c571587f0f4] is not implement [`TypeDefinition`][code-links/3c9ddc100b69df14] anymore. To add the scalar into the Schema, you can use `@type`/`@scalar` directive, or create a custom implementation of `TypeDefinition` contract to use with `Builder`/`Manipulator`.

## Tests
Expand Down Expand Up @@ -352,7 +354,7 @@ This section is actual only if you are extending the package. Please review and
[code-links/ab92ab72ccf08721]: src/SearchBy/Definitions/SearchByOperatorFieldDirective.php
"\LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorFieldDirective"

[code-links/5f93528c6eb9dc8f]: src/SearchBy/Operators.php#L60
[code-links/5f93528c6eb9dc8f]: src/SearchBy/Operators.php#L58
"\LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators::Object"

[code-links/b26bb0f7b2034eb1]: src/SortBy/Definitions/SortByOperatorFieldDirective.php
Expand Down
138 changes: 4 additions & 134 deletions packages/graphql/defaults/config.php
Original file line number Diff line number Diff line change
@@ -1,146 +1,16 @@
<?php declare(strict_types = 1);

use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Enums\Direction;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Enums\Nulls;
use Nuwave\Lighthouse\Schema\Directives\RenameDirective;
use LastDragon_ru\LaraASP\GraphQL\PackageConfig;

/**
* -----------------------------------------------------------------------------
* GraphQL Settings
* -----------------------------------------------------------------------------
*
* Note: You need to clear/rebuild the cached schema and IDE helper files after
* changing any of the settings.
*
* @see https://lighthouse-php.com/master/api-reference/commands.html#clear-cache
* @see https://lighthouse-php.com/master/api-reference/commands.html#ide-helper
*
* @var array{
* search_by: array{
* operators: array<string, list<string|class-string<Operator>>>,
* },
* sort_by: array{
* operators: array<string, list<string|class-string<Operator>>>,
* nulls: Nulls|non-empty-array<value-of<Direction>, Nulls>|null,
* },
* stream: array{
* search: array{
* name: string,
* enabled: bool,
* },
* sort: array{
* name: string,
* enabled: bool,
* },
* limit: array{
* name: string,
* default: int<1, max>,
* max: int<1, max>,
* },
* offset: array{
* name: string,
* }
* },
* builder: array{
* allowed_directives: list<class-string>,
* },
* } $settings
*/
$settings = [
/**
* Settings for {@see \LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByDirective @searchBy} directive.
*/
'search_by' => [
/**
* Operators
* ---------------------------------------------------------------------
*
* You can redefine operators for exiting (=default) types OR define own
* types here. Note that directives is the recommended way and have
* priority over the array. Please see the documentation for more
* details.
*/
'operators' => [
// empty
],
],

/**
* Settings for {@see \LastDragon_ru\LaraASP\GraphQL\SortBy\Definitions\SortByDirective @sortBy} directive.
*/
'sort_by' => [
/**
* Operators
* ---------------------------------------------------------------------
*
* You can redefine operators for exiting (=default) types OR define own
* types here. Note that directives is the recommended way and have
* priority over the array. Please see the documentation for more
* details.
*/
'operators' => [
// empty
],

/**
* NULLs
*
* ---------------------------------------------------------------------
*
* Determines how the `NULL` values should be treatment. By default,
* there is no any processing, so the order of `NULL` depends on the
* database. It may be set for all (if single value) or for each
* direction (if array). Not all databases/builders may be supported.
* Please check the documentation for more details.
*
* @see Nulls
*/
'nulls' => null,
],

/**
* Settings for {@see \LastDragon_ru\LaraASP\GraphQL\Stream\Definitions\StreamDirective @stream} directive.
*/
'stream' => [
'search' => [
'name' => 'where',
'enabled' => true,
],
'sort' => [
'name' => 'order',
'enabled' => true,
],
'limit' => [
'name' => 'limit',
'default' => 25,
'max' => 100,
],
'offset' => [
'name' => 'offset',
],
],
$config = PackageConfig::getDefaultConfig();

/**
* General settings for all `Builder` directives like `@searchBy`/`@sortBy`/etc.
*/
'builder' => [
/**
* The list of the directives which should be copied from the original
* field into the generated `input` field.
*
* Important notes:
* - All other directives except {@see Operator} (for the current
* directive) will be ignored.
* - There are no any checks that directive can be used on
* `INPUT_FIELD_DEFINITION`.
* - The `instanceof` operator is used to check.
* - Applies for Implicit types only.
*/
'allowed_directives' => [
RenameDirective::class,
],
],
];
// ...

return $settings;
return $config;
72 changes: 32 additions & 40 deletions packages/graphql/docs/Directives/@searchBy.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,52 +254,44 @@ on

### Schema

[include:example]: @searchByConfigOperators.php
[//]: # (start: preprocess/95312ea5dfacf197)
[//]: # (warning: Generated automatically. Do not edit.)

```php
<?php declare(strict_types = 1);

use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use LastDragon_ru\LaraASP\GraphQL\PackageConfig;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorBetweenDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorEqualDirective;

/**
* -----------------------------------------------------------------------------
* GraphQL Settings
* -----------------------------------------------------------------------------
*
* @var array{
* search_by: array{
* operators: array<string, list<string|class-string<Operator>>>
* }
* } $settings
*/
$settings = [
'search_by' => [
'operators' => [
// You can define a list of operators for each type
'Date' => [
SearchByOperatorEqualDirective::class,
SearchByOperatorBetweenDirective::class,
MyCustomOperator::class,
],

// Or re-use existing type
'DateTime' => [
'Date',
],

// Or re-use built-in type
'Int' => [
'Int', // built-in operators for `Int` will be used
MyCustomOperator::class,
],

// You can also use enum name to redefine default operators for it:
'MyEnum' => [
'Boolean',
],
],
],
$config = PackageConfig::getDefaultConfig();

// You can define a list of operators for each type
$config->searchBy->operators['Date'] = [
SearchByOperatorEqualDirective::class,
SearchByOperatorBetweenDirective::class,
// MyCustomOperator::class,
];

// Or re-use existing type
$config->searchBy->operators['DateTime'] = [
'Date',
];

return $settings;
// Or re-use built-in type
$config->searchBy->operators['Int'] = [
'Int', // built-in operators for `Int` will be used
// MyCustomOperator::class, // the custom operator will be added
];

// You can also use enum name to redefine default operators for it:
$config->searchBy->operators['MyEnum'] = [
'Boolean',
];

// Return
return $config;
```

[//]: # (end: preprocess/95312ea5dfacf197)
33 changes: 33 additions & 0 deletions packages/graphql/docs/Directives/@searchByConfigOperators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

use LastDragon_ru\LaraASP\GraphQL\PackageConfig;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorBetweenDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorEqualDirective;

$config = PackageConfig::getDefaultConfig();

// You can define a list of operators for each type
$config->searchBy->operators['Date'] = [
SearchByOperatorEqualDirective::class,
SearchByOperatorBetweenDirective::class,
// MyCustomOperator::class,
];

// Or re-use existing type
$config->searchBy->operators['DateTime'] = [
'Date',
];

// Or re-use built-in type
$config->searchBy->operators['Int'] = [
'Int', // built-in operators for `Int` will be used
// MyCustomOperator::class, // the custom operator will be added
];

// You can also use enum name to redefine default operators for it:
$config->searchBy->operators['MyEnum'] = [
'Boolean',
];

// Return
return $config;
Loading

0 comments on commit 4f0eddc

Please sign in to comment.