headless-cms
headless-cms copied to clipboard
[Sort of solved] v2 Breaks my site using, WPGatsby, WPGraphQL, WPGraphQL-JWT-Authentication
Howdy,
Firstly, a big thanks to Imran for all of your awesome videos, code, and hard work. I've decided to leave this here in case others are having a similar issue.
So, this issue is a really weird one. Complicated by the fact it's being utilised alongside Gatsby and WPGraphQL config.
I updated to V2 from v1.2 (I think). I don't think the error happened until running a build. If gatsby was already running during the update then I suppose WPGatsby isn't polling the headless-cms plugin by default.
Anyhow, the WPGatsby fails to build, early in the process wpgatsby->ingesting-wp.
ERROR #gatsby-source-wordpress_111007
gatsby-source-wordpress Request failed with status code 500
Your WordPress server is either overloaded or encountered a PHP error.
Enable WordPress debugging by adding the following to your wp-config.php file:
define("WP_DEBUG", true);
define("WP_DEBUG_LOG", true);
define("GRAPHQL_DEBUG", true);
(See https://wordpress.org/support/article/debugging-in-wordpress/ for more info)
Then run another build before checking your WordPress instance's debug.log file for errors.
If you don't see any errors in debug.log:
Try reducing the requestConcurrency for content updates or the previewRequestConcurrency for previews, and/or reducing the schema.perPage option:
{
resolve: 'gatsby-source-wordpress',
options: {
schema: {
perPage: 20,
requestConcurrency: 5,
previewRequestConcurrency: 2,
}
},
}
The error given on the WP php is,
Uncaught Exception: You must define the GraphQL JWT Auth secret to use the WPGraphQL JWT Authentication plugin.
see below.
[02-Aug-2022 12:53:22 UTC] PHP Fatal error: Uncaught Exception: You must define the GraphQL JWT Auth secret to use the WPGraphQL JWT Authentication plugin. in /Users/*/Local Sites/mysite-tech-auth-2022-08-02/app/public/wp-content/plugins/wp-graphql-jwt-authentication-develop/wp-graphql-jwt-authentication.php:195
Stack trace:
#0 /Users/*/Local Sites/mysite-tech-auth-2022-08-02/app/public/wp-includes/class-wp-hook.php(307): WPGraphQL\JWT_Authentication\JWT_Authentication::WPGraphQL\JWT_Authentication\{closure}('')
#1 /Users/*/Local Sites/mysite-tech-auth-2022-08-02/app/public/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array)
#2 /Users/*/Local Sites/mysite-tech-auth-2022-08-02/app/public/wp-includes/plugin.php(476): WP_Hook->do_action(Array)
#3 /Users/*/Local Sites/mysite-tech-auth-2022-08-02/app/public/wp-content/plugins/wp-graphql/src/Request.php(139): do_action('init_graphql_re...')
#4 /Users/*/Local Sites/mysite-tech-auth-2022-08-02/app/public/wp-content/plug in /Users/*/Local Sites/mysite-tech-auth-2022-08-02/app/public/wp-content/plugins/wp-graphql-jwt-authentication-develop/wp-graphql-jwt-authentication.php on line 195
I think this is invoked @ wp-graphql-jwt-authentication/wp-graphql-jwt-authentication.php
/**
* When the GraphQL Request is initiated, validate the token.
*
* If the Auth Token is not valid, prevent execution of resolvers. This will also set the
* response status to 403.
*/
add_action( 'init_graphql_request', function() {
=================== circa ln195
=================== circa ln195
$jwt_secret = Auth::get_secret_key();
if ( empty( $jwt_secret ) || 'graphql-jwt-auth' === $jwt_secret ) {
throw new \Exception( __( 'You must define the GraphQL JWT Auth secret to use the WPGraphQL JWT Authentication plugin.', 'graphql-jwt-auth' ) );
} else {
$token = Auth::validate_token();
if ( is_wp_error( $token ) ) {
add_action( 'graphql_before_resolve_field', function() use ( $token ) {
throw new \Exception( $token->get_error_code() . ' | ' . $token->get_error_message() );
}, 1 );
}
}
I've defined the global 'GRAPHQL_JWT_AUTH_SECRET_KEY' in my wp-config.php
define( 'GRAPHQL_JWT_AUTH_SECRET_KEY', 'generated@https://api.wordpress.org/secret-key/1.1/salt/' );
Right, so I've kind of figured it out.
In the WP admin area, at the Frontend Site URL, this input was set to be the live domain, not the local one. I switched it to "https://mysite.local" and the error vanished.
Frontend Site Details Section
- Example for local: http://localhost:3000
- Example for live: https://example.com
Frontend Site URL: mysite.local
Interestingly, if I switch the frontend site url input back to the live domain "https://mysite.com" then it still works in local development and the error doesn't persist.
Strange but intriguing.
I suppose at some stage headless-cms must overrule or invalidate the global, GRAPHQL_JWT_AUTH_SECRET_KEY that is set on the wp-config. And then (hopefully) once it's been reset in the WP admin it then exists again.