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

feat: support plugin registration for the IDE #61

Merged
merged 7 commits into from
Mar 4, 2024

Conversation

jasonbahl
Copy link
Collaborator

What this does:

This allows 3rd party plugins to "registerPlugin" to the WPGraphQL IDE.

ex:

help.php

<?php
/**
 * Plugin Name: GraphiQL Help Screen
 * Plugin Author: WPGraphQL
 * Description: Example of how third parties can register plugins that extend the WPGraphQL IDE
 */

namespace WPGraphQLIDE\Plugins\Help;

add_action( 'wpgraphqlide_enqueue_script', __NAMESPACE__ . '\\enqueue_plugin' );

function enqueue_plugin() {

	$asset_file = include WPGRAPHQL_IDE_PLUGIN_DIR_PATH . 'build/help.asset.php';

	wp_enqueue_script(
		'wpgraphql-ide-help-plugin',
		WPGRAPHQL_IDE_PLUGIN_URL . 'build/help.js',
		array_merge( $asset_file['dependencies']),
		$asset_file['version'],
		true
	);

}

help.js

const { registerPlugin } = WPGraphQLIDE;
import { Icon, help } from "@wordpress/icons";

registerPlugin( 'help', {
	title: 'Help',
	icon: () => <Icon icon={help} />,
	content: () => <><h2>This is a 3rd party plugin.</h2></>
})

Would output the following:

3rd-party-plugin-example


In fact, you can actually see this example plugin built-in to this PR:

see the ./plugins/help directory!


Summary of this PR:

  • import @wordpress/icons
  • introduce custom webpack config for different entries. This generates different asset.php files
  • separate index.js and app.jsx as different entry points. index sets up some re-usable functions that can be used by 3rd party extensions, app renders the app
  • define WPGRAPHQL_IDE_PLUGIN_DIR_PATH and WPGRAPHQL_IDE_PLUGIN_DIR_PATH for better re-usability of directory/path definitions
  • split the enqueing of the index.js and app.js with the wpgraphqlide_enqueue_script action in between. This allows for the initial "setup" of functions to be enqueued, then extensions to be enqueued, then the app to be enqueued
  • add /plugins/ directory with initial README.md
  • add initial plugins/help plugin to show how a 3rd party plugin could hook into the GraphiQL IDE
  • move the rendering of the App out of index.js into App.jsx
  • add "registerPlugin" function to index.js which dispatches to the store to register a plugin
  • update Editor.jsx to read "plugins" from the redux store and add them to
  • add action/selector/reducer for registering a plugin

- import @wordpress/icons
- introduce custom webpack config for different entries. This generates different asset.php files
- separate index.js and app.jsx as different entry points. index sets up some re-usable functions that can be used by 3rd party extensions, app renders the app
- define WPGRAPHQL_IDE_PLUGIN_DIR_PATH and WPGRAPHQL_IDE_PLUGIN_DIR_PATH for better re-usability of directory/path definitions
- split the enqueing of the index.js and app.js with the `wpgraphqlide_enqueue_script` action in between. This allows for the initial "setup" of functions to be enqueued, then extensions to be enqueued, then the app to be enqueued
- add `/plugins/` directory with initial README.md
- add initial plugins/help plugin to show how a 3rd party plugin could hook into the GraphiQL IDE
- move the rendering of the App out of index.js into App.jsx
- add "registerPlugin" function to index.js which dispatches to the store to register a plugin
- update Editor.jsx to read "plugins" from the redux store and add them to <GraphiQL />
- add action/selector/reducer for registering a plugin
@jasonbahl jasonbahl added enhancement New feature or request feature porting Porting features from the core WPGraphQL plugin labels Feb 29, 2024
@jasonbahl jasonbahl self-assigned this Feb 29, 2024
- ran composer fix-cs
- update help cards to contain all the cards from the current help screen on the GraphiQL in the current IDE
- add color variables for icon in help screen to match other icon styles
plugins/help/index.js Outdated Show resolved Hide resolved
…/index.js to do "import { registerPlugin } from "wpgraphql-ide";" and it not try to import it from npm

- add wpgraphql-ide to dependencies in wp_enqueue_scripts for the wpgraphql-ide-help-plugin
- separate help/index.js into separate components
- add css variables to the HelpCard component
- add README.md to the help plugin
- simplify the render of the <App />. . .turns out I was doing something stupid before when I thought I needed the addEventListener
- update som docblocks/code comments
- simplify some of the uses of useSelect
- update the onEditQuery method of the <GraphiQL> component to ensure the redux store stays in sync
- remove some console.logs
@jasonbahl
Copy link
Collaborator Author

@josephfusco I pushed up a new commit cleaning up some things, adding some inline comments, adding a README.md to the help plugin, etc.

Looking forward to any feedback you might have! 🙏🏻


module.exports = {
...defaults,
entry: {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've ejected into a custom wepback.config.js so that each script can be enqueued seperately.

This allows us to export functions in the index.js and enqueue them to be available in plugins and the app.

Since functions like registerPlugin and the redux store(s) need to be available before plugins and the App call those functions, and we're not importing them via an NPM build script, we need to make sure the enqueue order has the index.js script enqueued first (as a dependency of anything that needs those functions).

@@ -70,6 +87,17 @@ const selectors = {
isInitialStateLoaded: ( state ) => {
return state.isInitialStateLoaded;
},
getPluginsArray: (state) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that we have determined names for each of the IDE areas, possibly this could be renamed to something like getActivityBarPlugins instead of just plugins 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Feel free to make that change here if you feel inclined to, otherwise can make a separate issue to scrub the whole codebase for any other possible renames before moving on.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josephfusco let's follow-up on this.

I think we'll see some other patterns emerge re: naming things, file organization, etc as we start implementing the other "pluggable" areas

Comment on lines -235 to +240
'wpgraphql-ide-app',
'wpgraphql-ide',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean!

Copy link
Member

@josephfusco josephfusco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great work @jasonbahl, and thank you for the comments and included documentation!

We may want to add test coverage to check on the window object structure just to have that explicitly called out for posterity, but since the plugin relies on those things internally, I don't feel it's absolutely critical right now.

plugins/help/HelpPanel.js Outdated Show resolved Hide resolved
@josephfusco josephfusco self-requested a review March 4, 2024 16:54
@josephfusco josephfusco changed the title feat: P.O.C of a "plugin registry" feat: support plugin registration for the IDE Mar 4, 2024
@jasonbahl jasonbahl merged commit 591c1ea into main Mar 4, 2024
4 checks passed
@josephfusco josephfusco deleted the feat/plugin-registry branch May 17, 2024 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature porting Porting features from the core WPGraphQL plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants