การตั้งค่าปลั๊กอินการสร้าง Custom Internal Endpoints สำหรับ Blocks
การสร้าง Custom Internal Endpoints สำหรับ Blocks
คล้ายกับ blockEditor internal endpoint นักพัฒนายังสามารถสร้าง internal endpoints ที่กำหนดไว้ล่วงหน้าของตนเอง (เพื่อส่งข้อมูลให้แอปพลิเคชันหรือ blocks) และนำการกำหนดค่าเฉพาะมาใช้ได้:
- การใช้ nested mutations หรือไม่ก็ได้
- การใช้ namespacing หรือไม่ก็ได้
- การกำหนด CPTs ที่สามารถ query ได้ล่วงหน้า
- การกำหนดค่าอื่น ๆ ที่มีอยู่ใน Schema Configuration
โค้ด PHP ต่อไปนี้กำหนด custom internal endpoint ชื่อ accessMyPortfolioData ซึ่งกำหนดให้ฟิลด์ Root.customPosts (จากโมดูล "Custom Posts") เข้าถึงเฉพาะ 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 [];
}
}จะต้องทำการเริ่มต้นบน 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();
});สุดท้าย ให้เข้าถึง endpoint โดยแทนที่พารามิเตอร์ endpoint_group ด้วยชื่อที่เลือก:
https://yoursite.com/wp-admin/edit.php?page=gatographql&action=run_query&endpoint_group=accessMyPortfolioData