Skip to content

AxeWP/wp-graphql-plugin-boilerplate

Logo

WPGraphQL Plugin Boilerplate

🚨 NOTE: This is prerelease software. Use at your own risk 🚨

A boilerplate for creating WPGraphQL extensions. Can be used as a Composer dependency or as a tool to scaffold your own plugin.

Inspired by the following projects and their contributors:

Features

System Requirements

  • PHP 7.4+ | 8.0+ | 8.1+
  • WordPress 5.4.1+
  • WPGraphQL 1.8.0+

Getting Started

As a Composer dependency

We recommend installing this boilerplate using Strauss, to prevent plugin conflicts with other libraries. For more information see this explainer from StellarWP.

1. Add the dependency to your project.

composer require axewp/wp-graphql-plugin-boilerplate

2. Configure Strauss.

  1. Add the following scripts to composer .json:
"scripts": {
  "strauss": [
    "test -f ./bin/strauss.phar || curl -o bin/strauss.phar -L -C - https://github.com/BrianHenryIE/strauss/releases/download/0.14.0/strauss.phar",
    "@php bin/strauss.phar"
  ],
  "post-install-cmd": [
    "@strauss"
  ],
  "post-update-cmd": [
    "@strauss"
  ]
}
  1. Add the strauss config to "extra" in composer.json:
"extra": {
  "strauss": {
    "target_directory": "vendor-prefixed",
    "namespace_prefix": "WPGraphQL\\PluginName\\Vendor\\",
    "classmap_prefix": "WPGraphQL_PluginName",
    "constant_prefix": "GRAPHQL_PLUGINNAME",
    "include_modified_date": false,
    "delete_vendor_files": true,
    "packages": [
      "axepress/wp-graphql-plugin-boilerplate"
    ],
    "excluded_from_prefix": {
      "file_patterns": []
    }
  }
},
  1. Include the autoloader in your composer.json's classmap.
"autoload": {
  "files": [
    "access-functions.php"
  ],
  "psr-4": {
    "WPGraphQL\\PluginName\\": "src/"
  },
+  "classmap": [
+    "vendor-prefixed/"
+  ]
},

As a plugin starter

1. Initialize the plugin

Creating your WPGraphQL plugin is as simple as downloading the project to your machine and running curl -fsSL https://raw.github.com/AxeWP/wp-graphql-plugin-boilerplate/master/bin/install.sh | bash.

You will be asked to provide the following configuration details, or you can pass them as flags.

  • Branch (--branch) : The Github branch to use as the source.
  • Name (--name) : The name of your plugin (e.g. My Plugin for WPGraphQL).
  • Namespace (--namespace): The PHP namespace to be used for the plugin (e.g. MyPlugin).
  • Path (--path): The path to the directory directory where the plugin should be created (e.g. mysite/wp-content/plugins).
  • Prefix (--prefix): The plugin prefix (in snake case). This will be used to generate unique functions, hooks and constants (e.g. my_plugin).
  • Slug (--slug): The slug (in kebab-case) to use for the plugin (e.g. wp-graphql-my-plugin).

Alternatively, you can download the repository and run composer create-plugin.

2. Create your .env file

Rename .env.dist to .env, and set the variables to your particular localhost/testing environment.

