@@ -33,6 +33,7 @@ php artisan vendor:publish --provider="StudioNet\GraphQL\ServiceProvider"
33
33
- [ Definition] ( #definition )
34
34
- [ Query] ( #query )
35
35
- [ Mutation] ( #mutation )
36
+ - [ Pipeline] ( #pipeline )
36
37
- [ Require authorization] ( #require-authorization )
37
38
- [ Self documentation] ( #self-documentation )
38
39
- [ Examples] ( #examples )
@@ -331,6 +332,108 @@ return [
331
332
];
332
333
```
333
334
335
+ ### Pipeline
336
+
337
+ Pipeline are used to convert a definition into queryable and mutable operations.
338
+ But, you can easily create your own and manage useful cases like asserting ACL
339
+ before doing anything, etc.
340
+
341
+ Pipeline is implemented using the same [ Laravel Middleware] ( https://laravel.com/docs/5.7/middleware ) format
342
+ but pass as first argument the Eloquent Query Builder.
343
+
344
+ ## Create new pipe
345
+
346
+ ``` php
347
+ namespace App/GraphQL/Pipe;
348
+
349
+ use Closure;
350
+ use Illuminate\Database\Eloquent\Builder;
351
+
352
+ class OnlyAuthored {
353
+ /**
354
+ * returns only posts that the viewer handle
355
+ *
356
+ * @param Builder $builder
357
+ * @param Closure $next
358
+ * @param array $opts
359
+ * @return \Illuminate\Database\Eloquent\Model
360
+ */
361
+ public function handle(Builder $builder, Closure $next, array $opts) {
362
+ $builder->where('author_id', $opts['context']->getKey());
363
+
364
+ return $next($builder);
365
+ }
366
+ }
367
+ ```
368
+
369
+ ``` php
370
+ namespace App\GraphQL\Definition;
371
+
372
+ class PostDefinition extends EloquentDefinition {
373
+ // ...
374
+
375
+ /**
376
+ * {@inheritDoc}
377
+ *
378
+ * @return array
379
+ */
380
+ public function getPipes(): array {
381
+ return array_merge_recursive(parent::getPipes(), [
382
+ 'list' => [\App\GraphQL\Pipe\OnlyAuthored::class],
383
+ ]);
384
+ }
385
+
386
+ // ...
387
+ }
388
+ ```
389
+
390
+ With this sample, when you'll query ` posts ` query, you'll only get viewer posts,
391
+ not all one. Also, you can specify arguments in the pipe, like following :
392
+
393
+ ``` php
394
+ namespace App/GraphQL/Pipe;
395
+
396
+ use Closure;
397
+ use Illuminate\Database\Eloquent\Builder;
398
+ use GraphQL\Type\Definition\Type;
399
+ use StudioNet\GraphQL\Support\Pipe\Argumentable;
400
+ use StudioNet\GraphQL\Support\Definition\Definition;
401
+
402
+ class FilterableGroups implements Argumentable {
403
+ /**
404
+ * returns only given groups
405
+ *
406
+ * @param Builder $builder
407
+ * @param Closure $next
408
+ * @param array $opts
409
+ * @return \Illuminate\Database\Eloquent\Model
410
+ */
411
+ public function handle(Builder $builder, Closure $next, array $opts) {
412
+ if (array_get($opts, ['args.group_ids', false])) {
413
+ $builder->whereIn('group_id', $opts['args']['group_ids']);
414
+ }
415
+
416
+ return $next($builder);
417
+ }
418
+
419
+ /**
420
+ * @implements
421
+ *
422
+ * @param Definition $definition
423
+ * @return array
424
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
425
+ */
426
+ public function getArguments(Definition $definition): array {
427
+ return [
428
+ 'groups_id' => [
429
+ 'type' => Type::json(),
430
+ 'description' => 'Filtering by group IDs'
431
+ ]
432
+ ];
433
+ }
434
+ }
435
+ ```
436
+
334
437
### Require authorization
335
438
336
439
Currently you have a possibility to protect your own queries and mutations. You have to implement ` authorize() ` method in your query/mutation, that return a boolean, that indicates, if requested query/mutation has to be executed. If method return ` false ` , an ` UNAUTHORIZED ` GraphQL-Error will be thrown.
0 commit comments