Skip to content

Commit

Permalink
feat(graphql)!: Root types for @searchBy and @sortBy (#129)
Browse files Browse the repository at this point in the history
Closes: #118
  • Loading branch information
LastDragon-ru authored Feb 8, 2024
2 parents c6dcd94 + 0b3888f commit 693df03
Show file tree
Hide file tree
Showing 78 changed files with 5,926 additions and 1,194 deletions.
87 changes: 70 additions & 17 deletions packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,36 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)

* [ ] `@searchByOperatorRelation` => `@searchByOperatorRelationship` (and class too; generated types will be named as `SearchByRelationship*` instead of `SearchByComplex*`).

* [ ] `@searchByOperatorProperty` => `@searchByOperatorField` (and class too)

* [ ] `@searchByOperatorCondition` => `@searchByOperatorFieldObject`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators::Condition` => `LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators::Object`.

* [ ] `scalar SearchByCondition` => `scalar SearchByObject`.

* [ ] Added the root type that will contain only extra operators and newly added `field` operator (always present and cannot be removed). The new query syntax is:

```graphql
query {
# WHERE name = "LastDragon"
users(where: {
field: { name: {equal: "LastDragon"} }
}) {
id
}
}
```

If you want to use old query syntax, you can add following bindings into application provider:

```php
$this->app->bind(
LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Condition\Root::class,
LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Condition\V5::class,
);
$this->app->bind(
LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Condition\Condition::class,
LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Condition\V5::class,
);
```

## `@sortBy`

* [ ] `enum SortByTypeFlag { yes }` => `enum SortByTypeFlag { Yes }`. 🤝
Expand All @@ -71,22 +93,45 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)

* [ ] If you are overriding Extra operators, you may need to add `SortByOperators::Extra` to use new built-in:

```php
$settings = [
'sort_by' => [
'operators' => [
SortByOperators::Extra => [
SortByOperators::Extra,
SortByOperatorRandomDirective::class,
],
],
],
];
```
```php
$settings = [
'sort_by' => [
'operators' => [
SortByOperators::Extra => [
SortByOperators::Extra,
SortByOperatorRandomDirective::class,
],
],
],
];
```

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

* [ ] `@sortByOperatorProperty` => `@sortByOperatorFieldObject` (and class too)
* [ ] Added the root type that will contain only extra operators and newly added `field` operator (always present and cannot be removed). The new query syntax is:

```graphql
query {
# ORDER BY user.name ASC, text DESC
comments(order: [
{field: {user: {name: asc}}}
{field: {text: desc}}
])
}
```

If you want to use old query syntax, you can add following bindings into application provider:

```php
$this->app->bind(
LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Clause\Root::class,
LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Clause\V5::class,
);
$this->app->bind(
LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Clause\Clause::class,
LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Clause\V5::class,
);
```

## API

Expand Down Expand Up @@ -129,3 +174,11 @@ This section is actual only if you are extending the package. Please review and
* [ ] `LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\BaseOperator` => `LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\Operator`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Traits\ScoutSupport` => `LastDragon_ru\LaraASP\GraphQL\Builder\Traits\WithScoutSupport`

* [ ] `@searchByOperatorCondition` => `@searchByOperatorChild` (and class too)

* [ ] `@searchByOperatorProperty` => `@searchByOperatorCondition` (and class too)

* [ ] `@sortByOperatorProperty` => `@sortByOperatorChild` (and class too)