Project Structure
wp-graphql-plugin-name                # This will be renamed by `create-plugin` to the provided slug.
β”œβ”€β”€ .github/workflows
β”‚   β”œβ”€β”€ code-quality.yml              # Github workflow for PHPStan.
β”‚   β”œβ”€β”€ code-standard.yml             # Github workflow for PHPCS
β”‚   β”œβ”€β”€ integration-testing.yml       # Github workflow for Codeception tests and Coveralls.
β”‚   β”œβ”€β”€ schema-linter.yml             # Github workflow GraphQL schema linting.
β”‚   └── upload-schema-artifact.yml    # Generates a schema artifact on Github release, for use by schema-linter.yml
β”œβ”€β”€ .wordpress.org                    # Assets for use in WordPress's plugin directory.
β”œβ”€β”€ bin
β”‚   β”œβ”€β”€ _env.sh                       # The shared env variables used by other shell scripts.
β”‚   β”œβ”€β”€ _lib.sh                       # Shared functions used by other shell scripts
β”‚   β”œβ”€β”€ install-stan-env.sh           # Creates a development environment for running PHPStan.
β”‚   β”œβ”€β”€ install-test-env.sh           # Creates a development environment for running Codeception.
β”‚   β”œβ”€β”€ run-docker.sh                 # Builds and runs the Docker image.
β”‚   └── wp-cli.yml                    # WPCLI configuration used for scripts.
β”œβ”€β”€ docker
β”‚   β”œβ”€β”€ app.Dockerfile
β”‚   β”œβ”€β”€ app.entrypoint.sh
β”‚   β”œβ”€β”€ app.post-setup.sh
β”‚   β”œβ”€β”€ app.setup.sh
β”‚   β”œβ”€β”€ testing.Dockerfile
β”‚   └── testing.entrypoint.sh
β”œβ”€β”€ phpstan
β”‚   └── constants.php                 # Stubbed plugin constants for PHPStan.
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ Admin                         # Classes for modifying the WP dashboard.
β”‚   β”‚   └── Settings
β”‚   β”‚       └── Settings.php          # Adds custom settings to WPGraphQL's settings page.
β”‚   β”œβ”€β”€ Connection                    # GraphQL connections.
β”‚   β”œβ”€β”€ Data
β”‚   β”œβ”€β”€ Fields                        # Individual GraphQL fields.
β”‚   β”œβ”€β”€ Model                         # GraphQL object data modelers.
β”‚   β”œβ”€β”€ Mutation                      # GraphQL mutations
β”‚   β”œβ”€β”€ Type                          # GraphQL types.
β”‚   β”‚   β”œβ”€β”€ Enum                      # Enum types.
β”‚   β”‚   β”œβ”€β”€ Input                     # Input types.
β”‚   β”‚   β”œβ”€β”€ Union                     # Union types.
β”‚   β”‚   β”œβ”€β”€ WPInterface               # Interface types.
β”‚   β”‚   └── WPObject                  # Object types.
β”‚   β”œβ”€β”€ Utils                         # Helper functions used across the plugin
β”‚   β”œβ”€β”€ CoreSchemaFilters.php         # Entrypoint for modifying the default schema provided by WPGraphQL
β”‚   β”œβ”€β”€ Main.php                      # Bootstraps the plugin
β”‚   └── TypeRegistry.php              # Entrypoint for registering GraphQL types to the schema
β”œβ”€β”€ tests                             # Codeception tests
β”‚   β”œβ”€β”€ _data
β”‚   β”œβ”€β”€ _envs
β”‚   β”œβ”€β”€ _output
β”‚   β”œβ”€β”€ _support
β”‚   β”œβ”€β”€ acceptance
β”‚   β”œβ”€β”€ unit
β”‚   β”œβ”€β”€ wpunit
β”‚   β”œβ”€β”€ acceptance.suite.dist.yml
β”‚   β”œβ”€β”€ bootstrap.php
β”‚   β”œβ”€β”€ unit.suite.dist.yml
β”‚   └── wpunit.suite.dist.yml
β”œβ”€β”€ vendor                            # Composer dependencies
β”‚  └── composer/autoload.php          # Composer autoloader
β”œβ”€β”€ vendor-prefixed                   # Namespaced dependencies, including the wrapped AxeWP classes from this package.
β”œβ”€β”€ .distignore
β”œβ”€β”€ .env.dist
β”œβ”€β”€ .gitattributes
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .phpcs.xml.dist
β”œβ”€β”€ access-functions.php              # Globally-available functions for accessing class methods.
β”œβ”€β”€ activation.php                    # Methods that run on plugin activation.
β”œβ”€β”€ codeception.dist.yml
β”œβ”€β”€ composer.json
β”œβ”€β”€ deactivation.php                  # Methods that run on plugin deactivation.
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ LICENSE
β”œβ”€β”€ phpstan.neon.dist
β”œβ”€β”€ phpunit.xml.dist
β”œβ”€β”€ README.md                         # The repo readme file.
β”œβ”€β”€ readme.txt                        # The plugin readme file.
└── wp-graphql-plugin-name.php

Roadmap

  • Include example files.
  • Quality-of-life utils that make it easy to extend WPGraphQL.
  • Extensive documentation.

Documentation

@todo

Recipes

@todo

About

Easily scaffold your WPGraphQL plugins

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published