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

BUGFIX: Allow applyToReadingState to fail gracefully and defer to applyToList #323

Open
wants to merge 1 commit into
base: 1
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions src/GraphQL/Resolvers/VersionFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ public function applyToReadingState(array $versioningArgs)
switch ($mode) {
case Versioned::LIVE:
case Versioned::DRAFT:
Versioned::set_stage($mode);
break;
case 'latest_versions':
case 'all_versions':
Versioned::set_stage($mode);
Versioned::set_reading_mode($mode);
break;
case 'archive':
$date = $versioningArgs['archiveDate'];
Expand Down Expand Up @@ -186,6 +188,8 @@ public function validateArgs(array $versioningArgs)
switch ($mode) {
case Versioned::LIVE:
case Versioned::DRAFT:
case 'all_versions':
case 'latest_versions':
break;
case 'archive':
if (empty($versioningArgs['archiveDate'])) {
Expand All @@ -202,10 +206,6 @@ public function validateArgs(array $versioningArgs)
));
}
break;
case 'all_versions':
break;
case 'latest_versions':
break;
case 'status':
if (empty($versioningArgs['status'])) {
throw new InvalidArgumentException(sprintf(
Expand Down
16 changes: 11 additions & 5 deletions src/GraphQL/Resolvers/VersionedResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use GraphQL\Type\Definition\ResolveInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\GraphQL\QueryHandler\QueryHandler;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use SilverStripe\GraphQL\Resolvers\VersionFilters;
use SilverStripe\ORM\DataList;
Expand Down Expand Up @@ -101,11 +100,18 @@ public static function resolveVersionedRead(DataList $list, array $args, array $
return $list;
}

// Set the reading state globally, we don't support mixing versioned states in the same query
Injector::inst()->get(VersionFilters::class)
->applyToReadingState($args['versioning']);
// Set the reading state globally, we don't support mixing versioned
// states in the same query
try {
Injector::inst()->get(VersionFilters::class)
->applyToReadingState($args['versioning']);
} catch (InvalidArgumentException $e) {
// We don't really care if the args don't apply to the global state,
// because if they don't work there, they'll work on the list in the next,
// call, unless it's something totally unexpected, which will throw.
}

// Also set on the specific list
// Set on the specific list
$list = Injector::inst()->get(VersionFilters::class)
->applyToList($list, $args['versioning']);

Expand Down