File tree Expand file tree Collapse file tree 4 files changed +63
-0
lines changed Expand file tree Collapse file tree 4 files changed +63
-0
lines changed Original file line number Diff line number Diff 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
792022-06-11, 8.3.0
810-----------------
Original file line number Diff line number Diff line change 1010use GraphQL \Error \FormattedError ;
1111use GraphQL \Executor \ExecutionResult ;
1212use GraphQL \Server \OperationParams as BaseOperationParams ;
13+ use GraphQL \Type \Definition \Directive ;
1314use GraphQL \Type \Definition \ObjectType ;
1415use GraphQL \Type \Definition \Type ;
1516use 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 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 1515use Rebing \GraphQL \Exception \SchemaNotFound ;
1616use Rebing \GraphQL \Exception \TypeNotFound ;
1717use Rebing \GraphQL \Support \Facades \GraphQL ;
18+ use Rebing \GraphQL \Tests \Support \Directives \ExampleDirective ;
1819use Rebing \GraphQL \Tests \Support \Objects \CustomExampleType ;
1920use Rebing \GraphQL \Tests \Support \Objects \ExamplesQuery ;
2021use 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 ()));
You can’t perform that action at this time.
0 commit comments