Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.2.2 #5

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

## [0.2.2] - 2024-01-26
### Added
- Support for having conditions
- Literal condition values for first party mode

### Changed
- Order name can be unescaped in first party mode

## [0.2.1] - 2023-07-03
### Added
- Null and NotNull where comparators
Expand All @@ -24,7 +32,8 @@
- Nette database schema loader
- Nette database resolvers

[Unreleased]: https://github.com/efabrica-team/nette-graphql/compare/0.2.1...main
[Unreleased]: https://github.com/efabrica-team/nette-graphql/compare/0.2.2...main
[0.2.2]: https://github.com/efabrica-team/nette-graphql/compare/0.2.1...0.2.2
[0.2.1]: https://github.com/efabrica-team/nette-graphql/compare/0.2.0...0.2.1
[0.2.0]: https://github.com/efabrica-team/nette-graphql/compare/0.1.0...0.2.0
[0.1.0]: https://github.com/efabrica-team/nette-graphql/compare/0.0.0...0.1.0
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
],
"require": {
"php": "^7.4|^8.0",
"efabrica/graphql": "^0.2.1",
"ext-pdo": "*",
"efabrica/graphql": "0.2.2",
"nette/database": "^3.1",
"symfony/string": "^5.4",
"nette/di": "^3.1",
"ext-pdo": "*"
"nette/di": "^3.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
189 changes: 177 additions & 12 deletions src/Resolvers/NetteDatabase/DatabaseResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

use Efabrica\GraphQL\Exceptions\ResolverException;
use Efabrica\GraphQL\Helpers\AdditionalResponseData;
use Efabrica\GraphQL\Nette\Schema\Custom\Types\LiteralType;
use Efabrica\GraphQL\Resolvers\ResolverInterface;
use Efabrica\GraphQL\Schema\Custom\Arguments\ConditionsArgument;
use Efabrica\GraphQL\Schema\Custom\Arguments\OrderArgument;
use Efabrica\GraphQL\Schema\Custom\Arguments\PaginationArgument;
use Efabrica\GraphQL\Schema\Custom\Fields\GroupField;
use Efabrica\GraphQL\Schema\Custom\Fields\HavingAndField;
use Efabrica\GraphQL\Schema\Custom\Fields\HavingOrField;
use Efabrica\GraphQL\Schema\Custom\Fields\WhereAndField;
use Efabrica\GraphQL\Schema\Custom\Fields\WhereOrField;
use Efabrica\GraphQL\Schema\Custom\Types\GroupType;
use Efabrica\GraphQL\Schema\Custom\Types\HavingType;
use Efabrica\GraphQL\Schema\Custom\Types\OrderDirectionEnum;
use Efabrica\GraphQL\Schema\Custom\Types\WhereComparatorEnum;
use Efabrica\GraphQL\Schema\Custom\Types\WhereType;
Expand Down Expand Up @@ -54,17 +59,33 @@ protected function applyOrderToSelection(Selection $selection, array $args): voi
{
foreach ($args[OrderArgument::NAME] ?? [] as $orderArgs) {
$orderBy = $orderArgs[OrderArgument::FIELD_KEY] ?? null;
if ($orderBy) {
$order = $orderArgs[OrderArgument::FIELD_ORDER] ?? null;
$order = $orderArgs[OrderArgument::FIELD_ORDER] ?? null;

if ($order === OrderDirectionEnum::RAND) {
$selection->order('RAND()');
} elseif ($order === OrderDirectionEnum::DESC) {
$selection->order('?name DESC', $orderBy);
} else {
$selection->order('?name ASC', $orderBy);
}
if ($order === OrderDirectionEnum::RAND) {
$selection->order('RAND()');
continue;
}

if ($orderBy === null) {
continue;
}

$orderQuery = '';
$parameters = [];

if ($this->firstParty) {
$orderQuery = $orderBy;
} else {
$orderQuery .= ' ?name';
$parameters[] = $orderBy;
}

if ($order === OrderDirectionEnum::DESC) {
$orderQuery .= ' DESC';
} else {
$orderQuery .= ' ASC';
}
$selection->order($orderQuery, ...$parameters);
}
}

Expand All @@ -87,6 +108,25 @@ protected function applyConditionsToSelection(Selection $selection, array $args)
if ($whereQuery) {
$selection->where($whereQuery, ...$whereParameters);
}

[$havingAndQuery, $havingAndParameters] = $this->buildHavingQuery(
$selection,
$args[ConditionsArgument::NAME][HavingAndField::NAME] ?? [],
);

[$havingOrQuery, $havingOrParameters] = $this->buildHavingQuery(
$selection,
$args[ConditionsArgument::NAME][HavingOrField::NAME] ?? [],
'OR'
);

$havingQuery = implode(' AND ', array_filter([$havingAndQuery, $havingOrQuery]));
$havingParameters = array_merge($havingAndParameters, $havingOrParameters);

if ($havingQuery) {
$selection->having($havingQuery, ...$havingParameters);
}

$this->applyGroupToSelection($selection, $args[ConditionsArgument::NAME][GroupField::NAME] ?? []);
}

Expand Down Expand Up @@ -190,8 +230,15 @@ private function buildWhereQuery(Selection $selection, array $conditions, string
throw new ResolverException("'$comparator' is not a valid comparator.");
}

$conditionWhereQuery .= ' ?';
$parameters[] = $value ?? 'NULL';
if (LiteralType::isLiteral($value)) {
if (!$this->firstParty) {
throw new ResolverException("Literal values are not allowed when not in first party mode.");
}
$conditionWhereQuery .= ' ' . LiteralType::getLiteralValue($value);
} else {
$conditionWhereQuery .= ' ?';
$parameters[] = $value ?? 'NULL';
}
}
}

