Cấu hình plugin
Cấu hình pluginTạo Endpoint Nội Bộ Tùy Chỉnh cho Các Block

Tạo Endpoint Nội Bộ Tùy Chỉnh cho Các Block

Tương tự như endpoint nội bộ blockEditor, các nhà phát triển cũng có thể tạo các endpoint nội bộ được định sẵn của riêng mình (để cung cấp dữ liệu cho ứng dụng hoặc các block của họ), nhằm áp dụng một cấu hình cụ thể:

  • Sử dụng nested mutations hoặc không
  • Sử dụng namespacing hoặc không
  • Định sẵn các CPT có thể được truy vấn
  • Bất kỳ cấu hình nào khác có trong Schema Configuration

Đoạn mã PHP sau đây định nghĩa một endpoint nội bộ tùy chỉnh với tên accessMyPortfolioData, cấu hình trường Root.customPosts (từ module "Custom Posts") để chỉ truy cập 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 [];
  }
}

Nó phải được khởi tạo trên hook plugins_loaded:

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

Cuối cùng, endpoint được truy cập bằng cách thay thế tham số endpoint_group bằng tên đã chọn:

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