Skip to content

Commit

Permalink
Added Name field support for graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Dec 14, 2023
1 parent 931cab1 commit 17087d6
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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}
*
* @codeCoverageIgnore Unclear how to test for this.
*
* @GraphQLComposeFieldType(
* id = "name",
* type_sdl = "NameType",
* )
*/
class NameItem extends GraphQLComposeFieldTypeBase implements FieldProducerItemInterface {

use FieldProducerTrait;

/**
* {@inheritdoc}
*/
public function resolveFieldItem(FieldItemInterface $item, FieldContext $context) {
return [
'title' => $item->get('title')->getValue(),
'given' => $item->get('given')->getValue(),
'middle' => $item->get('middle')->getValue(),
'family' => $item->get('family')->getValue(),
'generational' => $item->get('generational')->getValue(),
'credentials' => $item->get('credentials')->getValue(),
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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}
*
* @codeCoverageIgnore Unclear how to test for this.
*
* @GraphQLComposeSchemaType(
* id = "NameType"
* )
*/
class NameType extends GraphQLComposeSchemaTypeBase {

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

if (!$this->moduleHandler->moduleExists('name')) {
return $types;
}

$types[] = new ObjectType([
'name' => $this->getPluginId(),
'description' => (string) $this->t('Smart Date data.'),
'fields' => fn() => [
'title' => [
'type' => Type::string(),
'description' => 'Title',
],
'given' => [
'type' => Type::string(),
'description' => 'Given',
],
'middle' => [
'type' => Type::string(),
'description' => 'Middle name(s)',
],
'family' => [
'type' => Type::string(),
'description' => 'Family',
],
'generational' => [
'type' => Type::string(),
'description' => 'Generational',
],
'credentials' => [
'type' => Type::string(),
'description' => 'Credentials',
],
],
]);

return $types;
}

}
18 changes: 16 additions & 2 deletions modules/stanford_graphql/stanford_graphql.module
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeFieldTypeInterfac
*/
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()]];
if ($field_definition->getName() == 'layout_selection') {
foreach ($results as &$result) {
$result = [
'id' => $results->id(),
'label' => $results->label(),
];
}
}

if ($field_definition->getType() == 'viewfield') {
/** @var \Drupal\views\ViewExecutable $result */
foreach ($results as $result) {
// Change the view display to use the graphql that should match the chosen
// view.
$result->setDisplay($result->current_display . '_graphql');
}
}
}

0 comments on commit 17087d6

Please sign in to comment.