Skip to content

Commit 241f15c

Browse files
committed
Fixes #363: Augment template variables instead of replacing them.
When adding a GraphQL query, a new template variable `graphql` containing the result will be injected.
1 parent 374b195 commit 241f15c

5 files changed

Lines changed: 4 additions & 18 deletions

File tree

modules/graphql_twig/graphql_twig.module

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function graphql_twig_process_query(&$variables, $hook, $info) {
104104

105105
$queryResult = $processor->processQuery($query, $arguments);
106106

107-
$variables['graphql_result'] = $queryResult->getData();
107+
$variables['graphql'] = $queryResult->getData();
108108

109109
$variables['#cache']['contexts'] = $queryResult->getCacheContexts();
110110
$variables['#cache']['tags'] = $queryResult->getCacheTags();

modules/graphql_twig/src/GraphQLTemplateTrait.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@
77
*/
88
trait GraphQLTemplateTrait {
99

10-
/**
11-
* {@inheritdoc}
12-
*
13-
* Override of the original template render. If the context contains as
14-
* `graphql_result` key, this will be passed into the template instead of
15-
* the whole context.
16-
*/
17-
public function render(array $variables) {
18-
if (array_key_exists('graphql_result', $variables)) {
19-
return parent::render($variables['graphql_result']);
20-
}
21-
return parent::render($variables);
22-
}
23-
2410
/**
2511
* Recursively build the GraphQL query.
2612
*

modules/graphql_twig/tests/themes/graphql_twig_test_theme/templates/graphql-echo.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ query($input: String!) {
33
echo(input: $input)
44
}
55
#}
6-
<strong>{{ data.echo }}</strong>
6+
<strong>{{ graphql.data.echo }}</strong>

modules/graphql_twig/tests/themes/graphql_twig_test_theme/templates/graphql-garage.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ query {
77
}
88
#}
99
Garage:
10-
{% for vehicle in data.garage %}
10+
{% for vehicle in graphql.data.garage %}
1111
{% include '@graphql_twig_test_theme/graphql-vehicle.html.twig' with vehicle %}
1212
{% endfor %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>{{ data.node.title }}</h1>
1+
<h1>{{ graphql.data.node.title }}</h1>

0 commit comments

Comments
 (0)