@@ -28,17 +28,15 @@ class LogData
28
28
*
29
29
* @param RequestInterface $request
30
30
* @param array $data
31
- * @param Schema $schema
32
- * @param HttpResponse $response
31
+ * @param Schema|null $schema
32
+ * @param HttpResponse|null $response
33
33
* @return array
34
- *
35
- * @throws SyntaxError
36
34
*/
37
35
public function getRequestInformation (
38
36
RequestInterface $ request ,
39
37
array $ data ,
40
- Schema $ schema ,
41
- HttpResponse $ response
38
+ Schema $ schema = null ,
39
+ HttpResponse $ response = null
42
40
) : array {
43
41
$ requestInformation = [];
44
42
$ requestInformation [LoggerInterface::HTTP_METHOD ] = $ request ->getMethod ();
@@ -51,18 +49,23 @@ public function getRequestInformation(
51
49
: 'false ' ;
52
50
$ requestInformation [LoggerInterface::REQUEST_LENGTH ] = $ request ->getHeader ('Content-Length ' ) ?: '' ;
53
51
54
- $ schemaConfig = $ schema ->getConfig ();
55
- $ mutationOperations = $ schemaConfig ->getMutation ()->getFields ();
56
- $ queryOperations = $ schemaConfig ->getQuery ()->getFields ();
57
- $ requestInformation [LoggerInterface::HAS_MUTATION ] = count ($ mutationOperations ) > 0 ? 'true ' : 'false ' ;
58
- $ requestInformation [LoggerInterface::NUMBER_OF_OPERATIONS ] =
59
- count ($ mutationOperations ) + count ($ queryOperations );
52
+ if ($ schema ) {
53
+ $ schemaConfig = $ schema ->getConfig ();
54
+ $ mutationOperations = $ schemaConfig ->getMutation ()->getFields ();
55
+ $ queryOperations = $ schemaConfig ->getQuery ()->getFields ();
56
+ $ requestInformation [LoggerInterface::HAS_MUTATION ] = count ($ mutationOperations ) > 0 ? 'true ' : 'false ' ;
57
+ $ requestInformation [LoggerInterface::NUMBER_OF_OPERATIONS ] =
58
+ count ($ mutationOperations ) + count ($ queryOperations );
59
+ $ operationNames = array_merge (array_keys ($ mutationOperations ), array_keys ($ queryOperations ));
60
+ $ requestInformation [LoggerInterface::OPERATION_NAMES ] =
61
+ count ($ operationNames ) > 0 ? implode (", " , $ operationNames ) : 'operationNameNotFound ' ;
62
+ }
60
63
61
- $ operationNames = array_merge (array_keys ($ mutationOperations ), array_keys ($ queryOperations ));
62
- $ requestInformation [LoggerInterface::OPERATION_NAMES ] =
63
- count ($ operationNames ) > 0 ? implode (", " , $ operationNames ) : 'operationNameNotFound ' ;
64
64
$ requestInformation [LoggerInterface::COMPLEXITY ] = $ this ->getFieldCount ($ data ['query ' ] ?? '' );
65
- $ requestInformation [LoggerInterface::HTTP_RESPONSE_CODE ] = $ response ->getHttpResponseCode ();
65
+
66
+ if ($ response ) {
67
+ $ requestInformation [LoggerInterface::HTTP_RESPONSE_CODE ] = $ response ->getHttpResponseCode ();
68
+ }
66
69
67
70
return $ requestInformation ;
68
71
}
@@ -74,25 +77,27 @@ public function getRequestInformation(
74
77
*
75
78
* @param string $query
76
79
* @return int
77
- * @throws SyntaxError
78
- * @throws \Exception
79
80
*/
80
81
private function getFieldCount (string $ query ): int
81
82
{
82
- if (!empty ($ query )) {
83
- $ totalFieldCount = 0 ;
84
- $ queryAst = Parser::parse (new Source ($ query ?: '' , 'GraphQL ' ));
85
- Visitor::visit (
86
- $ queryAst ,
87
- [
88
- 'leave ' => [
89
- NodeKind::FIELD => function (Node $ node ) use (&$ totalFieldCount ) {
90
- $ totalFieldCount ++;
91
- }
83
+ try {
84
+ if (!empty ($ query )) {
85
+ $ totalFieldCount = 0 ;
86
+ $ queryAst = Parser::parse (new Source ($ query ?: '' , 'GraphQL ' ));
87
+ Visitor::visit (
88
+ $ queryAst ,
89
+ [
90
+ 'leave ' => [
91
+ NodeKind::FIELD => function (Node $ node ) use (&$ totalFieldCount ) {
92
+ $ totalFieldCount ++;
93
+ }
94
+ ]
92
95
]
93
- ]
94
- );
95
- return $ totalFieldCount ;
96
+ );
97
+ return $ totalFieldCount ;
98
+ }
99
+ } catch (SyntaxError $ syntaxError ) {
100
+ } catch (\Exception $ exception ) {
96
101
}
97
102
return 0 ;
98
103
}
0 commit comments