Skip to content

generate a SchemaCompiledEvent when Type files are generated #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 21, 2020

Conversation

mathroc
Copy link
Contributor

@mathroc mathroc commented Aug 15, 2020

fixes #718

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Documented? no
Fixed tickets #718
License MIT

usage exemple:

<?php declare(strict_types=1);

namespace App\Infra\GraphQL\CacheWarmer;

use GraphQL\Utils\SchemaPrinter;
use Overblog\GraphQLBundle\Event\SchemaCompiledEvent;
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor;
use Overblog\GraphQLBundle\Request\ParserInterface;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class GraphQLSchemaDumperSubscriber implements EventSubscriberInterface
{
    private RequestExecutor $requestExecutor;

    private string $projectDir;

    private bool $schemaWasRecompiled = false;

    public function __construct(RequestExecutor $requestExecutor, string $projectDir)
    {
        $this->requestExecutor = $requestExecutor;
        $this->projectDir = $projectDir;
    }

    public function onSchemaCompiled(): void
    {
        $this->schemaWasRecompiled = true;
    }

    public function dumpSchema(): void
    {
        if (!$this->schemaWasRecompiled) {
            return;
        }

        file_put_contents(
            "{$this->projectDir}/schema.graphql",
            SchemaPrinter::doPrint($this->requestExecutor->getSchema()),
        ) or die("failed to save {$this->projectDir}/schema.graphql");

        $result = $this->requestExecutor
            ->execute(null, [
                ParserInterface::PARAM_QUERY => <<<GQL
                    query {
                        __schema {
                            types {
                                kind
                                name
                                possibleTypes {
                                    name
                                }
                            }
                        }
                    }
                GQL,
                ParserInterface::PARAM_VARIABLES => [],
            ])
            ->toArray();

        file_put_contents(
            "{$this->projectDir}/schema-fragments.json",
            \json_encode($result, \JSON_PRETTY_PRINT),
        ) or die("failed to save {$this->projectDir}/schema-fragments.json");
    }

    public static function getSubscribedEvents()
    {
        return [
            SchemaCompiledEvent::class => "onSchemaCompiled",
            RequestEvent::class => "dumpSchema",
            ConsoleCommandEvent::class => "dumpSchema",
        ];
    }
}

@mathroc mathroc force-pushed the feature/schema-compiled-event branch 2 times, most recently from b2a0bcc to 4051d2e Compare August 15, 2020 13:16
Copy link
Collaborator

@Vincz Vincz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but I think it could be interesting to do a little more.
Maybe we could also add a preCompile event with the ability to modify the $configs before the final compilation.
And we could also add a few parameters to the SchemaCompiledEvent.
@murtukov what do you think about it ?
Also, a little piece of doc would be great (maybe with your use-case a an example).

@mcg-web
Copy link
Contributor

mcg-web commented Aug 15, 2020

PreCompile will lead to bad behaviors since we also use configs in CompilePass, that is not something we want. This will make maintainers work much more difficult. Generator type is just a helper if you want to do something that it doesn't support, you can workaround with custom GraphQL types classes.

Copy link
Contributor

@mcg-web mcg-web left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mathroc this look good to me but can you provide some documentation and a simple test that verify that the event is well trigger at the right moment, no need of creating a listener, this could be done with simple mock I think.

@mathroc mathroc force-pushed the feature/schema-compiled-event branch 2 times, most recently from 298ef96 to 954cc0c Compare August 15, 2020 19:41
@mathroc
Copy link
Contributor Author

mathroc commented Aug 15, 2020

I’ve added a test and some documentation. I noticed something when writing the test, should I only dispatch the event in write mode ? if so, where should I tell the TypeGenerator to write the files in the tests ?

@murtukov
Copy link
Contributor

@mathroc I already did a huge refactoring of the TypeGenerator class (a further refactoring still needed tho), which changed the constructor signature. I added info about it in the UPGRADE-1.0.md. Your PR changes the signature again, could you please also update the UPGRADE-1.0.md?

@mathroc mathroc force-pushed the feature/schema-compiled-event branch from 954cc0c to 85a8329 Compare August 16, 2020 20:52
@mathroc
Copy link
Contributor Author

mathroc commented Aug 16, 2020

@murtukov it’s done 👍

It also reminds me that I had to comment out calls to TypeGenerator::addUseStatement, TypeGenerator::addImplement & TypeGenerator::setExpressionLanguage in order to try my changes on master. Should I open an issue to add that to the UPGRADE document?

@Vincz Vincz requested a review from mcg-web August 19, 2020 14:00
@Vincz Vincz merged commit 4534ab7 into overblog:master Aug 21, 2020
@mathroc mathroc deleted the feature/schema-compiled-event branch August 21, 2020 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dispatch a SchemaGenerated event
4 participants