diff --git a/src/GraphQLTemplateTrait.php b/src/GraphQLTemplateTrait.php index bd578be..968fe1d 100644 --- a/src/GraphQLTemplateTrait.php +++ b/src/GraphQLTemplateTrait.php @@ -45,8 +45,109 @@ trait GraphQLTemplateTrait { /** * {@inheritdoc} + * Used with Drupal 10.3.0 and later. */ - public function display(array $context, array $blocks = []) { + public function yield(array $context, array $blocks = []): iterable + { + if (!static::hasGraphQLOperations()) { + return parent::yield($context, $blocks); + } + + if (isset($context['graphql_arguments'])) { + $context = $context['graphql_arguments']; + } + + $query = trim($this->getGraphQLQuery()); + + if (!$query) { + return parent::yield($context, $blocks); + } + + $arguments = []; + foreach (static::rawGraphQLArguments() as $var) { + if (isset($context[$var])) { + $arguments[$var] = $context[$var] instanceof EntityInterface ? $context[$var]->id() : $context[$var]; + } + } + + $queryResult = $this->env->getGraphQlServer()->executeOperation(OperationParams::create([ + 'query' => $query, + 'variables' => $arguments, + ])); + + $build = [ + '#cache' => [ + 'contexts' => $queryResult->getCacheContexts(), + 'tags' => $queryResult->getCacheTags(), + 'max-age' => $queryResult->getCacheMaxAge(), + ], + ]; + + $this->env->getRenderer()->render($build); + + $config = \Drupal::config('graphql_twig.settings'); + $debug_placement = $config->get('debug_placement'); + + if ($this->env->isDebug() && \Drupal::currentUser()->hasPermission('use graphql explorer')) { + // Auto-attach the debug assets if necessary. + $template_attached = ['#attached' => ['library' => ['graphql_twig/debug']]]; + $this->env->getRenderer()->render($template_attached); + } + + if ($this->env->isDebug() && $debug_placement == 'wrapped') { + printf( + '