From 031d5a89b0abf602996dc9d687ec1e9463667754 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 5 Feb 2018 12:51:44 -0700 Subject: [PATCH 1/2] Add support for query batching --- src/Tracing.php | 44 ++++++++++++++++++++++++++++++++--------- wp-graphql-insights.php | 2 +- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Tracing.php b/src/Tracing.php index 812f8fa..1d513f6 100755 --- a/src/Tracing.php +++ b/src/Tracing.php @@ -186,12 +186,11 @@ public static function get_resolver_start_offset() { /** * Given a resolver start time, returns the duration of a resolver - * @param string $resolver_start The microtime for the resolver start time * @return mixed */ - public static function get_resolver_duration( $resolver_start ) { + public static function get_resolver_duration() { $resolver_end = microtime( true ); - return ( $resolver_end - $resolver_start ) * 1000000; + return ( $resolver_end - self::$request_start_microtime ) * 1000000; } /** @@ -269,7 +268,7 @@ public static function get_trace() { * * @param $response * - * @return \GraphQL\Executor\ExecutionResult + * @return mixed */ public static function add_tracing_to_response_extensions( $response, $schema, $operation_name, $request, $variables ) { @@ -292,8 +291,21 @@ public static function add_tracing_to_response_extensions( $response, $schema, $ /** * If tracing should be included in the response */ - if ( true === self::include_tracing_in_response( $response, $schema, $operation_name, $request, $variables ) ) { - $response->extensions['tracing'] = self::get_trace(); + if ( is_array( $response ) ) { + foreach ( $response as $key => $res ) { + if ( true === self::include_tracing_in_response( $res, $schema, $operation_name, $request, $variables ) ) { + if ( is_object( $res ) ) { + $res->extensions['tracing'] = self::get_trace(); + } else if ( is_array( $res ) ) { + $res['extensions']['tracing'] = self::get_trace(); + } + } + $response[ $key ] = $res; + } + } else { + if ( true === self::include_tracing_in_response( $response, $schema, $operation_name, $request, $variables ) ) { + $response->extensions['tracing'] = self::get_trace(); + } } /** @@ -315,9 +327,23 @@ public static function add_tracing_to_response_extensions( $response, $schema, $ */ public static function add_tracked_queries_to_response_extensions( $response, $schema, $operation_name, $request, $variables ) { - if ( true === self::include_tracing_in_response( $response, $schema, $operation_name, $request, $variables ) ) { - $response->extensions['queryLog'] = QueryTrace::get_trace(); + if ( is_array( $response ) ) { + foreach( $response as $key => $res ) { + if ( true === self::include_tracing_in_response( $res, $schema, $operation_name, $request, $variables ) ) { + if ( is_object( $res ) ) { + $res->extensions['queryLog'] = self::get_trace(); + } else if ( is_array( $res ) ) { + $res['extensions']['queryLog'] = self::get_trace(); + } + } + $response[ $key ] = $res; + } + } else { + if ( true === self::include_tracing_in_response( $response, $schema, $operation_name, $request, $variables ) ) { + $response->extensions['queryLog'] = QueryTrace::get_trace(); + } } + return $response; } @@ -357,7 +383,7 @@ public static function init_field_resolver_trace( $source, $args, $context, $inf * @param $field */ public static function close_field_resolver_trace( $source, $args, $context, $info, $field_resolver, $type_name, $field_key, $field ) { - self::$field_resolver_trace['duration'] = Tracing::get_resolver_duration( self::$resolver_start ); + self::$field_resolver_trace['duration'] = Tracing::get_resolver_duration(); Tracing::trace_resolver( self::$field_resolver_trace ); self::reset_field_resolver_trace(); } diff --git a/wp-graphql-insights.php b/wp-graphql-insights.php index 320f65f..0ed49dd 100644 --- a/wp-graphql-insights.php +++ b/wp-graphql-insights.php @@ -196,4 +196,4 @@ function graphql_insights_init() { return \WPGraphQL\Extensions\Insights::instance(); } -add_action( 'graphql_init', '\WPGraphQL\Extensions\graphql_insights_init' ); +add_action( 'init', '\WPGraphQL\Extensions\graphql_insights_init' ); From d1541d73bebb4a47725ed9c02e82cfeb1521f99c Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 5 Feb 2018 14:25:51 -0700 Subject: [PATCH 2/2] Add support for query batching --- src/Tracing.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Tracing.php b/src/Tracing.php index 1d513f6..34cba2d 100755 --- a/src/Tracing.php +++ b/src/Tracing.php @@ -253,6 +253,8 @@ public static function get_trace() { ] ]; + + /** * Filer and return the trace data * @param array $trace An array of Trace data @@ -293,11 +295,17 @@ public static function add_tracing_to_response_extensions( $response, $schema, $ */ if ( is_array( $response ) ) { foreach ( $response as $key => $res ) { + /** + * Only include the trace data once per request + */ + if ( 0 !== $key ) { + return false; + } if ( true === self::include_tracing_in_response( $res, $schema, $operation_name, $request, $variables ) ) { if ( is_object( $res ) ) { - $res->extensions['tracing'] = self::get_trace(); + $res->extensions['tracing'] = self::get_trace( $key ); } else if ( is_array( $res ) ) { - $res['extensions']['tracing'] = self::get_trace(); + $res['extensions']['tracing'] = self::get_trace( $key ); } } $response[ $key ] = $res; @@ -329,11 +337,17 @@ public static function add_tracked_queries_to_response_extensions( $response, $s if ( is_array( $response ) ) { foreach( $response as $key => $res ) { + /** + * Only include the trace data once per request + */ + if ( 0 !== $key ) { + return false; + } if ( true === self::include_tracing_in_response( $res, $schema, $operation_name, $request, $variables ) ) { if ( is_object( $res ) ) { - $res->extensions['queryLog'] = self::get_trace(); + $res->extensions['queryLog'] = QueryTrace::get_trace( $key ); } else if ( is_array( $res ) ) { - $res['extensions']['queryLog'] = self::get_trace(); + $res['extensions']['queryLog'] = QueryTrace::get_trace( $key ); } } $response[ $key ] = $res;