@@ -28,17 +28,15 @@ class LogData
2828 *
2929 * @param RequestInterface $request
3030 * @param array $data
31- * @param Schema $schema
32- * @param HttpResponse $response
31+ * @param Schema|null $schema
32+ * @param HttpResponse|null $response
3333 * @return array
34- *
35- * @throws SyntaxError
3634 */
3735 public function getRequestInformation (
3836 RequestInterface $ request ,
3937 array $ data ,
40- Schema $ schema ,
41- HttpResponse $ response
38+ Schema $ schema = null ,
39+ HttpResponse $ response = null
4240 ) : array {
4341 $ requestInformation = [];
4442 $ requestInformation [LoggerInterface::HTTP_METHOD ] = $ request ->getMethod ();
@@ -51,18 +49,23 @@ public function getRequestInformation(
5149 : 'false ' ;
5250 $ requestInformation [LoggerInterface::REQUEST_LENGTH ] = $ request ->getHeader ('Content-Length ' ) ?: '' ;
5351
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+ }
6063
61- $ operationNames = array_merge (array_keys ($ mutationOperations ), array_keys ($ queryOperations ));
62- $ requestInformation [LoggerInterface::OPERATION_NAMES ] =
63- count ($ operationNames ) > 0 ? implode (", " , $ operationNames ) : 'operationNameNotFound ' ;
6464 $ 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+ }
6669
6770 return $ requestInformation ;
6871 }
@@ -74,25 +77,27 @@ public function getRequestInformation(
7477 *
7578 * @param string $query
7679 * @return int
77- * @throws SyntaxError
78- * @throws \Exception
7980 */
8081 private function getFieldCount (string $ query ): int
8182 {
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+ ]
9295 ]
93- ]
94- );
95- return $ totalFieldCount ;
96+ );
97+ return $ totalFieldCount ;
98+ }
99+ } catch (SyntaxError $ syntaxError ) {
100+ } catch (\Exception $ exception ) {
96101 }
97102 return 0 ;
98103 }
0 commit comments