Commit 85c947b1 authored by Daniel Bosen's avatar Daniel Bosen Committed by Alex Pott
Browse files

Issue #3325982 by daniel.bosen: Add integration with GraphQL module

parent ed9c0484
Loading
Loading
Loading
Loading

composer.json

0 → 100644
+9 −0
Original line number Diff line number Diff line
{
    "name": "drupal/vgwort",
    "description": "Adds VG Wort integration using publisher defined keys.",
    "type": "drupal-module",
    "license": "GPL-2.0-or-later",
    "require-dev": {
        "drupal/graphql": "^4.4"
    }
}
+4 −0
Original line number Diff line number Diff line
type VgWort {
  counterId: String!
  url: String!
}
+0 −0

Empty file added.

+43 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\vgwort\Plugin\GraphQL\DataProducer;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;

/**
 * Resolves vgwort field data.
 *
 * @DataProducer(
 *   id = "vgwort",
 *   name = @Translation("VG Wort"),
 *   description = @Translation("Resolves the vgwort field."),
 *   produces = @ContextDefinition("map",
 *     label = @Translation("VG Wort field values")
 *   ),
 *   consumes = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("The entity.")
 *     )
 *   }
 * )
 */
class VgWort extends DataProducerPluginBase {

  /**
   * Resolve the VG Wort field.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity with a vgwort field.
   *
   * @return array
   *   The VG Wort field data.
   */
  public function resolve(ContentEntityInterface $entity): array {
    return [
      'counterId' => $entity->vgwort_counter_id->value,
      'url' => $entity->vgwort_counter_id->url,
    ];
  }

}
+41 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\vgwort\Plugin\GraphQL\SchemaExtension;

use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;

/**
 * The menu schema extension.
 *
 * @SchemaExtension(
 *   id = "vgwort",
 *   name = "VG Wort Extension",
 *   description = "VG Wort mappings.",
 *   schema = "composable"
 * )
 */
class VgWortSchemaExtension extends SdlSchemaExtensionPluginBase {

  /**
   * {@inheritdoc}
   */
  public function registerResolvers(ResolverRegistryInterface $registry): void {
    $builder = new ResolverBuilder();

    $fields = [
      'counterId',
      'url',
    ];

    foreach ($fields as $field) {
      $registry->addFieldResolver('VgWort', $field,
        $builder->callback(function ($arr) use ($field) {
          return $arr[$field];
        })
      );
    }
  }

}
Loading