Skip to content

Commit

Permalink
implement configuration flag
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi committed Jun 15, 2024
1 parent d4a35c6 commit 0b4d348
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/install/graphql.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dataproducer_populate_default_values: true
10 changes: 10 additions & 0 deletions config/schema/graphql.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ graphql.default_persisted_query_configuration:

plugin.plugin_configuration.persisted_query.*:
type: graphql.default_persisted_query_configuration

graphql.settings:
type: config_object
label: "GraphQL Settings"
mapping:
# @todo Remove in GraphQL 5.
dataproducer_populate_default_values:
type: boolean
label: "Populate dataproducer context default values"
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."
12 changes: 12 additions & 0 deletions graphql.install
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ function graphql_update_8001(): void {
*/
function graphql_update_8400() :void {
}

/**
* Preserve dataproducer default value behavior for old installations.
*
* Set dataproducer_populate_default_values to TRUE after you verified that your
* dataproducers are still working with the new default value behavior.
*/
function graphql_update_10400() :void {
\Drupal::configFactory()->getEditable('graphql.settings')
->set('dataproducer_populate_default_values', FALSE)
->save();
}
19 changes: 19 additions & 0 deletions src/Plugin/DataProducerPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class DataProducerPluginManager extends DefaultPluginManager {
*/
protected $resultCacheBackend;

/**
* Backwards compatibility flag to populate context defaults or not.
*
* @todo Remove in 5.x.
*/
protected bool $populateContextDefaults = TRUE;

/**
* DataProducerPluginManager constructor.
*
Expand Down Expand Up @@ -83,6 +90,18 @@ public function __construct(
$this->requestStack = $requestStack;
$this->contextsManager = $contextsManager;
$this->resultCacheBackend = $resultCacheBackend;

// We don't use dependency injection here to avoid a constructor signature
// change.
$this->populateContextDefaults = \Drupal::config('graphql.settings')->get('dataproducer_populate_default_values', TRUE);

Check failure on line 96 in src/Plugin/DataProducerPluginManager.php

View workflow job for this annotation

GitHub Actions / Drupal 10.2.x (PHP 8.3)

Method Drupal\Core\Config\Config::get() invoked with 2 parameters, 0-1 required.

Check failure on line 96 in src/Plugin/DataProducerPluginManager.php

View workflow job for this annotation

GitHub Actions / Drupal 10.2.x (PHP 8.3)

\Drupal calls should be avoided in classes, use dependency injection instead
}

/**
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = []) {
$configuration['dataproducer_populate_default_values'] = $this->populateContextDefaults;
return parent::createInstance($plugin_id, $configuration);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/GraphQL/DataProducer/DataProducerPluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function resolveField(FieldContext $field) {
if (!method_exists($this, 'resolve')) {
throw new \LogicException('Missing data producer resolve method.');
}

$context = $this->getContextValuesWithDefaults();
$populateDefaulktValues = $this->configuration['dataproducer_populate_default_values'] ?? TRUE;
$context = $populateDefaulktValues ? $this->getContextValuesWithDefaults() : $this->getContextValues();
return call_user_func_array(
[$this, 'resolve'],
array_values(array_merge($context, [$field]))
Expand Down

0 comments on commit 0b4d348

Please sign in to comment.