-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathExampleSchemaExtension.php
62 lines (53 loc) · 1.9 KB
/
ExampleSchemaExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Drupal\graphql_examples\Plugin\GraphQL\SchemaExtension;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
/**
* @SchemaExtension(
* id = "example_extension",
* name = "Example extension",
* description = "A simple extension that adds node related fields.",
* schema = "example"
* )
*/
class ExampleSchemaExtension extends SdlSchemaExtensionPluginBase {
/**
* {@inheritdoc}
*/
public function registerResolvers(ResolverRegistryInterface $registry): void {
$builder = new ResolverBuilder();
$this->addQueryFields($registry, $builder);
$this->addPageFields($registry, $builder);
}
/**
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
*/
protected function addPageFields(ResolverRegistryInterface $registry, ResolverBuilder $builder): void {
$registry->addFieldResolver('Page', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Page', 'title',
$builder->compose(
$builder->produce('entity_label')
->map('entity', $builder->fromParent()),
$builder->produce('uppercase')
->map('string', $builder->fromParent())
)
);
}
/**
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
*/
protected function addQueryFields(ResolverRegistryInterface $registry, ResolverBuilder $builder): void {
$registry->addFieldResolver('Query', 'page',
$builder->produce('entity_load')
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['page']))
->map('id', $builder->fromArgument('id'))
);
}
}