Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaking result cache metadata #588

Merged
merged 15 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Plugin/GraphQL/Fields/FieldPluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,26 @@ protected function isLanguageAwareField() {
* {@inheritdoc}
*/
protected function resolveDeferred(callable $callback, $value, array $args, ResolveContext $context, ResolveInfo $info) {
$result = $this->getRenderer()->executeInRenderContext(new RenderContext(), function () use ($callback, $value, $args, $context, $info) {
return $callback($value, $args, $context, $info);
$renderContext = new RenderContext();

$result = $this->getRenderer()->executeInRenderContext($renderContext, function () use ($callback, $value, $args, $context, $info) {
$result = $callback($value, $args, $context, $info);
Copy link
Contributor

@awm086 awm086 Apr 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to add $callback and $renderContext to the use statement.

if ($result instanceof \Generator) {
$result = iterator_to_array($result);
}
return $result;
});

if (!$renderContext->isEmpty() && $info->operation->operation === 'query') {
$context->addCacheableDependency($renderContext->pop());
}

if (is_callable($result)) {
return new Deferred(function () use ($result, $value, $args, $context, $info) {
return $this->resolveDeferred($result, $value, $args, $context, $info);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build fails for syntax error ... I think you're missing ); here.


$result = iterator_to_array($result);

// Only collect cache metadata if this is a query. All other operation types
// are not cacheable anyways.
if ($info->operation->operation === 'query') {
Expand Down
61 changes: 61 additions & 0 deletions tests/src/Kernel/Framework/ResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,65 @@ public function testPersistedQuery() {
$this->persistedQuery('query:b', ['value' => 'test']);
}

/**
* Test behavior in case of leaking cache metadata.
*
* Intentionally emit undeclared cache metadata as side effect of field
* resolvers. Should still be added to the processors result.
*/
public function testLeakingCacheMetadata() {

/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');

$this->mockField('leakA', [
'id' => 'leakA',
'name' => 'leakA',
'type' => 'String',
], function () use ($renderer) {
$el = [
'#plain_text' => 'Leak A',
'#cache' => [
'tags' => ['a'],
],
];
yield $renderer->render($el)->__toString();
});

$this->mockField('leakB', [
'id' => 'leakB',
'name' => 'leakB',
'type' => 'String',
], function () use ($renderer) {
$el = [
'#plain_text' => 'Leak B',
'#cache' => [
'tags' => ['b'],
],
];
$value = $renderer->render($el)->__toString();
return function () use ($value) {
yield $value;
};
});

$query = <<<GQL
query {
leakA
leakB
}
GQL;

$metadata = $this->defaultCacheMetaData()
->addCacheTags(['a', 'b']);

$this->assertResults($query, [], [
'leakA' => 'Leak A',
'leakB' => 'Leak B',
], $metadata);

$result = $this->query($query);
$this->assertEquals(200, $result->getStatusCode());
}

}