Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #15 from wp-graphql/feature/query-batch-support
Browse files Browse the repository at this point in the history
Initial query batch support
  • Loading branch information
jasonbahl authored Feb 6, 2018
2 parents 93b2226 + d1541d7 commit 5c68718
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
58 changes: 49 additions & 9 deletions src/Tracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -254,6 +253,8 @@ public static function get_trace() {
]
];



/**
* Filer and return the trace data
* @param array $trace An array of Trace data
Expand All @@ -269,7 +270,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 ) {

Expand All @@ -292,8 +293,27 @@ 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 ) {
/**
* 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( $key );
} else if ( is_array( $res ) ) {
$res['extensions']['tracing'] = self::get_trace( $key );
}
}
$response[ $key ] = $res;
}
} else {
if ( true === self::include_tracing_in_response( $response, $schema, $operation_name, $request, $variables ) ) {
$response->extensions['tracing'] = self::get_trace();
}
}

/**
Expand All @@ -315,9 +335,29 @@ 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 ) {
/**
* 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'] = QueryTrace::get_trace( $key );
} else if ( is_array( $res ) ) {
$res['extensions']['queryLog'] = QueryTrace::get_trace( $key );
}
}
$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;
}

Expand Down Expand Up @@ -357,7 +397,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();
}
Expand Down
2 changes: 1 addition & 1 deletion wp-graphql-insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

0 comments on commit 5c68718

Please sign in to comment.