Skip to content

Commit

Permalink
Add new request logger
Browse files Browse the repository at this point in the history
  • Loading branch information
johannessteu committed Jul 15, 2019
2 parents 4b5c447 + 365aa79 commit 24f8479
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Classes/Controller/GraphQLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Neos\Flow\Mvc\Controller\ActionController;
use t3n\GraphQL\Context;
use t3n\GraphQL\Exception\InvalidContextException;
use t3n\GraphQL\Log\RequestLoggerInterface;
use t3n\GraphQL\Service\DefaultFieldResolver;
use t3n\GraphQL\Service\SchemaService;
use t3n\GraphQL\Service\ValidationRuleService;
Expand Down Expand Up @@ -43,6 +44,13 @@ class GraphQLController extends ActionController
*/
protected $endpointConfigurations;

/**
* @Flow\Inject
*
* @var RequestLoggerInterface
*/
protected $requestLogger;

/**
* phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification
*
Expand All @@ -62,8 +70,10 @@ public function queryAction(string $endpoint, string $query, ?array $variables =
$schema = $this->schemaService->getSchemaForEndpoint($endpoint);
$validationRules = $this->validationRuleService->getValidationRulesForEndpoint($endpoint);

if (isset($this->endpointConfigurations[$endpoint]['context'])) {
$contextClassname = $this->endpointConfigurations[$endpoint]['context'];
$endpointConfiguration = $this->endpointConfigurations[$endpoint] ?? [];

if (isset($endpointConfiguration['context'])) {
$contextClassname = $endpointConfiguration['context'];
} else {
$contextClassname = $this->contextClassName;
}
Expand All @@ -73,6 +83,10 @@ public function queryAction(string $endpoint, string $query, ?array $variables =
throw new InvalidContextException('The configured Context must extend \t3n\GraphQL\Context', 1545945332);
}

if (isset($endpointConfiguration['logRequests']) && $endpointConfiguration['logRequests'] === true) {
$this->requestLogger->info('Incoming graphql request', ['endpoint' => $endpoint, 'query' => json_encode($query), 'variables' => empty($variables) ? 'none' : $variables]);
}

GraphQL::setDefaultFieldResolver([DefaultFieldResolver::class, 'resolve']);

$result = GraphQL::executeQuery(
Expand Down
11 changes: 11 additions & 0 deletions Classes/Log/RequestLoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace t3n\GraphQL\Log;

use Psr\Log\LoggerInterface;

interface RequestLoggerInterface extends LoggerInterface
{
}
8 changes: 8 additions & 0 deletions Classes/Service/SchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public function getSchemaForEndpoint(string $endpoint): Schema
return $schema;
}

/**
* @return mixed[]
*/
public function getEndpointConfiguration(string $endpoint): ?array
{
return $this->endpoints[$endpoint] ?? null;
}

protected function getSchemaFromEnvelope(string $envelopeClassName): Schema
{
$envelope = $this->objectManager->get($envelopeClassName);
Expand Down
8 changes: 8 additions & 0 deletions Configuration/Objects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ t3n\GraphQL\Service\SchemaService:
arguments:
1:
value: t3n_GraphQL_Schema

t3n\GraphQL\Log\RequestLoggerInterface:
scope: singleton
factoryObjectName: Neos\Flow\Log\PsrLoggerFactoryInterface
factoryMethodName: get
arguments:
1:
value: graphQLRequestLogger
12 changes: 12 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ Neos:
'graphQLOptions':
position: 'before routing'
component: 't3n\GraphQL\Http\HttpOptionsComponent'
log:
graphQLRequestLogger:
logger: Neos\Flow\Log\Logger
backend: Neos\Flow\Log\Backend\FileBackend
backendOptions:
logFileURL: '%FLOW_PATH_DATA%Logs/GraphQLRequests.log'
createParentDirectories: true
severityThreshold: '%LOG_INFO%'
maximumLogFileSize: 10485760
logFilesToKeep: 1
logMessageOrigin: false

t3n:
GraphQL:
context: 't3n\GraphQL\Context'
includeExceptionMessageInOutput: true
endpoints: []
# 'some-endpoint':
# 'logRequests': true # if enabled all requests are logged
# 'context:' 'Foo\Vendor\GraphQL\Context # optional context that overrides the global context
# 'schemaEnvelope': 'Some\Fully\Qualified\Namespace'
15 changes: 15 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,21 @@ class MutationResolver implements ResolverInterface
}
```

### Log incoming requests

You can enable logging of incoming requests per endpoint:

```yaml
t3n:
GraphQL:
endpoints:
'your-endpoint':
logRequests: true
```

Once activated all incoming requests will be logged to `Data/Logs/GraphQLRequests.log`. Each log entry
will contain the endpoint, query and variables.

### Secure your endpoint

To secure your api endpoints you have several options. The easiest way is to just configure
Expand Down

0 comments on commit 24f8479

Please sign in to comment.