overblog / GraphQLBundle

This bundle provides tools to build a complete GraphQL API server in your Symfony App.
MIT License
787 stars 221 forks source link

dispatch a SchemaGenerated event #718

Closed mathroc closed 4 years ago

mathroc commented 4 years ago
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Version/Branch 0.13

I’d like to export the schema to schema-fragments.json and schema.graphql files to commit them in the repo so that code review can view a diff of schema.graphql to easily understand how the changes affect the schema (schema-fragments.json is for apollo)

I couldn’t find any event to hook into for that, I tried doing this in a cache warmer, it works but needs is only called when explicitly calling console cache:clear not when the cache is updated on a web request

I have a job in CI to check that the schema.graphql is up to date in case someone forgot to call cache:clear but it’s annoying to have a pipeline fail because we forgot that, so I was hoping to find a way to do this automatically

do you think it would be appropriate to dispatch an event allowing that ?

mcg-web commented 4 years ago

This can be done with the Symfony command dump schema and composer events post update and post install so you don't need to commit them.

mathroc commented 4 years ago

I was probably not very clear I’ll try to explain what I’m trying to do in more details

        $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");

There’s a js project in the same repo for the frontend, it uses both files (schema.graphql for eslint and graphql auto compelete, and schema-fragments.json is for the apollo client IntrospectionFragmentMatcher

I want to generate this both automatically as soon as graphql-bundle update the generated schema code so that the js project can pickup the changes almost in real time when developing.

Vincz commented 4 years ago

I looked a little bit into it. It's not a bad idea and could allow multiple use-cases. It would only require to inject the eventDispatcher into the TypeGenerator and dispatch a CompileEvent. @mathroc What about a little PR ?