Skip to content

Commit 1d31ee0

Browse files
authored
Merge pull request #947 from sforward/feat/register-directives
2 parents 1495a15 + da8b153 commit 1d31ee0

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ CHANGELOG
33

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

79
2022-06-11, 8.3.0
810
-----------------

src/GraphQL.php

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GraphQL\Error\FormattedError;
1111
use GraphQL\Executor\ExecutionResult;
1212
use GraphQL\Server\OperationParams as BaseOperationParams;
13+
use GraphQL\Type\Definition\Directive;
1314
use GraphQL\Type\Definition\ObjectType;
1415
use GraphQL\Type\Definition\Type;
1516
use GraphQL\Type\Schema;
@@ -363,6 +364,7 @@ public function buildSchemaFromConfig(array $schemaConfig): Schema
363364
$schemaMutation = $schemaConfig['mutation'] ?? [];
364365
$schemaSubscription = $schemaConfig['subscription'] ?? [];
365366
$schemaTypes = $schemaConfig['types'] ?? [];
367+
$schemaDirectives = $schemaConfig['directives'] ?? [];
366368

367369
$this->addTypes($schemaTypes);
368370

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

383+
$directives = Directive::getInternalDirectives();
384+
385+
foreach ($schemaDirectives as $class) {
386+
$directive = $this->app->make($class);
387+
$directives[$directive->name] = $directive;
388+
}
389+
381390
return new Schema([
382391
'query' => $query,
383392
'mutation' => $mutation,
384393
'subscription' => $subscription,
394+
'directives' => $directives,
385395
'types' => function () {
386396
$types = [];
387397

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Support\Directives;
5+
6+
use GraphQL\Language\DirectiveLocation;
7+
use GraphQL\Type\Definition\Directive;
8+
use GraphQL\Type\Definition\FieldArgument;
9+
use GraphQL\Type\Definition\Type;
10+
11+
class ExampleDirective extends Directive
12+
{
13+
public function __construct()
14+
{
15+
parent::__construct([
16+
'name' => 'exampleDirective',
17+
'description' => 'This is an example directive',
18+
'locations' => [
19+
// See DirectiveLocation constants for all available locations
20+
DirectiveLocation::QUERY,
21+
],
22+
'args' => [
23+
new FieldArgument([
24+
'name' => 'first',
25+
'description' => 'Description of this argument',
26+
'type' => Type::string(),
27+
]),
28+
],
29+
]);
30+
}
31+
}

tests/Unit/GraphQLTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Rebing\GraphQL\Exception\SchemaNotFound;
1616
use Rebing\GraphQL\Exception\TypeNotFound;
1717
use Rebing\GraphQL\Support\Facades\GraphQL;
18+
use Rebing\GraphQL\Tests\Support\Directives\ExampleDirective;
1819
use Rebing\GraphQL\Tests\Support\Objects\CustomExampleType;
1920
use Rebing\GraphQL\Tests\Support\Objects\ExamplesQuery;
2021
use Rebing\GraphQL\Tests\Support\Objects\ExampleType;
@@ -454,6 +455,25 @@ public function testAddSchemaObjectAndExecuteQueryWithRootValue(): void
454455
self::assertSame($expectedResult, $result);
455456
}
456457

458+
public function testBuildSchemaWithDirectives(): void
459+
{
460+
$schema = GraphQL::buildSchemaFromConfig([
461+
'query' => [
462+
'examplesCustom' => ExamplesQuery::class,
463+
],
464+
'directives' => [
465+
ExampleDirective::class,
466+
],
467+
]);
468+
469+
self::assertSame([
470+
'include',
471+
'skip',
472+
'deprecated',
473+
'exampleDirective',
474+
], array_keys($schema->getDirectives()));
475+
}
476+
457477
public function testIsMacroable(): void
458478
{
459479
self::assertContains(Macroable::class, class_uses_recursive(GraphQL::getFacadeRoot()));

0 commit comments

Comments
 (0)