From 0b4d348388f414428e5c1df860c4ce41ae93269a Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sat, 15 Jun 2024 10:04:44 +0200 Subject: [PATCH] implement configuration flag --- config/install/graphql.settings.yml | 1 + config/schema/graphql.schema.yml | 10 ++++++++++ graphql.install | 12 ++++++++++++ src/Plugin/DataProducerPluginManager.php | 19 +++++++++++++++++++ .../DataProducer/DataProducerPluginBase.php | 4 ++-- 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 config/install/graphql.settings.yml diff --git a/config/install/graphql.settings.yml b/config/install/graphql.settings.yml new file mode 100644 index 000000000..abdb9497a --- /dev/null +++ b/config/install/graphql.settings.yml @@ -0,0 +1 @@ +dataproducer_populate_default_values: true diff --git a/config/schema/graphql.schema.yml b/config/schema/graphql.schema.yml index f90dc52f0..cec9ca533 100644 --- a/config/schema/graphql.schema.yml +++ b/config/schema/graphql.schema.yml @@ -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." diff --git a/graphql.install b/graphql.install index dcc9c47bc..bf79cf1cd 100644 --- a/graphql.install +++ b/graphql.install @@ -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(); +} diff --git a/src/Plugin/DataProducerPluginManager.php b/src/Plugin/DataProducerPluginManager.php index 549749f0e..407118ba5 100644 --- a/src/Plugin/DataProducerPluginManager.php +++ b/src/Plugin/DataProducerPluginManager.php @@ -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. * @@ -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); + } + + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = []) { + $configuration['dataproducer_populate_default_values'] = $this->populateContextDefaults; + return parent::createInstance($plugin_id, $configuration); } /** diff --git a/src/Plugin/GraphQL/DataProducer/DataProducerPluginBase.php b/src/Plugin/GraphQL/DataProducer/DataProducerPluginBase.php index 13209ede9..b21348cbc 100644 --- a/src/Plugin/GraphQL/DataProducer/DataProducerPluginBase.php +++ b/src/Plugin/GraphQL/DataProducer/DataProducerPluginBase.php @@ -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]))