Skip to content

Commit

Permalink
Add support for sending variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarten Staa committed Feb 25, 2021
1 parent ff4ab8b commit 83d6bde
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
16 changes: 16 additions & 0 deletions config/lighthouse-apollo.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@
* Which request headers to not include in the HTTP information for a tracing. Should be lowercase.
*/
'excluded_request_headers' => ['authentication', 'cookie', 'set-cookie'],

/**
* Whether to send query/mutation variables to Apollo Studio.
*/
'include_variables' => true,

/**
* Which variables to include if include_variables is true. If `variables_only_names` has any values,
* only variables with those names will be sent, the rest will be masked in the variables sent to
* Apollo. If `variables_except_names` has any values, those will be masked.
* The `except` setting has greater priority than `only`. In other words, if a key such as 'token' is
* present in both only `variables_only_names` and in `variables_except_names`, the value will still
* be masked.
*/
'variables_only_names' => [],
'variables_except_names' => ['password', 'username', 'email', 'token'],
];
36 changes: 36 additions & 0 deletions src/Listeners/ManipulateResultListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use JsonException;
use LogicException;
use Mdg\Trace\HTTP\Method;
use Mdg\Trace\HTTP\Values;
Expand Down Expand Up @@ -87,6 +88,7 @@ public function handle(ManipulateResult $event): void

$trace = new TracingResult(
$this->graphQlRequest->query(),
$this->variables(),
$this->extractClientInformation(),
$this->extractHttpInformation(),
$event->result->extensions['tracing'] ?? [],
Expand Down Expand Up @@ -166,4 +168,38 @@ private function isIntrospectionQuery(): bool
{
return (bool) preg_match('/^\s*query[^{]*{\s*(\w+:\s*)?__schema\s*{/', $this->graphQlRequest->query());
}

private function variables(): ?array
{
if (!$this->config->get('lighthouse-apollo.include_variables')) {
return null;
}

$variables = [];
/** @var string[] $only */
$only = $this->config->get('lighthouse-apollo.variables_only_names');
/** @var string[] $except */
$except = $this->config->get('lighthouse-apollo.variables_except_names');
foreach ($this->graphQlRequest->variables() as $key => $value) {
if (
(count($only) > 0 && !in_array($key, $only, true)) ||
(count($except) > 0 && in_array($key, $except, true))
) {
// Special case for private variables. Note that this is a different
// representation from a variable containing the empty string, as that
// will be sent as '""'.
$value = '';
} else {
try {
$value = json_encode($value, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
$value = '"[Unable to convert value to JSON]"';
}
}

$variables[$key] = $value;
}

return $variables;
}
}
12 changes: 11 additions & 1 deletion src/TracingResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class TracingResult
{
public string $queryText;

public ?array $variables;

public array $client;

public array $http;
Expand Down Expand Up @@ -44,14 +46,16 @@ class TracingResult
* Constructor.
*
* @param string $queryText
* @param array|null $variables
* @param array $client
* @param array $http
* @param array $tracing
* @param array $errors
*/
public function __construct(string $queryText, array $client, array $http, array $tracing, array $errors)
public function __construct(string $queryText, ?array $variables, array $client, array $http, array $tracing, array $errors)
{
$this->queryText = $queryText;
$this->variables = $variables;
$this->client = $client;
$this->http = $http;
$this->tracing = $tracing;
Expand Down Expand Up @@ -86,6 +90,12 @@ public function getTracingAsProtobuf(): Trace
if (!empty($this->client['version'])) {
$tracingData['client_version'] = $this->client['version'];
}
if ($this->variables !== null) {
$tracingData['details'] = new Trace\Details([
'variables_json' => $this->variables,
]);
}

$result = new Trace($tracingData);

foreach ($this->tracing['execution']['resolvers'] as $trace) {
Expand Down
23 changes: 23 additions & 0 deletions tests/TracingResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function testGetTracingAsProtobuf(): void
{
$tracing = new TracingResult(
'{ hello }',
null,
$this->sampleClientData(),
$this->sampleHttpData(),
$this->sampleTracingData(),
Expand All @@ -98,6 +99,25 @@ public function testGetTracingAsProtobuf(): void
self::assertEquals('phpunit', $proto->getClientName());
}

/**
* @covers \BrightAlley\LighthouseApollo\TracingResult::getTracingAsProtobuf
*/
public function testGetTracingAsProtobufWithVariables(): void
{
$tracing = new TracingResult(
'{ hello }',
['key' => json_encode('value', JSON_THROW_ON_ERROR)],
$this->sampleClientData(),
$this->sampleHttpData(),
$this->sampleTracingData(),
[]
);
$proto = $tracing->getTracingAsProtobuf();

self::assertNotNull($proto->getDetails());
self::assertNotNull($proto->getDetails()->getVariablesJson());
}

public function nullableClientFields(): array
{
return [
Expand All @@ -117,6 +137,7 @@ public function testGetTracingAsProtobufNullableClientFields(array $clientData):
{
$tracing = new TracingResult(
'{ hello }',
null,
array_merge($this->sampleClientData(), $clientData),
$this->sampleHttpData(),
$this->sampleTracingData(),
Expand All @@ -138,6 +159,7 @@ public function testGetTracingAsProtobufWithErrors(): void
// Top level error should show up on root.
$tracing = new TracingResult(
'{ hello }',
null,
$this->sampleClientData(),
$this->sampleHttpData(),
$this->sampleTracingData(),
Expand Down Expand Up @@ -171,6 +193,7 @@ public function testGetTracingAsProtobufWithErrors(): void
];
$tracing = new TracingResult(
'{ hello { world } }',
null,
$this->sampleClientData(),
$this->sampleHttpData(),
$tracingData,
Expand Down

0 comments on commit 83d6bde

Please sign in to comment.