Skip to content

Commit

Permalink
feat: Add the ability to have multiple **and** where queries by acc…
Browse files Browse the repository at this point in the history
…epting an array of arguments, closes ([#25](#25))
  • Loading branch information
khalwat committed Jan 25, 2025
1 parent 963f792 commit 72d134d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ So for example, this query:
{
entry(section: "homepage") {
... on homepage_homepage_Entry {
someDamAsset(where: {key: "default.Author", value: "Hernan Stockebrand"}, sortByDesc: { field: "default.Size" }) {
someDamAsset(where: {key: "default.Author", value: "Hernan Stockebrand"}, sortByDesc: { field: "default.Size", flags: SORT_NUMERIC }) {
id,
url {
directUrlOriginal
Expand Down Expand Up @@ -385,20 +385,20 @@ Here's a list of the available arguments, and the types they expect as parameter
* [`forPage`](https://laravel.com/docs/10.x/collections#method-forpage): `ForPageInput` - Paginate the items by page number and items per page.


* [`where`](https://laravel.com/docs/10.x/collections#method-where): `WhereFiltersInput` - Get all items by the given key value pair, using the optional operator for comparison.
* [`where`](https://laravel.com/docs/10.x/collections#method-where): `[WhereFiltersInput]` - Get all items by the given key value pair, using the optional operator for comparison.


* [`whereBetween`](https://laravel.com/docs/10.x/collections#method-wherebetween): `WhereBetweenFiltersInput`
* [`whereBetween`](https://laravel.com/docs/10.x/collections#method-wherebetween): `[WhereBetweenFiltersInput]`
Filter items such that the value of the given key is between the given values.


* [`whereIn`](https://laravel.com/docs/10.x/collections#method-wherein): `WhereInFiltersInput` - Filter items such that the value of the given key is in the array of values provided.
* [`whereIn`](https://laravel.com/docs/10.x/collections#method-wherein): `[WhereInFiltersInput]` - Filter items such that the value of the given key is in the array of values provided.


* [`whereNotBetween`](https://laravel.com/docs/10.x/collections#method-wherenotbetween): `WhereNotBetweenFiltersInput` - Filter items such that the value of the given key is NOT between the given values. This argument expects exactly three values in an array. You can use the field.subField syntax for nested fields.
* [`whereNotBetween`](https://laravel.com/docs/10.x/collections#method-wherenotbetween): `[WhereNotBetweenFiltersInput]` - Filter items such that the value of the given key is NOT between the given values. This argument expects exactly three values in an array. You can use the field.subField syntax for nested fields.


* [`whereNotIn`](https://laravel.com/docs/10.x/collections#method-wherenotin): `WhereNotInFiltersInput` - Filter items by the given key value pair, making sure the value is not in the array.
* [`whereNotIn`](https://laravel.com/docs/10.x/collections#method-wherenotin): `[WhereNotInFiltersInput]` - Filter items by the given key value pair, making sure the value is not in the array.


* [`whereNotNull`](https://laravel.com/docs/10.x/collections#method-wherenotnull): `String` - Return items from the collection where the given key is not null. You can use the `field.subField` syntax for nested fields.
Expand Down
24 changes: 12 additions & 12 deletions src/fields/CantoDamAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ protected function getGqlArguments(): array
'whereContainsIn' => [
'name' => 'whereContainsIn',
'description' => 'Look across the given key-values and return fuzzy match on a single search term',
'type' => new InputObjectType([
'type' => Type::listOf(new InputObjectType([
'name' => 'WhereContainsInFilterInput',
'fields' => [
'keys' => [
Expand All @@ -402,12 +402,12 @@ protected function getGqlArguments(): array
'description' => 'The value that should be fuzzy matched in the key-values'
],
]
]),
])),
],
'where' => [
'name' => 'where',
'description' => 'Get all items by the given key value pair, using the optional operator for comparison. (See https://laravel.com/docs/10.x/collections#method-where).',
'type' => new InputObjectType([
'type' => Type::listOf(new InputObjectType([
'name' => 'WhereFiltersInput',
'fields' => [
'key' => [
Expand All @@ -423,7 +423,7 @@ protected function getGqlArguments(): array
'description' => 'The comparison operator to use, e.g.: `=`, `>`, `<=`, etc. The default is `=`',
],
],
]),
])),
],
'whereNull' => [
'name' => 'whereNull',
Expand All @@ -438,7 +438,7 @@ protected function getGqlArguments(): array
'whereIn' => [
'name' => 'whereIn',
'description' => 'Filter items such that the value of the given key is in the array of values provided. (See https://laravel.com/docs/10.x/collections#method-wherein).',
'type' => new InputObjectType([
'type' => Type::listOf(new InputObjectType([
'name' => 'WhereInFiltersInput',
'fields' => [
'key' => [
Expand All @@ -450,12 +450,12 @@ protected function getGqlArguments(): array
'description' => 'The values that should be in the key',
],
],
]),
])),
],
'whereNotIn' => [
'name' => 'whereNotIn',
'description' => 'Filter items by the given key value pair, making sure the value is NOT in the array. (See https://laravel.com/docs/10.x/collections#method-wherenotin).',
'type' => new InputObjectType([
'type' => Type::listOf(new InputObjectType([
'name' => 'WhereNotInFiltersInput',
'fields' => [
'key' => [
Expand All @@ -467,12 +467,12 @@ protected function getGqlArguments(): array
'description' => 'The the values that should not be in the key',
],
],
]),
])),
],
'whereBetween' => [
'name' => 'whereBetween',
'description' => 'Filter items such that the value of the given key is between the given values. (See https://laravel.com/docs/10.x/collections#method-wherebetween).',
'type' => new InputObjectType([
'type' => Type::listOf(new InputObjectType([
'name' => 'WhereBetweenFiltersInput',
'fields' => [
'key' => [
Expand All @@ -484,12 +484,12 @@ protected function getGqlArguments(): array
'description' => 'The values that the key should be between',
],
],
]),
])),
],
'whereNotBetween' => [
'name' => 'whereNotBetween',
'description' => 'Filter items such that the value of the given key is not between the given values. (See https://laravel.com/docs/10.x/collections#method-wherenotbetween).',
'type' => new InputObjectType([
'type' => Type::listOf(new InputObjectType([
'name' => 'WhereNotBetweenFiltersInput',
'fields' => [
'key' => [
Expand All @@ -501,7 +501,7 @@ protected function getGqlArguments(): array
'description' => 'The values the key should not be between',
],
],
]),
])),
],
], 'CantoDamAssetQueryType');
}
Expand Down
55 changes: 42 additions & 13 deletions src/gql/resolvers/CantoDamAssetResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,17 @@ protected static function forPageArgs(Collection $collection, array $arguments,

protected static function whereArgs(Collection $collection, array $arguments, string $arg): Collection
{
return $collection->$arg(
$arguments[$arg]['key'] ?? null,
$arguments[$arg]['operator'] ?? null,
$arguments[$arg]['value'] ?? null
);
// Allow for an array of where arguments to be passed in
$argValues = self::isArrayList($arguments[$arg]) ? $arguments[$arg] : [$arguments[$arg]];
foreach ($argValues as $argValue) {
$collection = $collection->$arg(
$argValue['key'] ?? null,
$argValue['operator'] ?? null,
$argValue['value'] ?? null
);
}

return $collection;
}

protected static function sortArgs(Collection $collection, array $arguments, string $arg): Collection
Expand All @@ -106,18 +112,30 @@ protected static function sortArgs(Collection $collection, array $arguments, str

protected static function whereContainsInArgs(Collection $collection, array $arguments, string $arg): Collection
{
return $collection->$arg(
$arguments[$arg]['keys'] ?? null,
$arguments[$arg]['value'] ?? null
);
// Allow for an array of where arguments to be passed in
$argValues = self::isArrayList($arguments[$arg]) ? $arguments[$arg] : [$arguments[$arg]];
foreach ($argValues as $argValue) {
$collection = $collection->$arg(
$argValue['keys'] ?? null,
$argValue['value'] ?? null
);
}

return $collection;
}

protected static function whereArrayArgs(Collection $collection, array $arguments, string $arg): Collection
{
return $collection->$arg(
$arguments[$arg]['key'] ?? null,
$arguments[$arg]['values'] ?? null
);
// Allow for an array of where arguments to be passed in
$argValues = self::isArrayList($arguments[$arg]) ? $arguments[$arg] : [$arguments[$arg]];
foreach ($argValues as $argValue) {
$collection = $collection->$arg(
$argValue['key'] ?? null,
$argValue['values'] ?? null
);
}

return $collection;
}

protected static function simpleArgs(Collection $collection, array $arguments, string $arg): Collection
Expand All @@ -129,4 +147,15 @@ protected static function noArgs(Collection $collection, array $arguments, strin
{
return new Collection([$collection->$arg(null)]);
}

protected static function isArrayList(mixed $arr)
{
if (!is_array($arr)) {
return false;
}
if ($arr === []) {
return true;
}
return array_keys($arr) === range(0, count($arr) - 1);
}
}

0 comments on commit 72d134d

Please sign in to comment.