Skip to content

Commit

Permalink
Make sure there are conditions, or return an empty result set
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Feb 13, 2024
1 parent 74e18c5 commit 449ecea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 5 additions & 7 deletions src/gql/resolvers/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public static function prepareQuery(mixed $source, array $arguments, ?string $fi
if ($source === null) {
$query = EntryElement::find();
$pairs = GqlHelper::extractAllowedEntitiesFromSchema('read');

if (!isset($pairs['sections']) && !isset($pairs['nestedentryfields'])) {
return ElementCollection::empty();
}

$condition = ['or'];
$condition = [];

if (isset($pairs['sections'])) {
$entriesService = Craft::$app->getEntries();
Expand All @@ -62,8 +57,11 @@ public static function prepareQuery(mixed $source, array $arguments, ?string $fi
}
}

$query->andWhere($condition);
if (empty($condition)) {
return ElementCollection::empty();
}

$query->andWhere(['or', ...$condition]);
// If not, get the prepared element query
} else {
$query = $source->$fieldName;
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/gql/PrepareQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\db\Table;
use craft\elements\ElementCollection;
use craft\enums\PropagationMethod;
use craft\gql\resolvers\elements\Asset as AssetResolver;
use craft\gql\resolvers\elements\Category as CategoryResolver;
Expand Down Expand Up @@ -189,7 +190,11 @@ public function relationalFieldQueryPreparationProvider(): array
[
EntryResolver::class, [null, []], function($result) {
$section = Craft::$app->getEntries()->getSectionByUid(self::SECTION_UID);
return $result->where === ['or', ['in', 'entries.sectionId', array_filter([$section?->id])]];
// todo: figure out why $section is null during CI
if (!$section) {
return $result instanceof ElementCollection && $result->isEmpty();
}
return $result->where === ['or', ['in', 'entries.sectionId', [$section->id]]];
},
],

Expand Down

0 comments on commit 449ecea

Please sign in to comment.