Skip to content

Commit

Permalink
refactor: use directive implementations instead of plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Nov 23, 2023
1 parent 26769dc commit 0afb9bb
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 119 deletions.
1 change: 1 addition & 0 deletions apps/cms/config/sync/graphql.graphql_servers.main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ query_complexity: null
schema_configuration:
directable:
schema_definition: ../node_modules/@custom/schema/build/schema.graphql
autoload_registry: ../autoload.json
extensions:
silverback_campaign_urls: silverback_campaign_urls
silverback_gatsby: silverback_gatsby
Expand Down
9 changes: 8 additions & 1 deletion packages/drupal/custom/custom.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ services:
custom.route_subscriber:
class: Drupal\custom\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
- { name: event_subscriber }
custom.content_hub:
class: Drupal\custom\ContentHub
arguments: ['@entity_type.manager']

custom.webform:
class: Drupal\custom\Webform
arguments: ['@renderer', '@entity_type.manager']
11 changes: 9 additions & 2 deletions packages/drupal/custom/directives.graphql
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
directive @contentHub on FIELD_DEFINITION
directive @webformIdToUrl on FIELD_DEFINITION
"""
implementation(drupal): custom.content_hub::query
"""
directive @contentHub(pagination: String, query: String) on FIELD_DEFINITION

"""
implementation(drupal): custom.webform::url
"""
directive @webformIdToUrl(id: String!) on FIELD_DEFINITION
70 changes: 70 additions & 0 deletions packages/drupal/custom/src/ContentHub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drupal\custom;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\graphql_directives\DirectiveArguments;

/**
* Service to query content listings.
*/
class ContentHub {

/**
* The entity type manager, to query and load pages.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;

/**
* ContentHub constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager, to query and load pages.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}

/**
* Query a list of pages.
*
* @param \Drupal\graphql_directives\DirectiveArguments $args
* The graphql argument bag.
*
* @return array{'total': int, 'items': \Drupal\node\NodeInterface[]}
* Result of the query.
*/
public function query(DirectiveArguments $args) : array {
// @todo Switch this to views.
$offset = $args->args['pagination']['offset'];
$limit = $args->args['pagination']['limit'];
$nodeStorage = $this->entityTypeManager->getStorage('node');

$countQuery = $nodeStorage->getQuery();
$countQuery->condition('type', 'page');
$countQuery->condition('status', 1);
if (!empty($args->args['query'])) {
$countQuery->condition('title', $args->args['query'], 'CONTAINS');
}
$count = $countQuery->count()->accessCheck(TRUE)->execute();

$query = $nodeStorage->getQuery();
$query->condition('type', 'page');
$query->condition('status', 1);
if (!empty($args->args['query'])) {
$query->condition('title', $args->args['query'], 'CONTAINS');
}
$pageIds = $query->range($offset, $limit)
->sort('title', 'ASC')
->accessCheck(TRUE)
->execute();
$posts = $pageIds ? $nodeStorage->loadMultiple($pageIds) : [];
return [
'total' => $count,
'items' => $posts,
];
}

}
53 changes: 0 additions & 53 deletions packages/drupal/custom/src/Plugin/GraphQL/Directive/ContentHub.php

This file was deleted.

This file was deleted.

71 changes: 71 additions & 0 deletions packages/drupal/custom/src/Webform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Drupal\custom;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\graphql_directives\DirectiveArguments;

/**
* Helper service for managing webforms with graphql.
*/
class Webform {

/**
* The renderer for collecting cache information.
*/
protected RendererInterface $renderer;

/**
* The entity type manager, to query and load pages.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;

/**
* Webform constructor.
*/
public function __construct(
RendererInterface $renderer,
EntityTypeManagerInterface $entityTypeManager
) {
$this->entityTypeManager = $entityTypeManager;
$this->renderer = $renderer;
}

/**
* Generate the public url of a webform, based on its ID.
*/
public function url(DirectiveArguments $args) : ?string {
$webformId = $args->args['id'];
if (!$webformId) {
return NULL;
}

$webFormStorage = $this->entityTypeManager->getStorage('webform');

/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderContext = new RenderContext();

$result = $this->renderer->executeInRenderContext(
$renderContext,
function () use ($args, $webformId, $webFormStorage) {
$webform = $webFormStorage->load($webformId);
if (!$webform || !$webform->isOpen()) {
return NULL;
}
$args->context->addCacheableDependency($webform);
return $webform->toUrl()->setAbsolute()->toString();
},
);

if (!$renderContext->isEmpty()) {
$args->context->addCacheableDependency($renderContext->pop());
}

return $result;
}

}
4 changes: 2 additions & 2 deletions packages/schema/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type Hero {
union PageContent @resolveEditorBlockType = BlockMarkup | BlockMedia | BlockForm

type BlockForm @type(id: "custom/form") {
url: Url @resolveEditorBlockAttribute(key: "formId") @webformIdToUrl
url: Url @resolveEditorBlockAttribute(key: "formId") @webformIdToUrl(id: "$")
}

type BlockMarkup @type(id: "core/paragraph") {
Expand Down Expand Up @@ -130,7 +130,7 @@ type Query {
previewPage(id: ID!, rid: ID, locale: String!): Page
@fetchEntity(type: "node", id: "$id", rid: "$rid", language: "$locale")
contentHub(query: String, pagination: PaginationInput!): ContentHubResult!
@contentHub
@contentHub(query: "$query", pagination: "$pagination")
}

type MetaTag {
Expand Down

0 comments on commit 0afb9bb

Please sign in to comment.