Skip to content

Commit 7609edd

Browse files
committed
Rework of not: yes.
Changes: * Comparison operators: `{eq: 1, not: yes}` => `{notEqual: 1}` * Logical: `{anyOf: [...], not: yes}` => `not { anyOf: [...]} The `not` became more consistent with `allOf`/`anyOf` and compatible with [Tagged Type](https://github.com/graphql/graphql-spec/pull/733)/[oneOf input](graphql/graphql-spec#825). Also resolved ambiguity for Relation: ``` # this means "where count(...) != 2" now relation: { where: {....} eq: 2 not: yes } ```
1 parent ef6c97a commit 7609edd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1226
-646
lines changed

Diff for: readme.md

+63-23
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,23 @@ That's all, just search 😃
6161
query {
6262
# WHERE name = "LastDragon"
6363
users(where: {
64-
name: {eq: "LastDragon"}
64+
name: {equal: "LastDragon"}
6565
}) {
6666
id
6767
}
6868

6969
# WHERE name != "LastDragon"
7070
users(where: {
71-
name: {eq: "LastDragon", not: yes}
71+
name: {notEqual: "LastDragon"}
7272
}) {
7373
id
7474
}
7575

7676
# WHERE name = "LastDragon" or name = "Aleksei"
7777
users(where: {
7878
anyOf: [
79-
{name: {eq: "LastDragon"}}
80-
{name: {eq: "Aleksei"}}
79+
{name: {equal: "LastDragon"}}
80+
{name: {equal: "Aleksei"}}
8181
]
8282
}) {
8383
id
@@ -86,8 +86,8 @@ query {
8686
# WHERE NOT (name = "LastDragon" or name = "Aleksei")
8787
users(where: {
8888
anyOf: [
89-
{name: {eq: "LastDragon"}}
90-
{name: {eq: "Aleksei"}}
89+
{name: {equal: "LastDragon"}}
90+
{name: {equal: "Aleksei"}}
9191
]
9292
not: yes
9393
}) {
@@ -118,7 +118,7 @@ query {
118118
where: {
119119
date: {between: {min: "2021-01-01", max: "2021-04-01"}}
120120
}
121-
eq: 2
121+
equal: 2
122122
}
123123
}) {
124124
id
@@ -154,7 +154,7 @@ input SearchByConditionCommentsQuery {
154154
anyOf: [SearchByConditionCommentsQuery!]
155155

156156
"""Not."""
157-
not: SearchByFlag
157+
not: SearchByConditionCommentsQuery
158158

159159
"""Property condition."""
160160
text: SearchByScalarString
@@ -177,7 +177,7 @@ input SearchByConditionUsersQuery {
177177
anyOf: [SearchByConditionUsersQuery!]
178178

179179
"""Not."""
180-
not: SearchByFlag
180+
not: SearchByConditionUsersQuery
181181

182182
"""Property condition."""
183183
id: SearchByScalarID
@@ -193,11 +193,30 @@ enum SearchByFlag {
193193

194194
"""Relation condition for input UsersQuery."""
195195
input SearchByRelationUsersQuery {
196-
"""Conditions for the related objects."""
196+
"""
197+
Conditions for the related objects (`has()`).
198+
199+
See also:
200+
* https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
201+
* https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-absence
202+
"""
197203
where: SearchByConditionUsersQuery!
198204

205+
"""
206+
Shortcut for `doesntHave()`, same as:
207+
208+
\```
209+
where: [...]
210+
lt: 1
211+
\```
212+
"""
213+
not: Boolean! = false
214+
199215
"""Equal (`=`)."""
200-
eq: Int
216+
equal: Int
217+
218+
"""Not Equal (`!=`)."""
219+
notEqual: Int
201220

202221
"""Less than (`<`)."""
203222
lt: Int
@@ -210,22 +229,27 @@ input SearchByRelationUsersQuery {
210229

211230
"""Greater than or equal to (`>=`)."""
212231
gte: Int
213-
214-
"""Not."""
215-
not: SearchByFlag
216232
}
217233

218234
input SearchByScalarDateOperatorBetween {
219235
min: Date!
220236
max: Date!
221237
}
222238

239+
input SearchByScalarDateOperatorNotBetween {
240+
min: Date!
241+
max: Date!
242+
}
243+
223244
"""
224245
Available operators for scalar Date (only one operator allowed at a time).
225246
"""
226247
input SearchByScalarDateOrNull {
227248
"""Equal (`=`)."""
228-
eq: Date
249+
equal: Date
250+
251+
"""Not Equal (`!=`)."""
252+
notEqual: Date
229253

230254
"""Less than (`<`)."""
231255
lt: Date
@@ -242,51 +266,67 @@ input SearchByScalarDateOrNull {
242266
"""Within a set of values."""
243267
in: [Date!]
244268

269+
"""Outside a set of values."""
270+
notIn: [Date!]
271+
245272
"""Within a range."""
246273
between: SearchByScalarDateOperatorBetween
247274

275+
"""Outside a range."""
276+
notBetween: SearchByScalarDateOperatorNotBetween
277+
248278
"""Is NULL?"""
249279
isNull: SearchByFlag
250280

251-
"""Not."""
252-
not: SearchByFlag
281+
"""Is NOT NULL?"""
282+
isNotNull: SearchByFlag
253283
}
254284

255285
"""
256286
Available operators for scalar ID! (only one operator allowed at a time).
257287
"""
258288
input SearchByScalarID {
259289
"""Equal (`=`)."""
260-
eq: ID
290+
equal: ID
291+
292+
"""Not Equal (`!=`)."""
293+
notEqual: ID
261294

262295
"""Within a set of values."""
263296
in: [ID!]
264297

265-
"""Not."""
266-
not: SearchByFlag
298+
"""Outside a set of values."""
299+
notIn: [ID!]
267300
}
268301

269302
"""
270303
Available operators for scalar String! (only one operator allowed at a time).
271304
"""
272305
input SearchByScalarString {
273306
"""Equal (`=`)."""
274-
eq: String
307+
equal: String
308+
309+
"""Not Equal (`!=`)."""
310+
notEqual: String
275311

276312
"""Like."""
277313
like: String
278314

315+
"""Not like."""
316+
notLike: String
317+
279318
"""Within a set of values."""
280319
in: [String!]
281320

282-
"""Not."""
283-
not: SearchByFlag
321+
"""Outside a set of values."""
322+
notIn: [String!]
284323
}
285324

286325
input UsersQuery {
287326
id: ID!
288327
name: String!
289328
}
329+
290330
```
291331

292332
</details>

Diff for: src/SearchBy/AstManipulator.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,21 @@
1010
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
1111
use GraphQL\Language\Parser;
1212
use Illuminate\Contracts\Container\Container;
13-
use Illuminate\Support\Arr;
1413
use Illuminate\Support\Str;
1514
use LastDragon_ru\LaraASP\GraphQL\AstManipulator as BaseAstManipulator;
1615
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator;
1716
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\OperatorHasTypes;
1817
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\OperatorHasTypesForScalar;
1918
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\OperatorHasTypesForScalarNullable;
20-
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\OperatorNegationable;
19+
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\IsNotNull;
2120
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\IsNull;
2221
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\Relation;
23-
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Not;
2422
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
2523
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
2624

2725
use function array_map;
2826
use function array_merge;
2927
use function implode;
30-
use function is_a;
3128
use function is_array;
3229
use function is_null;
3330
use function sprintf;
@@ -394,11 +391,7 @@ protected function getScalarOperators(string $scalar, bool $nullable): array {
394391
// Add `null` for nullable
395392
if ($nullable) {
396393
$operators[] = IsNull::class;
397-
}
398-
399-
// Add `not` for negationable
400-
if (Arr::first($operators, static fn(string $o) => is_a($o, OperatorNegationable::class, true))) {
401-
$operators[] = Not::class;
394+
$operators[] = IsNotNull::class;
402395
}
403396

404397
// Return

Diff for: src/SearchBy/Contracts/OperatorNegationable.php

-7
This file was deleted.

Diff for: src/SearchBy/Directive.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\LessThan;
1919
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\LessThanOrEqual;
2020
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\Like;
21+
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\NotBetween;
22+
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\NotEqual;
23+
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\NotIn;
24+
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\NotLike;
2125
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\Relation;
2226
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical\AllOf;
2327
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical\AnyOf;
24-
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Not;
28+
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical\Not;
2529
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
2630
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
2731
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
@@ -52,39 +56,51 @@ class Directive extends BaseDirective implements ArgManipulator, ArgBuilderDirec
5256
// Standard types
5357
'ID' => [
5458
Equal::class,
59+
NotEqual::class,
5560
In::class,
61+
NotIn::class,
5662
],
5763
'Int' => [
5864
Equal::class,
65+
NotEqual::class,
5966
LessThan::class,
6067
LessThanOrEqual::class,
6168
GreaterThan::class,
6269
GreaterThanOrEqual::class,
6370
In::class,
71+
NotIn::class,
6472
Between::class,
73+
NotBetween::class,
6574
],
6675
'Float' => 'Int',
6776
'Boolean' => [
6877
Equal::class,
6978
],
7079
'String' => [
7180
Equal::class,
81+
NotEqual::class,
7282
Like::class,
83+
NotLike::class,
7384
In::class,
85+
NotIn::class,
7486
],
7587

7688
// Special types
7789
self::Enum => [
7890
Equal::class,
91+
NotEqual::class,
7992
In::class,
93+
NotIn::class,
8094
],
8195
self::Logic => [
8296
AllOf::class,
8397
AnyOf::class,
98+
Not::class,
8499
],
85100
self::Relation => [
86101
Relation::class,
87102
Equal::class,
103+
NotEqual::class,
88104
LessThan::class,
89105
LessThanOrEqual::class,
90106
GreaterThan::class,
@@ -146,7 +162,6 @@ public function handleBuilder($builder, $value): EloquentBuilder|QueryBuilder {
146162
return (new SearchBuilder(
147163
$this->translator,
148164
(new Collection($this->scalars))
149-
->add(Not::class)
150165
->flatten()
151166
->unique()
152167
->filter(static function (string $operator): bool {

Diff for: src/SearchBy/DirectiveTest.php

+16-17
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,24 @@ public function dataProviderHandleBuilder(): array {
7070
'valid condition' => [
7171
true,
7272
[
73-
'not' => 'yes',
74-
'allOf' => [
75-
[
76-
'a' => [
77-
'eq' => 1,
78-
'not' => 'yes',
73+
'not' => [
74+
'allOf' => [
75+
[
76+
'a' => [
77+
'notEqual' => 1,
78+
],
7979
],
80-
],
81-
[
82-
'anyOf' => [
83-
[
84-
'a' => [
85-
'eq' => 2,
80+
[
81+
'anyOf' => [
82+
[
83+
'a' => [
84+
'equal' => 2,
85+
],
8686
],
87-
],
88-
[
89-
'b' => [
90-
'eq' => 3,
91-
'not' => 'yes',
87+
[
88+
'b' => [
89+
'notEqual' => 3,
90+
],
9291
],
9392
],
9493
],

0 commit comments

Comments
 (0)