Expand All @@ -201,11 +248,129 @@ private function buildWhereQuery(Selection $selection, array $conditions, string
return [$whereQuery, $parameters];
}

private function buildHavingQuery(Selection $selection, array $conditions, string $type = 'AND'): array
{
$havingQuery = '';
$parameters = [];

foreach ($conditions as $condition) {
$conditionHavingQuery = '';
if (!empty($havingQuery)) {
$conditionHavingQuery .= ' ' . $type;
}

$andSubHaving = $condition[HavingAndField::NAME] ?? [];
$orSubHaving = $condition[HavingOrField::NAME] ?? [];

if (count($andSubHaving) || count($orSubHaving)) {
$conditionAndHavingQuery = null;
$conditionOrHavingQuery = null;

[$subquery, $subparameters] = $this->buildHavingQuery($selection, $andSubHaving);
if ($subquery) {
$conditionAndHavingQuery .= ' (' . $subquery . ')';
$parameters = array_merge($parameters, $subparameters);
}

[$subquery, $subparameters] = $this->buildHavingQuery($selection, $orSubHaving, 'OR');
if ($subquery) {
$conditionOrHavingQuery .= ' (' . $subquery . ')';
$parameters = array_merge($parameters, $subparameters);
}

$conditionHavingQuery .= implode(
' AND ',
array_filter([$conditionAndHavingQuery, $conditionOrHavingQuery])
);
} elseif ($condition[HavingType::FIELD_COLUMN] !== null) {
if ($this->firstParty) {
// SQL INJECTION
$conditionHavingQuery .= ' ' . $condition[HavingType::FIELD_COLUMN];
} else {
// Will not join tables automaticaly when using variables nor will allow aggregate functions.
$conditionHavingQuery .= ' ?name';
$parameters[] = $condition[HavingType::FIELD_COLUMN];
}

$comparator = $condition[HavingType::FIELD_COMPARATOR];
$value = $condition[HavingType::FIELD_VALUE] ?? null;

if (in_array($comparator, [WhereComparatorEnum::IN, WhereComparatorEnum::NOT_IN], true)) {
switch ($comparator) {
case WhereComparatorEnum::IN:
$conditionHavingQuery .= ' IN (?)';
break;
case WhereComparatorEnum::NOT_IN:
if (!$value || !count($value = array_filter($value))) {
continue 2;
}
$conditionHavingQuery .= ' NOT IN (?)';
break;
}
$parameters[] = $value;
} elseif(in_array($comparator, [WhereComparatorEnum::NULL, WhereComparatorEnum::NOT_NULL], true)) {
switch ($comparator) {
case WhereComparatorEnum::NULL:
$conditionHavingQuery .= ' IS NULL';
break;
case WhereComparatorEnum::NOT_NULL:
$conditionHavingQuery .= ' IS NOT NULL';
break;
}
} else {
$value = $value ? reset($value) : null;
switch ($comparator) {
case WhereComparatorEnum::EQUAL:
$conditionHavingQuery .= ' =';
break;
case WhereComparatorEnum::NOT_EQUAL:
$conditionHavingQuery .= ' !=';
break;
case WhereComparatorEnum::LESS_THAN:
$conditionHavingQuery .= ' <';
break;
case WhereComparatorEnum::LESS_THAN_EQUAL:
$conditionHavingQuery .= ' <=';
break;
case WhereComparatorEnum::MORE_THAN:
$conditionHavingQuery .= ' >';
break;
case WhereComparatorEnum::MORE_THAN_EQUAL:
$conditionHavingQuery .= ' >=';
break;
case WhereComparatorEnum::LIKE:
$conditionHavingQuery .= ' LIKE';
break;
case WhereComparatorEnum::NOT_LIKE:
$conditionHavingQuery .= ' NOT LIKE';
break;
default:
throw new ResolverException("'$comparator' is not a valid comparator.");
}

if (LiteralType::isLiteral($value)) {
if (!$this->firstParty) {
throw new ResolverException('Literal values are not allowed when not in first party mode.');
}
$conditionHavingQuery .= ' ' . LiteralType::getLiteralValue($value);
} else {
$conditionHavingQuery .= ' ?';
$parameters[] = $value ?? 'NULL';
}
}
}

$havingQuery .= $conditionHavingQuery;
}

return [$havingQuery, $parameters];
}

private function applyGroupToSelection(Selection $selection, array $conditions): void
{
$groupBy = [];
foreach ($conditions as $condition) {
$groupBy[] = $condition[GroupField::FIELD_COLUMN];
$groupBy[] = $condition[GroupType::FIELD_COLUMN];
}

if ($this->firstParty) {
Expand Down
16 changes: 16 additions & 0 deletions src/Schema/Custom/Types/LiteralType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Efabrica\GraphQL\Nette\Schema\Custom\Types;

class LiteralType
{
public static function isLiteral(string $value): bool
{
return str_starts_with($value, 'LITERAL:');
}

public static function getLiteralValue(string $value): string
{
return substr($value, 8);
}
}
Loading