De plugin configurerenAangepaste interne endpoints maken voor blokken
Aangepaste interne endpoints maken voor blokken
Net als het blockEditor interne endpoint, kunnen ontwikkelaars ook hun eigen vooraf gedefinieerde interne endpoints maken (om gegevens aan hun applicatie of blokken te leveren), om een specifieke configuratie toe te passen:
- Geneste mutaties gebruiken of niet
- Naamgeving (namespacing) gebruiken of niet
- Vooraf CPT's definiëren die opgevraagd kunnen worden
- Elke andere configuratie die beschikbaar is in de Schema-configuratie
De volgende PHP-code definieert een aangepast intern endpoint met de naam accessMyPortfolioData, dat het veld Root.customPosts (uit de module "Custom Posts") configureert om uitsluitend toegang te krijgen tot het CPT MyPortfolio:
<?php
declare(strict_types=1);
use GatoGraphQL\GatoGraphQL\PluginSkeleton\ExtensionHooks\AbstractAddCustomAdminEndpointHook;
use PoP\Root\Module\ModuleInterface;
use PoPCMSSchema\CustomPosts\Environment as CustomPostsEnvironment;
use PoPCMSSchema\CustomPosts\Module as CustomPostsModule;
class MyPortfolioCustomAdminEndpointHook extends AbstractAddCustomAdminEndpointHook
{
protected function getAdminEndpointGroup(): string
{
return 'accessMyPortfolioData';
}
/**
* Allow querying a specific CPT
*
* @param array<class-string<ModuleInterface>,array<string,mixed>> $moduleClassConfiguration [key]: Module class, [value]: Configuration
* @return array<class-string<ModuleInterface>,array<string,mixed>> [key]: Module class, [value]: Configuration
*/
protected function doGetPredefinedAdminEndpointModuleClassConfiguration(
array $moduleClassConfiguration,
): array {
$moduleClassConfiguration[CustomPostsModule::class][CustomPostsEnvironment::QUERYABLE_CUSTOMPOST_TYPES] = ['MyPortfolio'];
return $moduleClassConfiguration;
}
/**
* Do not disable any schema modules
*
* @param array<class-string<ModuleInterface>> $schemaModuleClassesToSkip List of `Module` class which must not initialize their Schema services
* @return array<class-string<ModuleInterface>> List of `Module` class which must not initialize their Schema services
*/
protected function doGetSchemaModuleClassesToSkip(
array $schemaModuleClassesToSkip,
): array {
return [];
}
}Het moet worden geïnitialiseerd op de plugins_loaded hook:
add_action('plugins_loaded', function () {
// Validate Gato GraphQL is installed, or exit
if (!class_exists(\GatoGraphQL\GatoGraphQL\Plugin::class)) {
return;
}
new MyPortfolioCustomAdminEndpointHook();
});Ten slotte wordt het endpoint benaderd door de parameter endpoint_group te vervangen door de gekozen naam:
https://yoursite.com/wp-admin/edit.php?page=gatographql&action=run_query&endpoint_group=accessMyPortfolioData