Das Plugin konfigurieren
Das Plugin konfigurierenBenutzerdefinierte interne Endpunkte für Blöcke erstellen

Benutzerdefinierte interne Endpunkte für Blöcke erstellen

Ähnlich wie beim internen Endpunkt blockEditor können Entwickler auch eigene vordefinierte interne Endpunkte erstellen (um ihre Anwendung oder Blöcke mit Daten zu versorgen) und dabei eine spezifische Konfiguration anwenden:

  • Verschachtelte Mutations verwenden oder nicht
  • Namespacing verwenden oder nicht
  • CPTs vordefinieren, die abgefragt werden können
  • Jede andere Konfiguration, die in der Schema-Konfiguration verfügbar ist

Der folgende PHP-Code definiert einen benutzerdefinierten internen Endpunkt mit dem Namen accessMyPortfolioData, der das Feld Root.customPosts (aus dem Modul „Custom Posts") so konfiguriert, dass nur auf den CPT MyPortfolio zugegriffen wird:

<?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 [];
  }
}

Er muss auf dem Hook plugins_loaded initialisiert werden:

add_action('plugins_loaded', function () {
  // Validate Gato GraphQL is installed, or exit
  if (!class_exists(\GatoGraphQL\GatoGraphQL\Plugin::class)) {
    return;
  }
 
  new MyPortfolioCustomAdminEndpointHook();
});

Schließlich wird auf den Endpunkt zugegriffen, indem der Parameter endpoint_group durch den gewählten Namen ersetzt wird:

https://yoursite.com/wp-admin/edit.php?page=gatographql&action=run_query&endpoint_group=accessMyPortfolioData