WPGraphQL Federations
A Wordpress plugin to add the federations feature to wp-graphql
by Manuel Antunes · github.com/manuel-antunes/wp-graphql-federations
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/manuel-antunes/wp-graphql-federations/archive/refs/heads/main.zipWPGraphQL Federations is a WordPress plugin that adds Apollo Federation v2 support to WPGraphQL. It allows your WordPress site to function as a subgraph in a federated GraphQL architecture, enabling seamless integration with other microservices.
🚀 The Problem
In modern microservices architectures, teams often use GraphQL Federation to combine multiple GraphQL services (subgraphs) into a single, unified gateway (supergraph).
While WordPress is a powerful CMS and WPGraphQL provides an excellent API, it does not support Apollo Federation out of the box. Specifically:
- It lacks the required
_serviceand_entitiesroot fields. - It doesn't support Federation directives like
@key,@shareable, or@external. - It cannot resolve entities based on their representation objects (e.g., resolving a Post by its ID from another service).
This makes it difficult to include WordPress in a larger federated ecosystem without complex custom code or brittle middleware.
✨ The Solution
WPGraphQL Federations bridges this gap by transforming WPGraphQL into a fully compliant Apollo Federation subgraph. It provides:
- Automatic Entity Discovery: Detects your Post Types, Taxonomies, Users, and Comments and makes them available as federated entities.
- Runtime Registry: A management interface to configure Federation v2 directives (
@key,@shareable, etc.) for any GraphQL type or field. - Entity Resolution: Automatically handles the
_entitiesquery, allowing other services to "join" data with WordPress objects using global IDs or custom keys. - Schema Augmentation: Injects the necessary Federation scalars and types into the schema and provides the full SDL with directives via the
_servicefield.
🛠 Features
- Full Federation v2 Support: Implements the latest Apollo Federation specification.
- Admin Settings Page: A user-friendly interface in the WordPress dashboard to manage federation settings.
- Type-Level Directives: Configure
@key,@shareable, and@inaccessiblefor any Object Type. - Field-Level Directives: Apply
@external,@requires,@provides,@override, and@tagto specific fields. - Flexible Keys: Support for both standard Relay Global IDs and internal database IDs as entity keys.
- Custom Directives: Support for adding additional custom directives to types.
📦 Installation
- Upload the
wp-graphql-federationsfolder to the/wp-content/plugins/directory. - Activate the plugin through the 'Plugins' menu in WordPress.
- Ensure WPGraphQL is also installed and active.
- Run
composer installin the plugin directory to ensure the autoloader is generated (if using the composer version).
⚙️ Configuration
Once activated, go to Settings > WPGraphQL Federation in your WordPress admin.
- Enable Types: Select the GraphQL types (Post Types, Users, etc.) you want to expose as entities.
- Define Keys: Specify the
@keyfields (usuallyid) that the gateway will use to identify these entities. - Field Directives: Expand the "Manage Fields" section for any type to apply field-specific federation logic.
- Save Changes: The plugin will automatically update the schema and SDL.
🔍 Technical Implementation
Entity Resolution
The plugin implements a robust resolve_entity method that understands how to fetch WordPress objects based on the __typename and key provided by the gateway. It supports:
- Post Types: Posts, Pages, and any custom post types.
- Taxonomies: Categories, Tags, and custom taxonomies.
- Core Entities: Users and Comments.
SDL Generation
The plugin hooks into graphql_schema_config and graphql_register_types to inject:
scalar _Anyandscalar _FieldSettype _Service { sdl: String }union _Entity(containing all your enabled federated types)_serviceand_entitiesroot queries.
📋 Requirements
- PHP: 7.4 or higher
- WordPress: 5.0 or higher
- WPGraphQL: 1.0 or higher
📄 License
This project is licensed under the MIT License.
Created by Manuel Antunes