Skip to content

Commit c358b53

Browse files
committed
Properly injecting the language context.
1 parent 716490a commit c358b53

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

graphql.services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ services:
124124
- '@container.namespaces'
125125
- '@module_handler'
126126
- '@cache.graphql.definitions'
127+
- '@graphql.language_context'
127128
- '\Drupal\graphql\Plugin\FieldPluginInterface'
128129
- '\Drupal\graphql\Annotation\GraphQLField'
129130
- '%graphql.config%'

src/Plugin/FieldPluginManager.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Cache\CacheBackendInterface;
77
use Drupal\Core\Extension\ModuleHandlerInterface;
88
use Drupal\Core\Plugin\DefaultPluginManager;
9+
use Drupal\graphql\GraphQLLanguageContext;
910

1011
class FieldPluginManager extends DefaultPluginManager {
1112

@@ -16,6 +17,15 @@ class FieldPluginManager extends DefaultPluginManager {
1617
*/
1718
protected $instances;
1819

20+
/**
21+
* The language context.
22+
*
23+
* Will be passed on to all plugin instances.
24+
*
25+
* @var \Drupal\graphql\GraphQLLanguageContext
26+
*/
27+
protected $languageContext;
28+
1929
/**
2030
* FieldPluginManager constructor.
2131
*
@@ -28,6 +38,8 @@ class FieldPluginManager extends DefaultPluginManager {
2838
* The module handler.
2939
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
3040
* The cache backend.
41+
* @param \Drupal\graphql\GraphQLLanguageContext $languageContext
42+
* The language context.
3143
* @param string|null $pluginInterface
3244
* The interface each plugin should implement.
3345
* @param string $pluginAnnotationName
@@ -40,10 +52,12 @@ public function __construct(
4052
\Traversable $namespaces,
4153
ModuleHandlerInterface $moduleHandler,
4254
CacheBackendInterface $cacheBackend,
55+
GraphQLLanguageContext $languageContext,
4356
$pluginInterface,
4457
$pluginAnnotationName,
4558
array $config
4659
) {
60+
4761
parent::__construct(
4862
$pluginSubdirectory,
4963
$namespaces,
@@ -52,11 +66,22 @@ public function __construct(
5266
$pluginAnnotationName
5367
);
5468

69+
$this->languageContext = $languageContext;
5570
$this->alterInfo('graphql_fields');
5671
$this->useCaches(empty($config['development']));
5772
$this->setCacheBackend($cacheBackend, 'fields', ['graphql']);
5873
}
5974

75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function createInstance($plugin_id, array $configuration = []) {
79+
/** @var \Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase $plugin */
80+
$plugin = parent::createInstance($plugin_id, $configuration);
81+
$plugin->setLanguageContext($this->languageContext);
82+
return $plugin;
83+
}
84+
6085
/**
6186
* {@inheritdoc}
6287
*/

src/Plugin/GraphQL/Fields/FieldPluginBase.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use Drupal\Component\Render\MarkupInterface;
77
use Drupal\Core\Cache\CacheableDependencyInterface;
88
use Drupal\Core\Cache\CacheableMetadata;
9+
use Drupal\Core\Language\LanguageManagerInterface;
910
use Drupal\graphql\GraphQL\Execution\ResolveContext;
1011
use Drupal\graphql\GraphQL\ValueWrapperInterface;
12+
use Drupal\graphql\GraphQLLanguageContext;
1113
use Drupal\graphql\Plugin\FieldPluginInterface;
1214
use Drupal\graphql\Plugin\FieldPluginManager;
1315
use Drupal\graphql\Plugin\GraphQL\Traits\ArgumentAwarePluginTrait;
@@ -29,6 +31,23 @@ abstract class FieldPluginBase extends PluginBase implements FieldPluginInterfac
2931
use ArgumentAwarePluginTrait;
3032
use DeprecatablePluginTrait;
3133

34+
/**
35+
* The language context, for simulating active languages in fields.
36+
*
37+
* @var \Drupal\graphql\GraphQLLanguageContext
38+
*/
39+
protected $languageContext;
40+
41+
/**
42+
* Set the language context instance.
43+
*
44+
* @param \Drupal\graphql\GraphQLLanguageContext $languageContext
45+
* The language context instance.
46+
*/
47+
public function setLanguageContext(GraphQLLanguageContext $languageContext) {
48+
$this->languageContext = $languageContext;
49+
}
50+
3251
/**
3352
* {@inheritdoc}
3453
*/
@@ -83,7 +102,7 @@ public function resolve($value, array $args, ResolveContext $context, ResolveInf
83102
}
84103
}
85104

86-
return \Drupal::service('graphql.language_context')
105+
return $this->languageContext
87106
->executeInLanguageContext(function () use ($value, $args, $context, $info) {
88107
return $this->resolveDeferred([$this, 'resolveValues'], $value, $args, $context, $info);
89108
}, $context->getContext('language', $info));

0 commit comments

Comments
 (0)