Skip to content
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

Register directives via schema config #947

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CHANGELOG

[Next release](https://github.com/rebing/graphql-laravel/compare/8.3.0...master)
--------------
### Added
- Register directives via schema config [\#947 / sforward](https://github.com/rebing/graphql-laravel/pull/947)

2022-06-11, 8.3.0
-----------------
Expand Down
10 changes: 10 additions & 0 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GraphQL\Error\FormattedError;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Server\OperationParams as BaseOperationParams;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
Expand Down Expand Up @@ -363,6 +364,7 @@ public function buildSchemaFromConfig(array $schemaConfig): Schema
$schemaMutation = $schemaConfig['mutation'] ?? [];
$schemaSubscription = $schemaConfig['subscription'] ?? [];
$schemaTypes = $schemaConfig['types'] ?? [];
$schemaDirectives = $schemaConfig['directives'] ?? [];

$this->addTypes($schemaTypes);

Expand All @@ -378,10 +380,18 @@ public function buildSchemaFromConfig(array $schemaConfig): Schema
? $this->objectType($schemaSubscription, ['name' => 'Subscription'])
: null;

$directives = Directive::getInternalDirectives();

foreach ($schemaDirectives as $class) {
$directive = $this->app->make($class);
$directives[$directive->name] = $directive;
}

return new Schema([
'query' => $query,
'mutation' => $mutation,
'subscription' => $subscription,
'directives' => $directives,
'types' => function () {
$types = [];

Expand Down
31 changes: 31 additions & 0 deletions tests/Support/Directives/ExampleDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types = 1);
namespace Rebing\GraphQL\Tests\Support\Directives;

use GraphQL\Language\DirectiveLocation;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\Type;

class ExampleDirective extends Directive
{
public function __construct()
{
parent::__construct([
'name' => 'exampleDirective',
'description' => 'This is an example directive',
'locations' => [
// See DirectiveLocation constants for all available locations
DirectiveLocation::QUERY,
],
'args' => [
new FieldArgument([
'name' => 'first',
'description' => 'Description of this argument',
'type' => Type::string(),
]),
],
]);
}
}
20 changes: 20 additions & 0 deletions tests/Unit/GraphQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rebing\GraphQL\Exception\SchemaNotFound;
use Rebing\GraphQL\Exception\TypeNotFound;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Tests\Support\Directives\ExampleDirective;
use Rebing\GraphQL\Tests\Support\Objects\CustomExampleType;
use Rebing\GraphQL\Tests\Support\Objects\ExamplesQuery;
use Rebing\GraphQL\Tests\Support\Objects\ExampleType;
Expand Down Expand Up @@ -454,6 +455,25 @@ public function testAddSchemaObjectAndExecuteQueryWithRootValue(): void
self::assertSame($expectedResult, $result);
}

public function testBuildSchemaWithDirectives(): void
{
$schema = GraphQL::buildSchemaFromConfig([
'query' => [
'examplesCustom' => ExamplesQuery::class,
],
'directives' => [
ExampleDirective::class,
],
]);

self::assertSame([
'include',
'skip',
'deprecated',
'exampleDirective',
], array_keys($schema->getDirectives()));
}

public function testIsMacroable(): void
{
self::assertContains(Macroable::class, class_uses_recursive(GraphQL::getFacadeRoot()));
Expand Down