Skip to content

Commit 0468c7f

Browse files
committed
Add includes of parent template, even if overriding the query.
1 parent 241f15c commit 0468c7f

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

modules/graphql_twig/src/GraphQLTemplateTrait.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@ public function getGraphQLQuery() {
1919
$includes = [];
2020

2121
if ($this instanceof \Twig_Template) {
22+
/** @var \Twig_Template $parent */
23+
$parent = $this->graphqlParent ? $this->loadTemplate($this->graphqlParent) : NULL;
24+
2225
// If there is no query for this template, try to get one from the
2326
// parent template.
2427
if ($this->graphqlQuery) {
2528
$query = $this->graphqlQuery;
2629
}
27-
elseif ($this->graphqlParent) {
28-
$query = $this->loadTemplate($this->graphqlParent)->getGraphQLQuery();
30+
elseif ($parent) {
31+
$query = $parent->getGraphQLQuery();
2932
}
3033

3134
// Recursively collect all included fragments.
3235
$includes = array_map(function ($template) {
3336
return $this->loadTemplate($template)->getGraphQLQuery();
3437
}, $this->getGraphQLIncludes());
38+
39+
// Always add includes from parent templates.
40+
if ($parent) {
41+
$includes += array_map(function ($template) {
42+
return $this->loadTemplate($template)->getGraphQLQuery();
43+
}, $parent->getGraphQLIncludes());
44+
}
3545
}
3646

3747

modules/graphql_twig/tests/src/Unit/GraphQLTwigExtensionTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function setUp() {
2828
'dynamic_include' => '{% graphql %}a{% endgraphql %}{% include sub_fragment with { foo: "bar" } %}',
2929
'fragment' => '{% graphql %}b{% endgraphql %}{% include "sub_fragment" %}',
3030
'sub_fragment' => '{% graphql %}c{% endgraphql %}',
31+
'extend_include' => '{% graphql %}a{% endgraphql %}{% extends "fragment" %}'
3132
]));
3233
$this->twig->addExtension(new GraphQLTwigExtension());
3334
}
@@ -66,4 +67,8 @@ function testDynamicInclude() {
6667
$this->assertGraphQLQuery('dynamic_include', "a");
6768
}
6869

70+
function testExtendInclude() {
71+
$this->assertGraphQLQuery('extend_include', "a\nc");
72+
}
73+
6974
}

0 commit comments

Comments
 (0)