Skip to content

Support class based schemas #706

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 6 commits into from
Mar 12, 2021
Merged

Support class based schemas #706

merged 6 commits into from
Mar 12, 2021

Conversation

jasonvarga
Copy link
Contributor

@jasonvarga jasonvarga commented Jan 20, 2021

Summary

This PR adds support for defining a class based schema.

Rather than defining the entire schema directly in the config file, like this:

'schemas' => [
    'default' => [
        'query' => [
            QueryOne::class,
            QueryTwo::class
        ]
    ]
]

You'll be able to define it by specifying a class name:

'schemas' => [
  'default' => \App\GraphQL\MySchema::class
]

This class should implement the new ConfigConvertible contract which will convert to an array you'd have previously used in the config file.

use Rebing\GraphQL\Support\Contracts\ConfigConvertible;

class MySchema implements ConfigConvertible
{
    public function toConfig(): array
    {
        return [
            'query' => [
                QueryOne::class,
                QueryTwo::class
            ]
        ];
    }
}

The reason this would be useful for us (Statamic, a Laravel CMS package) is because we intend to be able to have an entire schema that users can put into their config. We don't want them to have to manually define the config.

Also, we intend to allow third parties to inject their own query classes into our schema. The schema class above is a bit simplified. Ours might look more like this:

class Schema implements ConfigConvertible
{
    private static $customQueries = [];

    public function toConfig(): array
    {
        return [
            'query' => array_merge([
                NativeQueryOne::class,
                NativeQueryTwo::class
            ], static::$customQueries);
        ];
    }

    public static function addQuery($class)
    {
        static::$customQueries[] = $class;
    }
}

I'm not quite sure how you feel about the naming of things here though. I used ConfigConvertible and toConfig() because I saw a TypeConvertible interface with toType().

The only technically breaking change here is that if you already used a string, you'd previously get a GraphQL error like cannot query field "some_query" and now you'd get a BindingResolutionException. But your app wouldn't have been working in the first place so I don't think it really matters.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist

  • Existing tests have been adapted and/or new tests have been added
  • Add a CHANGELOG.md entry
  • Update the README.md
  • Code style has been fixed via composer fix-style

@jasonvarga
Copy link
Contributor Author

Not sure why the static analysis is failing. I didn't touch routes.php. Looks like it fails on master too.
The code style wants to make whacky migration class names. Also unrelated to anything in this PR.

🤔 🤷

@jasonvarga jasonvarga marked this pull request as ready for review January 21, 2021 00:46
@mfn
Copy link
Collaborator

mfn commented Jan 22, 2021

Hey @jasonvarga , thanks for the PR!

I'll check it out as soon as I find time, might take a while though. I wouldn't worry yet about the analyzer, I'll check this out later too.

@jasonvarga
Copy link
Contributor Author

Thanks!

Copy link
Collaborator

@mfn mfn left a comment

Choose a reason for hiding this comment

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

Very nice, LGTM!

Also nice job with statamic/cms#2982 😱

Will merge & release as soon as everything is green

@mfn mfn merged commit 22c3b26 into rebing:master Mar 12, 2021
@mfn
Copy link
Collaborator

mfn commented Mar 12, 2021

@jasonvarga
Copy link
Contributor Author

Awesome, thanks!

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.

2 participants