Skip to content

Commit

Permalink
Provide some plugins for GraphQL Compose module
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Dec 13, 2023
1 parent 35d7645 commit c08f3e0
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_graphql\Plugin\GraphQLCompose\EntityType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeEntityTypeBase;

/**
* {@inheritdoc}
*
* @GraphQLComposeEntityType(
* id = "citation"
* )
*/
class Citation extends GraphQLComposeEntityTypeBase {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_graphql\Plugin\GraphQLCompose\EntityType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeEntityTypeBase;

/**
* {@inheritdoc}
*
* @GraphQLComposeEntityType(
* id = "config_pages"
* )
*/
class ConfigPage extends GraphQLComposeEntityTypeBase {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_graphql\Plugin\GraphQLCompose\EntityType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeEntityTypeBase;

/**
* {@inheritdoc}
*
* @GraphQLComposeEntityType(
* id = "layout",
* type_sdl = "LayoutLibrary"
* )
*/
class LayoutLibrary extends GraphQLComposeEntityTypeBase {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_graphql\Plugin\GraphQLCompose\EntityType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeEntityTypeBase;

/**
* {@inheritdoc}
*
* @GraphQLComposeEntityType(
* id = "su_policy_log"
* )
*/
class PolicyLog extends GraphQLComposeEntityTypeBase {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_graphql\Plugin\GraphQLCompose\FieldType;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql_compose\Plugin\GraphQL\DataProducer\FieldProducerItemInterface;
use Drupal\graphql_compose\Plugin\GraphQL\DataProducer\FieldProducerTrait;
use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeFieldTypeBase;

/**
* {@inheritDoc}
*
* @GraphQLComposeFieldType(
* id = "smartdate",
* type_sdl = "SmartDateType",
* )
*/
class SmartDateItem extends GraphQLComposeFieldTypeBase implements FieldProducerItemInterface{

use FieldProducerTrait;

/**
* {@inheritdoc}
*/
public function resolveFieldItem(FieldItemInterface $item, FieldContext $context) {
return [
'value' => $item->get('value')->getValue(),
'end_value' => $item->get('end_value')->getValue(),
'duration' => $item->get('duration')->getValue(),
'rrule' => $item->get('rrule')->getValue(),
'rrule_index' => $item->get('rrule_index')->getValue(),
'timezone' => $item->get('timezone')->getValue(),
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_graphql\Plugin\GraphQLCompose\SchemaType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeSchemaTypeBase;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

/**
* {@inheritdoc}
*
* @GraphQLComposeSchemaType(
* id = "LayoutLibrary"
* )
*/
class LayoutLibrary extends GraphQLComposeSchemaTypeBase {

/**
* {@inheritdoc}
*/
public function getTypes(): array {
$types = [];

$types[] = new ObjectType([
'name' => $this->getPluginId(),
'description' => (string) $this->t('Layout Library entity.'),
'fields' => fn() => [
'id' => [
'type' => Type::nonNull(Type::id()),
'description' => (string) $this->t('Machine name of the layout definition.'),
],
'label' => [
'type' => Type::nonNull(Type::string()),
'description' => (string) $this->t('Human readable name of the layout definition.'),
],
],
]);

return $types;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_graphql\Plugin\GraphQLCompose\SchemaType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeSchemaTypeBase;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

/**
* {@inheritdoc}
*
* @GraphQLComposeSchemaType(
* id = "SmartDateType"
* )
*/
class SmartDateType extends GraphQLComposeSchemaTypeBase {

/**
* {@inheritdoc}
*/
public function getTypes(): array {
$types = [];

if (!$this->moduleHandler->moduleExists('smart_date')) {
return [];
}

$types[] = new ObjectType([
'name' => $this->getPluginId(),
'description' => (string) $this->t('Smart Date data.'),
'fields' => fn() => [
'value' => [
'type' => Type::nonNull(static::type('Timestamp')),
'description' => 'Start timestamp value',
],
'end_value' => [
'type' => Type::nonNull(static::type('Timestamp')),
'description' => 'End timestamp value',
],
'duration' => [
'type' => Type::int(),
'description' => 'Duration, in minutes',
],
'rrule' => [
'type' => Type::int(),
'description' => 'RRule ID',
],
'rrule_index' => [
'type' => Type::int(),
'description' => 'RRule Index',
],
'timezone' => [
'type' => Type::string(),
'description' => 'Timezone',
],
],
]);

return $types;
}

}
10 changes: 10 additions & 0 deletions modules/stanford_graphql/stanford_graphql.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: 'Stanford GraphQL'
type: module
description: 'GraphQL helper.'
core_version_requirement: ^9 || ^10
package: Stanford
version: 9.4.1
dependencies:
- graphql_compose:graphql_compose_layout_paragraphs
- graphql_compose:graphql_compose_menus
- graphql_compose:graphql_compose_views
19 changes: 19 additions & 0 deletions modules/stanford_graphql/stanford_graphql.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file
* stanford_graphql.module
*/

use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeFieldTypeInterface;

/**
* Implements hook_graphql_compose_field_results_alter().
*/
function stanford_graphql_graphql_compose_field_results_alter(array &$results, $entity, GraphQLComposeFieldTypeInterface $plugin, FieldContext $context) {
$field_definition = $plugin->getFieldDefinition();
if ($field_definition->getName() == 'layout_selection' && isset($results[0])) {
$results = [['id' => $results[0]?->id(), 'label' => $results[0]?->label()]];
}
}

0 comments on commit c08f3e0

Please sign in to comment.