ExplorerController.php in GraphQL 8.4
File
src/Controller/ExplorerController.php
View source
<?php
namespace Drupal\graphql\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\graphql\Entity\ServerInterface;
use Drupal\graphql\GraphQL\Utility\Introspection;
use Drupal\graphql\Plugin\SchemaPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class ExplorerController implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $urlGenerator;
protected $introspection;
protected $pluginManager;
public static function create(ContainerInterface $container) : self {
return new static($container
->get('url_generator'), $container
->get('graphql.introspection'), $container
->get('plugin.manager.graphql.schema'));
}
public function __construct(UrlGeneratorInterface $urlGenerator, Introspection $introspection, SchemaPluginManager $pluginManager) {
$this->urlGenerator = $urlGenerator;
$this->introspection = $introspection;
$this->pluginManager = $pluginManager;
}
public function viewExplorer(ServerInterface $graphql_server, Request $request) {
$url = $this->urlGenerator
->generate("graphql.query.{$graphql_server->id()}");
$introspectionData = $this->introspection
->introspect($graphql_server);
return [
'#type' => 'markup',
'#markup' => '<div id="graphql-explorer"></div>',
'#attached' => [
'library' => [
'graphql/explorer',
],
'drupalSettings' => [
'graphqlRequestUrl' => $url,
'graphqlIntrospectionData' => $introspectionData,
'graphqlQuery' => $request
->get('query'),
'graphqlVariables' => $request
->get('variables'),
],
],
];
}
}