-
-
Save bummzack/cf71fa2e2311a8f5fd5d45f5e1e9f873 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config: | |
modelConfig: | |
DataObject: | |
operations: | |
read: | |
plugins: | |
fluentLocale: true | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SilverStripe\Core\Injector\Injector: | |
SilverStripe\GraphQL\Schema\Registry\PluginRegistry: | |
constructor: | |
- MyProject\GraphQL\FluentQueryPlugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyProject\GraphQL; | |
use SilverStripe\GraphQL\Schema\Field\ModelQuery; | |
use SilverStripe\GraphQL\Schema\Interfaces\ModelQueryPlugin; | |
use SilverStripe\GraphQL\Schema\Interfaces\SchemaUpdater; | |
use SilverStripe\GraphQL\Schema\Schema; | |
use SilverStripe\GraphQL\Schema\Type\Enum; | |
use TractorCow\Fluent\Model\Locale; | |
use TractorCow\Fluent\State\FluentState; | |
class FluentQueryPlugin implements ModelQueryPlugin, SchemaUpdater | |
{ | |
public function getIdentifier(): string | |
{ | |
return 'fluentLocale'; | |
} | |
public static function updateSchema(Schema $schema): void | |
{ | |
$locales = Locale::getLocales()->map('ID', 'Locale')->values(); | |
$locales = array_combine($locales, $locales); | |
$enum = Enum::create('FluentLocale', $locales, 'The locales available'); | |
$schema->addEnum($enum); | |
} | |
public function apply(ModelQuery $query, Schema $schema, array $config = []): void | |
{ | |
$queryType = $schema->getQueryType(); | |
// Ensure that we only apply the Locale variable to root queries. | |
// Nested queries with their own locales is super edge-casey and weird. | |
if (!$queryType->getFieldByName($query->getName())) { | |
return; | |
} | |
$query->addArg('locale', 'FluentLocale!'); | |
$query->addResolverMiddleware([static::class, 'setLocale']); | |
} | |
public static function setLocale($obj, $args, $context) | |
{ | |
FluentState::singleton()->setLocale($args['locale']); | |
return $obj; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query ReadPages($Locale: FluentLocale!) { | |
readPages(locale: $FluentLocale) { | |
nodes { title } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment