|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Alancolant\PulseGraphql\Recorders; |
| 4 | + |
| 5 | +use Alancolant\PulseGraphql\ResolverMiddlewares\SendEventToPulseMiddleware; |
| 6 | +use GraphQL\Language\Printer; |
| 7 | +use GraphQL\Type\Definition\ResolveInfo; |
| 8 | +use Illuminate\Config\Repository; |
| 9 | +use Illuminate\Contracts\Foundation\Application; |
| 10 | +use Illuminate\Support\Arr; |
| 11 | +use Illuminate\Support\Carbon; |
| 12 | +use Illuminate\Support\Facades\Config; |
| 13 | +use Illuminate\Support\Facades\Route; |
| 14 | +use Laragraph\Utils\RequestParser; |
| 15 | +use Laravel\Pulse\Concerns\ConfiguresAfterResolving; |
| 16 | +use Laravel\Pulse\Pulse; |
| 17 | +use Rebing\GraphQL\GraphQL; |
| 18 | + |
| 19 | +class GraphqlRecorder |
| 20 | +{ |
| 21 | + |
| 22 | + use ConfiguresAfterResolving; |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new recorder instance. |
| 26 | + */ |
| 27 | + public function __construct( |
| 28 | + protected Pulse $pulse, |
| 29 | + protected Repository $config, |
| 30 | + protected RequestParser $parser |
| 31 | + ) |
| 32 | + { |
| 33 | + // |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Register the recorder. |
| 38 | + */ |
| 39 | + public function register(callable $record, Application $app): void |
| 40 | + { |
| 41 | + $this->afterResolving( |
| 42 | + $app, GraphQL::class, |
| 43 | + function () use (&$record) { |
| 44 | + \Rebing\GraphQL\Support\Facades\GraphQL::appendGlobalResolverMiddleware(new SendEventToPulseMiddleware($record)); |
| 45 | + } |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function record(Carbon $startedAt, array $args, $context, ResolveInfo $info): void |
| 50 | + { |
| 51 | + |
| 52 | + if (!in_array($info->parentType, ["Query", "Mutation"])) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $schemaName = Arr::first(Route::current()->parameters()) ?? Config::get('graphql.default_schema', 'default'); |
| 57 | + |
| 58 | + $operationType = $info->operation->operation; |
| 59 | + $query = $info->fieldName; |
| 60 | + |
| 61 | +// $operation = $info->operation->name->value ?? null; |
| 62 | +// $fields = array_keys(Arr::dot($info->getFieldSelection(PHP_INT_MAX))); |
| 63 | +// $vars = $this->formatVariableDefinitions($info->operation->variableDefinitions); |
| 64 | + |
| 65 | + |
| 66 | + $duration = ($startedAt->diffInMilliseconds()); |
| 67 | + $this->pulse->record( |
| 68 | + type: 'graphql_request', |
| 69 | + key: json_encode([ |
| 70 | + $schemaName, $operationType, $query |
| 71 | + ], flags: JSON_THROW_ON_ERROR), |
| 72 | + value: $duration, |
| 73 | + timestamp: $startedAt, |
| 74 | + )->max()->avg()->count(); |
| 75 | + } |
| 76 | + |
| 77 | + private function formatVariableDefinitions(?iterable $variableDefinitions = []): array |
| 78 | + { |
| 79 | + return collect($variableDefinitions) |
| 80 | + ->map(function ($def) { |
| 81 | + return Printer::doPrint($def); |
| 82 | + }) |
| 83 | + ->toArray(); |
| 84 | + } |
| 85 | +} |
0 commit comments