* [ ] `@sortByOperatorField` => `@sortByOperatorSort` (and class too)
40 changes: 24 additions & 16 deletions packages/graphql/docs/Directives/@searchBy.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ That's all, just search 😃 (or look at [generated GraphQL schema](../../src/Se
query {
# WHERE name = "LastDragon"
users(where: {
name: {equal: "LastDragon"}
field: { name: { equal: "LastDragon" } }
}) {
id
}

# WHERE name != "LastDragon"
users(where: {
name: {notEqual: "LastDragon"}
field: { name: { notEqual: "LastDragon" } }
}) {
id
}

# WHERE name = "LastDragon" or name = "Aleksei"
users(where: {
anyOf: [
{name: {equal: "LastDragon"}}
{name: {equal: "Aleksei"}}
{ field: { name: { equal: "LastDragon" } } }
{ field: { name: { equal: "Aleksei" } } }
]
}) {
id
Expand All @@ -101,8 +101,8 @@ query {
users(where: {
not: {
anyOf: [
{name: {equal: "LastDragon"}}
{name: {equal: "Aleksei"}}
{ field: { name: { equal: "LastDragon" } } }
{ field: { name: { equal: "Aleksei" } } }
]
}
}) {
Expand All @@ -111,16 +111,20 @@ query {

# WHERE date IS NULL
users(where: {
date: {isNull: yes}
field: { date: { isNull: Yes } }
}) {
id
}

# Relationship: WHERE EXIST (related table)
comments(where: {
user: {
where: {
date: {between: {min: "2021-01-01", max: "2021-04-01"}}
field: {
user: {
where: {
field: {
date: { between: { min: "2021-01-01", max: "2021-04-01" } }
}
}
}
}
}) {
Expand All @@ -129,12 +133,16 @@ query {

# Relationship: WHERE COUNT (related table) = 2
comments(where: {
user: {
where: {
date: {between: {min: "2021-01-01", max: "2021-04-01"}}
}
count: {
equal: 2
field: {
user: {
where: {
field: {
date: { between: { min: "2021-01-01", max: "2021-04-01"} }
}
}
count: {
equal: 2
}
}
}
}) {
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/docs/Directives/@sortBy.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ And:
query {
# ORDER BY user.name ASC, text DESC
comments(order: [
{user: {name: asc}}
{text: desc}
{field: {user: {name: asc}}}
{field: {text: desc}}
])
}
```
Expand Down Expand Up @@ -216,7 +216,7 @@ query {
# ORDER BY user.name ASC NULLS FIRST, text DESC
comments(order: [
{nullsFirst: {user: {name: asc}}}
{text: desc}
{field: {text: desc}}
])
}
```
4 changes: 2 additions & 2 deletions packages/graphql/docs/Directives/@stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ Query:
query example(
$limit: Int!,
$offset: StreamOffset,
$where: SearchByConditionObject,
$order: [SortByClauseObject!],
$where: SearchByRootObject,
$order: [SortByRootObject!],
) {
objects(where: $where, order: $order, limit: $limit, offset: $offset) {
items {
Expand Down
24 changes: 20 additions & 4 deletions packages/graphql/src/Builder/Types/InputObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace LastDragon_ru\LaraASP\GraphQL\Builder\Types;

use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\BlockString;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextImplicit;
Expand Down Expand Up @@ -87,10 +90,7 @@ public function getTypeDefinition(
);

// Add searchable fields
$object = $source->getType();
$fields = $object instanceof Type
? $object->getFields()
: $object->fields;
$fields = $this->getFields($manipulator, $source, $context);

foreach ($fields as $field) {
// Name should be unique (may conflict with Type's operators)
Expand Down Expand Up @@ -143,6 +143,22 @@ protected function getOperators(
return $operators;
}

/**
* @return iterable<array-key, FieldDefinition|FieldDefinitionNode|InputObjectField|InputValueDefinitionNode>
*/
protected function getFields(
Manipulator $manipulator,
InterfaceSource|InputSource|ObjectSource $source,
Context $context,
): iterable {
$object = $source->getType();
$fields = $object instanceof Type
? $object->getFields()
: $object->fields;

return $fields;
}

/**
* Determines if the field can be converted or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions;

use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\FieldObject;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Child;

class SearchByOperatorFieldObjectDirective extends FieldObject {
class SearchByOperatorChildDirective extends Child {
// Lighthouse loads all classes from directive namespace this leads to
// 'Class "Orchestra\Testbench\TestCase" not found' error for our *Test
// classes. This class required to avoid this error.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions;

use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Condition;

class SearchByOperatorConditionDirective extends Condition {
// Lighthouse loads all classes from directive namespace this leads to
// 'Class "Orchestra\Testbench\TestCase" not found' error for our *Test
// classes. This class required to avoid this error.
}
Loading

0 comments on commit 693df03

Please sign in to comment.