Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Troubles Namespacing WPGraphQL #66

Closed
tn3rb opened this issue Apr 26, 2023 · 5 comments
Closed

Troubles Namespacing WPGraphQL #66

tn3rb opened this issue Apr 26, 2023 · 5 comments

Comments

@tn3rb
Copy link

tn3rb commented Apr 26, 2023

Attempting to solve depedency conflicts with https://github.com/wp-graphql/wp-graphql

composer.json


"require": {
	"wp-graphql/wp-graphql": "^1.12"
},
"extra": {
	"strauss": {
		"target_directory": "Dependencies",
		"namespace_prefix": "MyProject\\Dependencies\\",
		"classmap_prefix": "Prefix_",
		"constant_prefix": "Prefix_",
		"packages": [
			"wp-graphql/wp-graphql"
		],
		"delete_vendor_packages": true,
		"delete_vendor_files": true
	}
},

results in a whole lot of weirdness with the WPGraphQL package ultimately not working.

The main WPGraphQL class is not namespaced, the top of the file DOES have the following commented out line added:

// Global. - namespace MyProject\Dependencies\WPGraphQL;

but the actual class declaration remains untouched:

final class WPGraphQL {

whereas the expectation was for the class to either be namespaced via the namespace keyword or via the classmap_prefix prefix specified in composer.json.

Either way, autoloading does not work and results in errors like:

[26-Apr-2023 23:08:28 UTC] 
PHP Fatal error:  Uncaught Error: 
Class "MyProject\Dependencies\WPGraphQL" not found 
in /path/to/project/Dependencies/wp-graphql/wp-graphql/src/Registry/Utils/PostObject.php:181

which appears to be expecting the WPGraphQL to be namespaced like MyProject\Dependencies\WPGraphQL

PLease note that the autoload file generated by straus is loaded during the very first few steps of my bootstrapping process:

require_once __DIR__ . '/Dependencies/autoload.php';

Anyways, not sure whether my problems are caused by a bug, or due to misconfiguration, or if there is something else i failed to do.

Thank you in advance for all of your time, effort, and talent you have commited to this project. I'll gladly help write unit tests if any fix is needed to get this working.

@BrianHenryIE
Copy link
Owner

Hey, I think I have this fixed: 49c444a

Try the master branch and let me know:
composer require brianhenryie/strauss:dev-master

@BrianHenryIE
Copy link
Owner

Reopening: the unit test passes but the integration test does not :(

@BrianHenryIE BrianHenryIE reopened this Apr 27, 2023
@BrianHenryIE
Copy link
Owner

OK, I made further changes. 168 passing tests now! Let me know if it works for you. I get:

final class Prefix_WPGraphQL {

BrianHenryIE added a commit that referenced this issue Apr 27, 2023
@tn3rb
Copy link
Author

tn3rb commented Apr 27, 2023

OH it's better...

the global scope class is now prefixed correctly:

final class EECD_WPGraphQL {

however, the prefix is not applied consistently throughout the rest of the codebase, so everything fatals.

For example, in /Dependencies/wp-graphql/wp-graphql/src/Registry/Utils/PostObject.php:

the import is incorrect

namespace MyProject\Dependencies\WPGraphQL\Registry\Utils;

use MyProject\Dependencies\WPGraphQL;

and in the class code, the following code on line 181 is wrong :

$allowed_taxonomies = WPGraphQL::get_allowed_taxonomies( 'objects' );

but then a little bit below that on line 203 it's correct :

$taxonomies = \EECD_WPGraphQL::get_allowed_taxonomies();

so weird, since the two lines of code are almost identical.

Looking at the source code repo, it appears that the inconsistency is in the code there:
https://github.com/wp-graphql/wp-graphql/blob/develop/src/Registry/Utils/PostObject.php

line 175:

$allowed_taxonomies = WPGraphQL::get_allowed_taxonomies( 'objects' );

line 197

$taxonomies = \WPGraphQL::get_allowed_taxonomies();

so it appears that Strauss is getting tripped up when the globally scoped class is imported.

Oh, and most importantly, THANK YOU so much for taking the time to work on this, it is so greatly appreaciated.

@BrianHenryIE
Copy link
Owner

I finally got this done. It's in master but not in a release yet: cbd1617

I replaced the use WPGraphQL; with use Prefix_WPGraphQL as WPGraphQL;, then by doing that before the namespace replacements, it wasn't inadvertently changed to use MyProject\Dependencies\WPGraphQL;. Inside the class, WPGraphQL and \Prefix_WPGraphQL are both valid.

Give it a try and let me know how it's working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants