|
| 1 | +--- |
| 2 | +title: "WPGraphQL Logging with Next.js Pages Router" |
| 3 | +description: "This demonstrates the usage of WPGraphQL Logging with Next.js Pages Router and WPGraphQL. It shows how GraphQL queries are logged and can be viewed, filtered, and exported from the WordPress admin." |
| 4 | +--- |
| 5 | + |
| 6 | +# Example: WPGraphQL Logging with Next.js |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +This example shows the WPGraphQL Logging plugin in action. The example uses a simple Next.js application that makes various GraphQL queries to WordPress, and demonstrates how those queries are logged and can be monitored through the WordPress admin interface. |
| 11 | + |
| 12 | +The example includes a wp-env setup, which will allow you to build and start this example quickly. With this wp-env setup, you don't need to have a separate WordPress instance or demo data to inspect the example. |
| 13 | + |
| 14 | +## What does this example do |
| 15 | + |
| 16 | +1. Fetch and display a list of posts from WordPress using GraphQL |
| 17 | +2. Fetch and display individual posts and pages using GraphQL with variables |
| 18 | +3. Automatically log all GraphQL queries to the WordPress database |
| 19 | +4. View logged queries in the WordPress admin (GraphQL Logs → All Logs) |
| 20 | +5. Filter logs by date range and log level |
| 21 | +6. Export logs to CSV format |
| 22 | +7. Configured WordPress instance with demo data and required plugins, using wp-env |
| 23 | + |
| 24 | +## Screenshots |
| 25 | + |
| 26 | +|| | | | |
| 27 | +|| :--------------------------------------------------------------------------: | :-----------------------------------------------------------------------------: | :--------------------------------------------------------------------------: | |
| 28 | +||  <br> View all logs |  <br> Individual log details |  <br> Filter and export logs | |
| 29 | + |
| 30 | +## Project Structure |
| 31 | + |
| 32 | +``` |
| 33 | +├── example-app |
| 34 | +│ └── src |
| 35 | +│ ├── components # Reusable React components |
| 36 | +│ ├── lib |
| 37 | +│ │ └── client.js # Apollo client instance |
| 38 | +│ ├── pages |
| 39 | +│ │ ├── index.js # Home page - list of posts |
| 40 | +│ │ ├── posts.js # Posts list with pagination |
| 41 | +│ │ └── [slug].js # Dynamic route for single post/page |
| 42 | +│ └── styles |
| 43 | +│ └── globals.css # Global styles |
| 44 | +├── .wp-env.json # wp-env configuration file |
| 45 | +└── wp-env |
| 46 | + ├── db |
| 47 | + │ └── database.sql # WordPress database including all demo data for the example |
| 48 | + └── uploads.zip # WordPress content to be used by wp-env |
| 49 | +``` |
| 50 | + |
| 51 | +## Running the example with wp-env |
| 52 | + |
| 53 | +### Prerequisites |
| 54 | + |
| 55 | +- Node.js (v18+ recommended) |
| 56 | +- [Docker](https://www.docker.com/) (if you plan on running the example see details below) |
| 57 | + |
| 58 | +**Note** Please make sure you have all prerequisites installed as mentioned above and Docker running (`docker ps`) |
| 59 | + |
| 60 | +### 1. Setup Repository and Packages |
| 61 | + |
| 62 | +- Clone the repo `git clone https://github.com/wpengine/hwptoolkit.git` |
| 63 | +- Install packages `cd hwptoolkit && npm install` |
| 64 | + |
| 65 | +### 2. Build and start the application |
| 66 | + |
| 67 | +- `cd plugins/wpgraphql-logging/examples/wpgraphql-logging-nextjs` |
| 68 | +- Then run `npm run example:build` will build and start your application. |
| 69 | + |
| 70 | +This starts the wp-env instance and frontend application. |
| 71 | + |
| 72 | +> [!IMPORTANT] |
| 73 | +> After the wp-env instance starts, ensure that all installed plugins are activated for this example to work properly. |
| 74 | +
|
| 75 | +| Frontend | Admin | |
| 76 | +| ---------------------- | ------------------------------- | |
| 77 | +| http://localhost:3000/ | http://localhost:8888/wp-admin/ | |
| 78 | + |
| 79 | +> **Note:** The login details for the admin is username "admin" and password "password" |
| 80 | +
|
| 81 | +### 3. Create environment variables for Next.js |
| 82 | + |
| 83 | +Create a `.env.local` file in the `example-app` directory with the following content: |
| 84 | + |
| 85 | +```bash |
| 86 | +NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8888 |
| 87 | +``` |
| 88 | + |
| 89 | +This tells the Next.js application where to find your WordPress GraphQL endpoint. |
| 90 | + |
| 91 | +### 4. Explore the logged queries |
| 92 | + |
| 93 | +1. Navigate to the Next.js frontend at http://localhost:3000/ |
| 94 | +2. Browse through the posts by clicking on them |
| 95 | +3. Visit http://localhost:3000/posts to see a different query |
| 96 | +4. Login to WordPress admin at http://localhost:8888/wp-admin/ |
| 97 | +5. Go to **GraphQL Logs → All Logs** to see all logged queries |
| 98 | +6. Click on any log entry to see detailed information including: |
| 99 | + - Query text and variables |
| 100 | + - Response data |
| 101 | + - Execution time and memory usage |
| 102 | + - Request headers and context |
| 103 | +7. Use the filters to narrow down logs by date range or log level |
| 104 | +8. Export logs to CSV for offline analysis |
| 105 | + |
| 106 | +### 5. Configure logging settings (Optional) |
| 107 | + |
| 108 | +Navigate to **GraphQL Logs → Settings** in the WordPress admin to configure: |
| 109 | + |
| 110 | +- Enable/disable logging |
| 111 | +- Adjust data sampling rate (default is 100% for this example) |
| 112 | +- Configure data retention period |
| 113 | +- Set up data sanitization rules |
| 114 | +- Exclude specific queries from logging |
| 115 | + |
| 116 | +If you want to learn more about the logging plugin, check out [the documentation](../../../docs/plugins/wpgraphql-logging/index.md). |
| 117 | + |
| 118 | +### Command Reference |
| 119 | + |
| 120 | +| Command | Description | |
| 121 | +| --------------------- | ----------------------------------------------------------------------------------------------------------------------- | |
| 122 | +| `example:build` | Prepares the environment by unzipping images, starting WordPress, importing the database, and starting the application. | |
| 123 | +| `example:dev` | Runs the Next.js development server. | |
| 124 | +| `example:dev:install` | Installs the required Next.js packages. | |
| 125 | +| `example:start` | Starts WordPress and the Next.js development server. | |
| 126 | +| `example:stop` | Stops the WordPress environment. | |
| 127 | +| `example:prune` | Rebuilds and restarts the application by destroying and recreating the WordPress environment. | |
| 128 | +| `wp:start` | Starts the WordPress environment. | |
| 129 | +| `wp:stop` | Stops the WordPress environment. | |
| 130 | +| `wp:destroy` | Completely removes the WordPress environment. | |
| 131 | +| `wp:db:query` | Executes a database query within the WordPress environment. | |
| 132 | +| `wp:db:export` | Exports the WordPress database to `wp-env/db/database.sql`. | |
| 133 | +| `wp:db:import` | Imports the WordPress database from `wp-env/db/database.sql`. | |
| 134 | +| `wp:images:unzip` | Extracts the WordPress uploads directory. | |
| 135 | +| `wp:images:zip` | Compresses the WordPress uploads directory. | |
| 136 | + |
| 137 | +> **Note** You can run `npm run wp-env` and use any other wp-env command. You can also see <https://www.npmjs.com/package/@wordpress/env> for more details on how to use or configure `wp-env`. |
| 138 | +
|
| 139 | +### Database access |
| 140 | + |
| 141 | +If you need database access add the following to your `.wp-env.json`: `"phpmyadminPort": 11111,` (where port 11111 is not allocated). |
| 142 | + |
| 143 | +You can check if a port is free by running `lsof -i :11111` |
0 commit comments