Skip to content

Commit

Permalink
refactor(gatsby-oauth): use directives service
Browse files Browse the repository at this point in the history
  • Loading branch information
colorfield committed Apr 16, 2024
1 parent c4be894 commit 923d68e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
4 changes: 4 additions & 0 deletions packages/drupal/custom/custom.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ services:
custom.webform:
class: Drupal\custom\Webform
arguments: ['@renderer', '@entity_type.manager', '@serializer']

custom.directives:
class: Drupal\custom\Directives
arguments: ['@current_user', '@date.formatter']
42 changes: 42 additions & 0 deletions packages/drupal/custom/src/Directives.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Drupal\custom;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\Entity\User;

/**
* Custom GraphQL directives.
*/
final class Directives {

/**
* Constructs a Directives object.
*/
public function __construct(
private readonly AccountProxyInterface $currentUser,
private readonly DateFormatterInterface $dateFormatter,
) {}

/**
* Wrapper of current user data to be used for the OAuth demo.
*/
public function currentUser(): array|null {
$account = \Drupal::currentUser();
if ($this->currentUser->isAnonymous()) {
return NULL;
}
else {
$user = User::load($account->id());
$memberFor = $this->dateFormatter->formatTimeDiffSince($user->getCreatedTime());
return [
'id' => $account->id(),
'name' => $account->getDisplayName(),
'email' => $account->getEmail(),
'memberFor' => $memberFor,
];
}
}

}

This file was deleted.

2 changes: 1 addition & 1 deletion packages/schema/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const config: CodegenConfig = {
context: ['gatsby'],
},
},
// Directive autoloader for Drupla.
// Directive autoloader for Drupal.
'build/drupal-autoload.json': {
plugins: ['@amazeelabs/codegen-autoloader'],
config: {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ directive @route(
Retrieve the properties of the current user.
Provided by the "silverback_gatsby" module.
Implemented in "Drupal\custom\Plugin\GraphQL\Directive\CurrentUser".
implementation(drupal): custom.directives::currentUser
"""
directive @currentUser repeatable on FIELD_DEFINITION | SCALAR | UNION | ENUM | INTERFACE | OBJECT

Expand Down

0 comments on commit 923d68e

Please sign in to comment.