Skip to content

Commit

Permalink
fix(dataproducer): Populate context default values before resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi committed May 30, 2024
1 parent 46a63fd commit fdbf86a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Plugin/GraphQL/DataProducer/DataProducerPluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,26 @@ public function resolveField(FieldContext $field) {
throw new \LogicException('Missing data producer resolve method.');
}

$context = $this->getContextValues();
$context = $this->getContextValuesWithDefaults();
return call_user_func_array(
[$this, 'resolve'],
array_values(array_merge($context, [$field]))
);
}

/**
* Initializes all contexts and populates default values.
*
* We cannot use ::getContextValues() here because it does not work with
* default_value.
*/
public function getContextValuesWithDefaults() {
$values = [];
foreach ($this->getContextDefinitions() as $name => $definition) {
$values[$name] = $this->getContext($name)->getContextValue();
}

return $values;
}

}
28 changes: 28 additions & 0 deletions tests/src/Kernel/DataProducer/DefaultValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Drupal\Tests\graphql\Kernel\DataProducer;

use Drupal\Tests\graphql\Kernel\GraphQLTestBase;

/**
* Context default value test.
*
* @group graphql
*/
class DefaultValueTest extends GraphQLTestBase {


/**
* Test that the entity_load data producer has the correct default values.
*/
public function testEntityLoadDefaultValue(): void {
$manager = $this->container->get('plugin.manager.graphql.data_producer');
$plugin = $manager->createInstance('entity_load');
// Only type is required.
$plugin->setContextValue('type', 'node');
$context_values = $plugin->getContextValuesWithDefaults();
$this->assertSame(TRUE, $context_values['access']);
$this->assertSame('view', $context_values['access_operation']);
}

}

0 comments on commit fdbf86a

Please sign in to comment.