因此,在进一步挖掘和查看更多文档后,我想出了这个似乎可行的解决方案。我不确定这是否是;“有福的”;是的,但我认为它能在紧要关头完成工作。
注册我的Pods CPT:
add_filter( \'register_post_type_args\', function( $args, $post_type ) {
if ( \'item\' === $post_type ) {
$args[\'show_in_graphql\'] = true;
$args[\'graphql_single_name\'] = \'item\';
$args[\'graphql_plural_name\'] = \'items\';
}
return $args;
}, 10, 2 );
添加注册新对象类型的操作(
GalleryImage) 并且只返回
guid 通过调用
register_graphql_object_type. 然后我打电话
register_graphql_field 并希望返回
list_of galleryImage 对象。我不完全确定的部分是解析器函数获取
item pod并返回
gallery 领域这是可行的,也是有意义的,但我相信它是可以优化的。
add_action( \'graphql_register_types\', function() {
register_graphql_object_type(
\'GalleryImage\',
[
\'description\' => __( \'Gallery Image\' ),
\'fields\' => [
\'guid\' => [
\'type\' => \'String\',
\'description\' => \'image guid\'
]
],
]
);
register_graphql_field(
\'Item\',
\'galleryImages\',
[
\'description\' => __( \'Return images\' ),
\'type\' => [ \'list_of\' => \'galleryImage\' ],
\'resolve\' => function() {
$images = pods( \'item\', [ \'limit\' => -1 ])->field( \'gallery\' );
return $images;
}
]
);
} );
These docs 真的很有帮助。