Skip to content

Commit 0b4d348

Browse files
committed
implement configuration flag
1 parent d4a35c6 commit 0b4d348

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

config/install/graphql.settings.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dataproducer_populate_default_values: true

config/schema/graphql.schema.yml

+10
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,13 @@ graphql.default_persisted_query_configuration:
6666

6767
plugin.plugin_configuration.persisted_query.*:
6868
type: graphql.default_persisted_query_configuration
69+
70+
graphql.settings:
71+
type: config_object
72+
label: "GraphQL Settings"
73+
mapping:
74+
# @todo Remove in GraphQL 5.
75+
dataproducer_populate_default_values:
76+
type: boolean
77+
label: "Populate dataproducer context default values"
78+
description: "Legacy setting: Populate dataproducer context default values before executing the resolve method. Set this to true to be future-proof. This setting is deprecated and will be removed in a future release."

graphql.install

+12
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,15 @@ function graphql_update_8001(): void {
6969
*/
7070
function graphql_update_8400() :void {
7171
}
72+
73+
/**
74+
* Preserve dataproducer default value behavior for old installations.
75+
*
76+
* Set dataproducer_populate_default_values to TRUE after you verified that your
77+
* dataproducers are still working with the new default value behavior.
78+
*/
79+
function graphql_update_10400() :void {
80+
\Drupal::configFactory()->getEditable('graphql.settings')
81+
->set('dataproducer_populate_default_values', FALSE)
82+
->save();
83+
}

src/Plugin/DataProducerPluginManager.php

+19
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class DataProducerPluginManager extends DefaultPluginManager {
3535
*/
3636
protected $resultCacheBackend;
3737

38+
/**
39+
* Backwards compatibility flag to populate context defaults or not.
40+
*
41+
* @todo Remove in 5.x.
42+
*/
43+
protected bool $populateContextDefaults = TRUE;
44+
3845
/**
3946
* DataProducerPluginManager constructor.
4047
*
@@ -83,6 +90,18 @@ public function __construct(
8390
$this->requestStack = $requestStack;
8491
$this->contextsManager = $contextsManager;
8592
$this->resultCacheBackend = $resultCacheBackend;
93+
94+
// We don't use dependency injection here to avoid a constructor signature
95+
// change.
96+
$this->populateContextDefaults = \Drupal::config('graphql.settings')->get('dataproducer_populate_default_values', TRUE);
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function createInstance($plugin_id, array $configuration = []) {
103+
$configuration['dataproducer_populate_default_values'] = $this->populateContextDefaults;
104+
return parent::createInstance($plugin_id, $configuration);
86105
}
87106

88107
/**

src/Plugin/GraphQL/DataProducer/DataProducerPluginBase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function resolveField(FieldContext $field) {
5050
if (!method_exists($this, 'resolve')) {
5151
throw new \LogicException('Missing data producer resolve method.');
5252
}
53-
54-
$context = $this->getContextValuesWithDefaults();
53+
$populateDefaulktValues = $this->configuration['dataproducer_populate_default_values'] ?? TRUE;
54+
$context = $populateDefaulktValues ? $this->getContextValuesWithDefaults() : $this->getContextValues();
5555
return call_user_func_array(
5656
[$this, 'resolve'],
5757
array_values(array_merge($context, [$field]))

0 commit comments

Comments
 (0)