File tree 4 files changed +63
-0
lines changed
4 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ CHANGELOG
3
3
4
4
[ Next release] ( https://github.com/rebing/graphql-laravel/compare/8.3.0...master )
5
5
--------------
6
+ ### Added
7
+ - Register directives via schema config [ \# 947 / sforward] ( https://github.com/rebing/graphql-laravel/pull/947 )
6
8
7
9
2022-06-11, 8.3.0
8
10
-----------------
Original file line number Diff line number Diff line change 10
10
use GraphQL \Error \FormattedError ;
11
11
use GraphQL \Executor \ExecutionResult ;
12
12
use GraphQL \Server \OperationParams as BaseOperationParams ;
13
+ use GraphQL \Type \Definition \Directive ;
13
14
use GraphQL \Type \Definition \ObjectType ;
14
15
use GraphQL \Type \Definition \Type ;
15
16
use GraphQL \Type \Schema ;
@@ -363,6 +364,7 @@ public function buildSchemaFromConfig(array $schemaConfig): Schema
363
364
$ schemaMutation = $ schemaConfig ['mutation ' ] ?? [];
364
365
$ schemaSubscription = $ schemaConfig ['subscription ' ] ?? [];
365
366
$ schemaTypes = $ schemaConfig ['types ' ] ?? [];
367
+ $ schemaDirectives = $ schemaConfig ['directives ' ] ?? [];
366
368
367
369
$ this ->addTypes ($ schemaTypes );
368
370
@@ -378,10 +380,18 @@ public function buildSchemaFromConfig(array $schemaConfig): Schema
378
380
? $ this ->objectType ($ schemaSubscription , ['name ' => 'Subscription ' ])
379
381
: null ;
380
382
383
+ $ directives = Directive::getInternalDirectives ();
384
+
385
+ foreach ($ schemaDirectives as $ class ) {
386
+ $ directive = $ this ->app ->make ($ class );
387
+ $ directives [$ directive ->name ] = $ directive ;
388
+ }
389
+
381
390
return new Schema ([
382
391
'query ' => $ query ,
383
392
'mutation ' => $ mutation ,
384
393
'subscription ' => $ subscription ,
394
+ 'directives ' => $ directives ,
385
395
'types ' => function () {
386
396
$ types = [];
387
397
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 15
15
use Rebing \GraphQL \Exception \SchemaNotFound ;
16
16
use Rebing \GraphQL \Exception \TypeNotFound ;
17
17
use Rebing \GraphQL \Support \Facades \GraphQL ;
18
+ use Rebing \GraphQL \Tests \Support \Directives \ExampleDirective ;
18
19
use Rebing \GraphQL \Tests \Support \Objects \CustomExampleType ;
19
20
use Rebing \GraphQL \Tests \Support \Objects \ExamplesQuery ;
20
21
use Rebing \GraphQL \Tests \Support \Objects \ExampleType ;
@@ -454,6 +455,25 @@ public function testAddSchemaObjectAndExecuteQueryWithRootValue(): void
454
455
self ::assertSame ($ expectedResult , $ result );
455
456
}
456
457
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
+
457
477
public function testIsMacroable (): void
458
478
{
459
479
self ::assertContains (Macroable::class, class_uses_recursive (GraphQL::getFacadeRoot ()));
You can’t perform that action at this time.
0 commit comments