@@ -186,3 +186,91 @@ class TypeDecorator
186186 }
187187}
188188` ` `
189+
190+ Schema Compiled
191+ ----------------
192+
193+ *Event:* `Overblog\GraphQLBundle\Event\SchemaCompiledEvent`
194+
195+ Used to be notified when the schema has been newly compiled.
196+
197+ Example :
198+
199+ ` ` ` php
200+ <?php declare(strict_types=1);
201+
202+ namespace App\I nfra\G raphQL\C acheWarmer;
203+
204+ use GraphQL\Utils\Sch emaPrinter;
205+ use Overblog\G raphQLBundle\E vent\S chemaCompiledEvent;
206+ use Overblog\G raphQLBundle\R equest\E xecutor as RequestExecutor;
207+ use Overblog\G raphQLBundle\R equest\P arserInterface;
208+ use Symfony\C omponent\C onsole\E vent\C onsoleCommandEvent;
209+ use Symfony\C omponent\E ventDispatcher\E ventSubscriberInterface;
210+ use Symfony\C omponent\H ttpKernel\E vent\R equestEvent;
211+
212+ class GraphQLSchemaDumperSubscriber implements EventSubscriberInterface
213+ {
214+ private RequestExecutor $requestExecutor;
215+
216+ private string $projectDir;
217+
218+ private bool $schemaWasRecompiled = false;
219+
220+ public function __construct(RequestExecutor $requestExecutor, string $projectDir)
221+ {
222+ $this->requestExecutor = $requestExecutor;
223+ $this->projectDir = $projectDir;
224+ }
225+
226+ public function onSchemaCompiled(): void
227+ {
228+ $this->schemaWasRecompiled = true;
229+ }
230+
231+ public function dumpSchema(): void
232+ {
233+ if (!$this->schemaWasRecompiled) {
234+ return;
235+ }
236+
237+ file_put_contents(
238+ "{$this->projectDir}/schema.graphql",
239+ SchemaPrinter::doPrint($this->requestExecutor->getSchema()),
240+ ) or die("failed to save {$this->projectDir}/schema.graphql");
241+
242+ $result = $this->requestExecutor
243+ ->execute(null, [
244+ ParserInterface::PARAM_QUERY => <<<GQL
245+ query {
246+ __schema {
247+ types {
248+ kind
249+ name
250+ possibleTypes {
251+ name
252+ }
253+ }
254+ }
255+ }
256+ GQL,
257+ ParserInterface::PARAM_VARIABLES => [],
258+ ])
259+ ->toArray();
260+
261+ file_put_contents(
262+ "{$this->projectDir}/schema-fragments.json",
263+ \j son_encode($result, \J SON_PRETTY_PRINT),
264+ ) or die("failed to save {$this->projectDir}/schema-fragments.json");
265+ }
266+
267+ public static function getSubscribedEvents()
268+ {
269+ return [
270+ SchemaCompiledEvent::class => "onSchemaCompiled",
271+ RequestEvent::class => "dumpSchema",
272+ ConsoleCommandEvent::class => "dumpSchema",
273+ ];
274+ }
275+ }
276+ ` ` `
0 commit comments