diff --git a/composer.json b/composer.json index 3dbc7f7..3989989 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "description": "Insights for WPGraphQL. Performance and error logging.", "type": "wordpress-plugin", "license": "GPLv3", + "version":"0.2.0", "authors": [ { "name": "Jason Bahl", diff --git a/src/InstrumentSchema.php b/src/InstrumentSchema.php deleted file mode 100755 index 7b5aa46..0000000 --- a/src/InstrumentSchema.php +++ /dev/null @@ -1,147 +0,0 @@ -getTypeMap(); - - if ( ! empty( $types ) && is_array( $types ) ) { - foreach ( $types as $type_name => $type_object ) { - if ( $type_object instanceof ObjectType || $type_object instanceof WPObjectType ) { - $fields = $type_object->getFields(); - $new_fields = self::wrap_field_resolvers( $fields, $type_name ); - $new_type_object = $type_object; - $new_type_object->config['fields'] = $new_fields; - $new_types[ $type_name ] = $new_type_object; - } - } - } - - if ( ! empty( $new_types ) && is_array( $new_types ) ) { - $schema->config['types'] = $new_types; - } - - return $schema; - - } - - /** - * This wraps field resolvers to gather reporting data - * - * @since 0.0.1 - * @param $fields - * @param $type_name - * - * @return array - */ - public static function wrap_field_resolvers( $fields, $type_name ) { - - if ( ! empty( $fields ) && is_array( $fields ) ) { - - foreach ( $fields as $field_key => $field ) { - - $start_offset = Tracing::get_resolver_start_offset(); - $resolver_start = null; - $resolver_start = microtime( true ); - - if ( $field instanceof FieldDefinition ) { - - /** - * Get the fields resolve function - * @since 0.0.1 - */ - $field_resolver = ! empty( $field->resolveFn ) ? $field->resolveFn : null; - - /** - * Replace the existing field resolve method with a new function that captures data about - * the resolver to be stored in the resolver_report - * @since 0.0.1 - * - * @param $source - * @param array $args - * @param AppContext $context - * @param ResolveInfo $info - * - * @use function|null $field_resolve_function - * @use string $type_name - * @use string $field_key - * @use object $field - * - * @return mixed - * @throws \Exception - */ - $field->resolveFn = function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $field_resolver, $type_name, $field_key, $field, $start_offset, $resolver_start ) { - - $trace = [ - 'path' => $info->path, - 'parentType' => $info->parentType, - 'fieldName' => $info->fieldName, - 'returnType' => $info->returnType, - 'startOffset' => $start_offset, - ]; - - /** - * If the current field doesn't have a resolve function, use the defaultFieldResolver, - * otherwise use the $field_resolver - */ - if ( null === $field_resolver || ! is_callable( $field_resolver ) ) { - $result = Executor::defaultFieldResolver( $source, $args, $context, $info ); - } else { - $result = call_user_func( $field_resolver, $source, $args, $context, $info ); - } - - $trace['duration'] = Tracing::get_resolver_duration( $resolver_start ); - Tracing::trace_resolver( $trace ); - return $result; - - }; - - } - } - } - - /** - * Return the fields - * @since 0.0.1 - */ - return $fields; - - } - -} diff --git a/src/Tracing.php b/src/Tracing.php index d0b4ee1..812f8fa 100755 --- a/src/Tracing.php +++ b/src/Tracing.php @@ -321,7 +321,17 @@ public static function add_tracked_queries_to_response_extensions( $response, $s return $response; } - public static function init_field_resolver_trace( $field, $type_object, $source, $args, AppContext $context, ResolveInfo $info ) { + /** + * @param $source + * @param $args + * @param $context + * @param $info + * @param $field_resolver + * @param $type_name + * @param $field_key + * @param $field + */ + public static function init_field_resolver_trace( $source, $args, $context, $info, $field_resolver, $type_name, $field_key, $field ) { $start_offset = Tracing::get_resolver_start_offset(); self::$resolver_start = microtime( true ); @@ -336,12 +346,25 @@ public static function init_field_resolver_trace( $field, $type_object, $source, } - public static function close_field_resolver_trace( $field, $type_object, $source, $args, AppContext $context, ResolveInfo $info ) { + /** + * @param $source + * @param $args + * @param $context + * @param $info + * @param $field_resolver + * @param $type_name + * @param $field_key + * @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 ); Tracing::trace_resolver( self::$field_resolver_trace ); self::reset_field_resolver_trace(); } + /** + * + */ protected static function reset_field_resolver_trace() { self::$field_resolver_trace = []; self::$resolver_start = null; diff --git a/tests/test-InstrumentSchema.php b/tests/test-InstrumentSchema.php deleted file mode 100644 index d1a96d1..0000000 --- a/tests/test-InstrumentSchema.php +++ /dev/null @@ -1,55 +0,0 @@ - new \WPGraphQL\Type\WPObjectType([ - 'name' => 'test', - 'resolve' => function( $root, $args, $context, $info ) { - return 'test'; - } - ]) - ]; - - return new \WPGraphQL\WPSchema( $schema ); - } - - /** - * Test getting the resolver traces - */ - public function testGetResolver() { - $test_array = [ - 'test' => 'test' - ]; - _InstrumentSchema::set_resolver_traces( $test_array ); - $actual = \WPGraphQL\Extensions\Insights\InstrumentSchema::get_resolver(); - $this->assertEquals( $test_array, $actual ); - } - - /** - * Ensure that an instrumented schema still returns a WPSchema - */ - public function test_InstrumentSchemaReturnsValidSchema() { - $test_schema = self::_get_test_schema(); - $this->assertTrue( $test_schema instanceof \WPGraphQL\WPSchema ); - - $instrumented_schema = \WPGraphQL\Extensions\Insights\InstrumentSchema::instrument( $test_schema ); - $this->assertTrue( $instrumented_schema instanceof \WPGraphQL\WPSchema ); - } - -} \ No newline at end of file diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index c56a74b..6e0181e 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -7,6 +7,6 @@ return array( 'WPGraphQL\\Extensions\\Insights\\Data' => $baseDir . '/src/Data.php', - 'WPGraphQL\\Extensions\\Insights\\InstrumentSchema' => $baseDir . '/src/InstrumentSchema.php', + 'WPGraphQL\\Extensions\\Insights\\QueryTrace' => $baseDir . '/src/QueryTrace.php', 'WPGraphQL\\Extensions\\Insights\\Tracing' => $baseDir . '/src/Tracing.php', ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index c2a2dba..58e94ef 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -22,7 +22,7 @@ class ComposerStaticInit9e12acd0d8d678fe7190ac4fe7971f29 public static $classMap = array ( 'WPGraphQL\\Extensions\\Insights\\Data' => __DIR__ . '/../..' . '/src/Data.php', - 'WPGraphQL\\Extensions\\Insights\\InstrumentSchema' => __DIR__ . '/../..' . '/src/InstrumentSchema.php', + 'WPGraphQL\\Extensions\\Insights\\QueryTrace' => __DIR__ . '/../..' . '/src/QueryTrace.php', 'WPGraphQL\\Extensions\\Insights\\Tracing' => __DIR__ . '/../..' . '/src/Tracing.php', ); diff --git a/wp-graphql-insights.php b/wp-graphql-insights.php index 293bdc6..320f65f 100644 --- a/wp-graphql-insights.php +++ b/wp-graphql-insights.php @@ -7,7 +7,7 @@ * Author URI: https://www.wpgraphql.com * Text Domain: wp-graphql-insights * Domain Path: /languages - * Version: 0.0.2 + * Version: 0.2.0 * * @package WPGraphQL_Insights */ @@ -84,7 +84,7 @@ private static function setup_constants() { // Plugin version. if ( ! defined( 'WPGRAPHQL_INSIGHTS_VERSION' ) ) { - define( 'WPGRAPHQL_INSIGHTS_VERSION', '0.0.2' ); + define( 'WPGRAPHQL_INSIGHTS_VERSION', '0.2.0' ); } // Plugin Folder Path. @@ -147,12 +147,12 @@ private function actions() { /** * Initialize each resolver trace */ - add_action( 'graphql_before_resolve', [ 'WPGraphQL\Extensions\Insights\Tracing', 'init_field_resolver_trace' ], 10, 6 ); + add_action( 'graphql_before_resolve_field', [ 'WPGraphQL\Extensions\Insights\Tracing', 'init_field_resolver_trace' ], 10, 8 ); /** * Close each resolver trace */ - add_action( 'graphql_after_resolve', [ 'WPGraphQL\Extensions\Insights\Tracing', 'close_field_resolver_trace' ], 10, 6 ); + add_action( 'graphql_after_resolve_field', [ 'WPGraphQL\Extensions\Insights\Tracing', 'close_field_resolver_trace' ], 10, 8 ); } @@ -183,7 +183,7 @@ private function filters() { function graphql_insights_init() { /** - * If the version of WPGraphQL isn't up to date, don't instantiate tracing + * If the version of WPGraphQL isn't up to date, don't instantiate tracing as it won't work properly * @todo: consider displaying an Admin Notice or something to that tune if the versions aren't compatible */ if ( defined( 'WPGRAPHQL_VERSION' ) && version_compare( WPGRAPHQL_VERSION, '0.0.20', '<=' ) ) {