You are here

class RouteProvider in GraphQL 8.4

Provides Symfony routing information for each defined GraphQL server.

Hierarchy

Expanded class hierarchy of RouteProvider

1 string reference to 'RouteProvider'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses RouteProvider
graphql.route_provider in ./graphql.services.yml
Drupal\graphql\RouteProvider

File

src/RouteProvider.php, line 12

Namespace

Drupal\graphql
View source
class RouteProvider {

  /**
   * The authentication collector service.
   *
   * @var \Drupal\Core\Authentication\AuthenticationCollectorInterface
   */
  protected $authenticationCollector;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * RouteProvider constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   * @param \Drupal\Core\Authentication\AuthenticationCollectorInterface $authenticationCollector
   *   The authentication collector service.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, AuthenticationCollectorInterface $authenticationCollector) {
    $this->authenticationCollector = $authenticationCollector;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Collects routes for the server endpoints.
   */
  public function routes() : array {
    $storage = $this->entityTypeManager
      ->getStorage('graphql_server');

    /** @var \Drupal\graphql\Entity\ServerInterface[] $servers */
    $servers = $storage
      ->loadMultiple();
    $routes = [];

    // Allow all authentication providers by default.
    $auth = array_keys($this->authenticationCollector
      ->getSortedProviders());
    foreach ($servers as $id => $server) {
      $path = $server
        ->get('endpoint');
      $routes["graphql.query.{$id}"] = (new Route($path))
        ->addDefaults([
        'graphql_server' => $id,
        '_graphql' => TRUE,
        '_controller' => '\\Drupal\\graphql\\Controller\\RequestController::handleRequest',
        '_disable_route_normalizer' => TRUE,
      ])
        ->addRequirements([
        '_graphql_query_access' => 'graphql_server:{graphql_server}',
        '_format' => 'json',
      ])
        ->addOptions([
        '_auth' => $auth,
        'no_cache' => TRUE,
        'default_url_options' => [
          'path_processing' => FALSE,
        ],
        'parameters' => [
          'graphql_server' => [
            'type' => 'entity:graphql_server',
          ],
        ],
      ]);
    }
    return $routes;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteProvider::$authenticationCollector protected property The authentication collector service.
RouteProvider::$entityTypeManager protected property The entity type manager service.
RouteProvider::routes public function Collects routes for the server endpoints.
RouteProvider::__construct public function RouteProvider constructor.