Skip to content

Commit 4426b96

Browse files
authored
Merge 61bba98 into e09ad15
2 parents e09ad15 + 61bba98 commit 4426b96

File tree

6 files changed

+84
-2
lines changed

6 files changed

+84
-2
lines changed

docs/events/index.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,35 @@ For example:
160160
*Note:*
161161
- This event is dispatch by this bundle default error handler, that the reason why, disabling
162162
error handler will also disable this event.
163+
164+
Type loaded
165+
----------------
166+
167+
*Event:* `graphql.type_loaded`
168+
169+
This event can be listen to modified type before schema registration.
170+
171+
For example:
172+
173+
```yaml
174+
App\EventListener\TypeDecorator:
175+
tags:
176+
- { name: kernel.event_listener, event: graphql.type_loaded, method: onTypeLoaded }
177+
```
178+
179+
```php
180+
<?php
181+
182+
namespace App\EventListener;
183+
184+
use Overblog\GraphQLBundle\Event\TypeLoadedEvent;
185+
186+
class TypeDecorator
187+
{
188+
public function onTypeLoaded(TypeLoadedEvent $event)
189+
{
190+
$type = $event->getType();
191+
// here we can modified type
192+
}
193+
}
194+
```

src/Event/Events.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ final class Events
1010
public const PRE_EXECUTOR = 'graphql.pre_executor';
1111
public const POST_EXECUTOR = 'graphql.post_executor';
1212
public const ERROR_FORMATTING = 'graphql.error_formatting';
13+
public const TYPE_LOADED = 'graphql.type_loaded';
1314
}

src/Event/TypeLoadedEvent.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Event;
6+
7+
use GraphQL\Type\Definition\Type;
8+
use Symfony\Component\EventDispatcher\Event;
9+
10+
final class TypeLoadedEvent extends Event
11+
{
12+
/** @var Type */
13+
private $type;
14+
15+
public function __construct(Type $type)
16+
{
17+
$this->type = $type;
18+
}
19+
20+
public function getType(): Type
21+
{
22+
return $this->type;
23+
}
24+
}

src/Resolver/AbstractResolver.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function getSolutionOptions(string $id)
8181
return isset($this->solutionOptions[$id]) ? $this->solutionOptions[$id] : [];
8282
}
8383

84+
protected function onLoadSolution($solution): void
85+
{
86+
}
87+
8488
/**
8589
* @param string $id
8690
*
@@ -97,10 +101,11 @@ private function loadSolution(string $id)
97101
return $this->solutions[$id];
98102
} else {
99103
$loader = $this->solutions[$id];
100-
$this->solutions[$id] = $loader();
104+
$this->solutions[$id] = $solution = $loader();
105+
$this->onLoadSolution($solution);
101106
$this->fullyLoadedSolutions[$id] = true;
102107

103-
return $this->solutions[$id];
108+
return $solution;
104109
}
105110
}
106111

src/Resolver/TypeResolver.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@
55
namespace Overblog\GraphQLBundle\Resolver;
66

77
use GraphQL\Type\Definition\Type;
8+
use Overblog\GraphQLBundle\Event\Events;
9+
use Overblog\GraphQLBundle\Event\TypeLoadedEvent;
10+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
811

912
class TypeResolver extends AbstractResolver
1013
{
1114
private $cache = [];
1215

16+
/** @var EventDispatcherInterface */
17+
private $dispatcher;
18+
19+
public function setDispatcher(EventDispatcherInterface $dispatcher): void
20+
{
21+
$this->dispatcher = $dispatcher;
22+
}
23+
24+
protected function onLoadSolution($solution): void
25+
{
26+
if (null !== $this->dispatcher) {
27+
$this->dispatcher->dispatch(Events::TYPE_LOADED, new TypeLoadedEvent($solution));
28+
}
29+
}
30+
1331
/**
1432
* @param string $alias
1533
*

src/Resources/config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ services:
3838
overblog_graphql.type_resolver:
3939
class: Overblog\GraphQLBundle\Resolver\TypeResolver
4040
public: true
41+
calls:
42+
- ['setDispatcher', ['@event_dispatcher']]
4143
tags:
4244
- { name: overblog_graphql.global_variable, alias: typeResolver }
4345

0 commit comments

Comments
 (0)