Skip to content

Commit 8514a44

Browse files
authored
Converting schemas to plugins. (#365)
1 parent e0e7e5c commit 8514a44

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Drupal\graphql_json\Plugin\Deriver;
4+
5+
use Drupal\Component\Plugin\Derivative\DeriverBase;
6+
use Drupal\Core\Extension\ModuleHandlerInterface;
7+
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8+
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
10+
11+
/**
12+
* Only add the field if the serializer module is enabled.
13+
*/
14+
class EntityToJsonDeriver extends DeriverBase implements ContainerDeriverInterface {
15+
16+
/**
17+
* The module handler service.
18+
*
19+
* @var \Drupal\Core\Extension\ModuleHandlerInterface
20+
*/
21+
protected $moduleHandler;
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public static function create(ContainerInterface $container, $basePluginId) {
27+
return new static(
28+
$container->get('module_handler')
29+
);
30+
}
31+
32+
/**
33+
* Creates a EntityToJsonDeriver object.
34+
*
35+
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
36+
* The module handler service.
37+
*/
38+
public function __construct(ModuleHandlerInterface $moduleHandler) {
39+
$this->moduleHandler = $moduleHandler;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function getDerivativeDefinitions($basePluginDefinition) {
46+
if ($this->moduleHandler->moduleExists('serialization')) {
47+
$this->derivatives['entity_to_json'] = $basePluginDefinition;
48+
}
49+
50+
return parent::getDerivativeDefinitions($basePluginDefinition);
51+
}
52+
53+
}

src/Plugin/GraphQL/Fields/EntityToJson.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
66
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7-
use Drupal\graphql\Annotation\GraphQLField;
87
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
98
use Symfony\Component\DependencyInjection\ContainerInterface;
109
use Symfony\Component\Serializer\SerializerInterface;
@@ -17,7 +16,8 @@
1716
* id = "entity_to_json",
1817
* name = "toJson",
1918
* type = "JsonObject",
20-
* types = {"Entity"}
19+
* types = {"Entity"},
20+
* deriver = "\Drupal\graphql_json\Plugin\Deriver\EntityToJsonDeriver"
2121
* )
2222
*/
2323
class EntityToJson extends FieldPluginBase implements ContainerFactoryPluginInterface {
@@ -39,14 +39,12 @@ public static function create(
3939
$plugin_id,
4040
$plugin_definition
4141
) {
42-
if ($container->get('module_handler')->moduleExists('serialization')) {
43-
return new static(
44-
$configuration,
45-
$plugin_id,
46-
$plugin_definition,
47-
$container->get('serializer')
48-
);
49-
}
42+
return new static(
43+
$configuration,
44+
$plugin_id,
45+
$plugin_definition,
46+
$container->get('serializer')
47+
);
5048
}
5149

5250
/**
@@ -70,5 +68,4 @@ protected function resolveValues($value, array $args, ResolveInfo $info) {
7068
yield json_decode($this->serializer->serialize($value, 'json'), TRUE);
7169
}
7270

73-
7471
}

src/Plugin/GraphQL/Fields/JsonPathToEntity.php

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function __construct(
6666
parent::__construct($configuration, $pluginId, $pluginDefinition);
6767
}
6868

69-
7069
/**
7170
* {@inheritdoc}
7271
*/

src/Plugin/GraphQL/Fields/JsonResponseContent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Drupal\graphql_json\Plugin\Graphql\Fields;
3+
namespace Drupal\graphql_json\Plugin\GraphQL\Fields;
44

55
use Drupal\graphql_core\Plugin\GraphQL\Fields\ResponseContent;
66
use Youshido\GraphQL\Execution\ResolveInfo;

0 commit comments

Comments
 (0)