Created
          February 3, 2022 16:20 
        
      - 
      
- 
        Save jasonbahl/264f40a1d90c61b4287cb0aef48f9c36 to your computer and use it in GitHub Desktop. 
    WPGraphQL Request Logger
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| /** | |
| * Plugin Name: WPGraphQL Request Logger | |
| * Description: This plugin logs WPGraphQL Requests to a Custom Post Type. This is a debugging tool. Not intended for production use. Also, not fully tested. Use at your own risk. | |
| * Plugin Author: Jason Bahl | |
| * Author URI: https://www.wpgraphql.com | |
| */ | |
| add_action( 'init', function() { | |
| register_post_type( 'graphql_request_logs', [ | |
| 'label' => __( 'GraphQL Request Logs', 'wp-graphql' ), | |
| 'public' => false, | |
| 'show_ui' => true, | |
| 'publicly_queryable' => false, | |
| 'show_in_menu' => false, | |
| 'menu_position' => null, | |
| 'can_export' => true, | |
| 'has_archive' => false, | |
| 'supports' => [ 'title', 'editor' ], | |
| 'show_in_graphql' => true, | |
| 'graphql_single_name' => 'GraphQLRequestLog', | |
| 'graphql_plural_name' => 'GraphQLRequestLogs', | |
| ] ); | |
| } ); | |
| add_action( 'graphql_register_types', function() { | |
| register_graphql_fields( 'GraphQLRequestLog', [ | |
| 'response' => [ 'type' => 'String', 'resolve' => function( $post ) { return get_post_meta( $post->databaseId, 'response', true ); }], | |
| 'result' => [ 'type' => 'String', 'resolve' => function( $post ) { return get_post_meta( $post->databaseId, 'result', true ); }], | |
| 'operationName' => [ 'type' => 'String', 'resolve' => function( $post ) { return get_post_meta( $post->databaseId, 'operation_name', true ); }], | |
| 'query' => [ 'type' => 'String', 'resolve' => function( $post ) { return get_post_meta( $post->databaseId, 'query', true ); }], | |
| 'variables' => [ 'type' => 'String', 'resolve' => function( $post ) { return get_post_meta( $post->databaseId, 'variables', true ); }], | |
| 'server' => [ 'type' => 'String', 'resolve' => function( $post ) { return get_post_meta( $post->databaseId, 'server', true ); }], | |
| ] ); | |
| }); | |
| add_action( 'admin_menu', function() { | |
| add_submenu_page( 'graphql', 'Request Logs', 'Request Logs', 'manage_options', 'edit.php?post_type=graphql_request_logs', NULL ); | |
| }, 99 ); | |
| add_action( 'graphql_process_http_request_response', function ( $response, $result, $operation_name, $query, $variables ) { | |
| global $_SERVER; | |
| $errors = null; | |
| if ( is_array( $response ) && isset( $response['errors'] ) ) { | |
| $errors = wp_json_encode( $response['errors'] ); | |
| } else if( isset( $response->errors ) ) { | |
| $errors = wp_json_encode( $response->errors ); | |
| } | |
| // return; | |
| wp_insert_post( [ | |
| 'post_type' => 'graphql_request_logs', | |
| 'post_status' => 'draft', | |
| 'post_title' => isset( $operation_name ) ? $operation_name : 'GraphQL Request', | |
| 'meta_input' => [ | |
| 'response' => wp_json_encode( $response ), | |
| 'result' => wp_json_encode( $result ), | |
| 'operation_name' => $operation_name, | |
| 'query' => wp_json_encode( wp_strip_all_tags( $query, true ) ), | |
| 'variables' => wp_json_encode( $variables ), | |
| 'server' => wp_json_encode( $_SERVER ), | |
| 'errors' => $errors, | |
| ] | |
| ] ); | |
| }, 10, 5 ); | |
| add_filter( 'manage_graphql_request_logs_posts_columns', 'filter_graphql_request_logs_columns' ); | |
| function filter_graphql_request_logs_columns( $columns ) { | |
| $columns['query'] = __( 'Query', 'wp-graphql-request-logs' ); | |
| $columns['errors'] = __( 'Errors', 'wp-graphql-request-logs' ); | |
| return $columns; | |
| } | |
| add_action( 'manage_graphql_request_logs_posts_custom_column', 'graphql_request_logs_column', 10, 2); | |
| function graphql_request_logs_column( $column, $post_id ) { | |
| // Image column | |
| if ( 'query' === $column ) { | |
| $query = get_post_meta( $post_id, 'query', true); | |
| echo "<pre style=\" | |
| width: 300px; | |
| overflow: scroll; | |
| height: 300px; | |
| white-space: break-spaces; | |
| \">" . $query . "</pre>"; | |
| } | |
| if ( 'errors' === $column ) { | |
| $errors = get_post_meta( $post_id, 'errors', true); | |
| echo "<pre style=\" | |
| width: 300px; | |
| overflow: scroll; | |
| height: 300px; | |
| white-space: break-spaces; | |
| \">" . $errors . "</pre>"; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
This is a quick plugin I created to help log WPGraphQL requests and debug when there were errors.
This plugin captures and stores GraphQL requests and responses in a Custom Post Type.