hsimah-services / wp-graphql-facetwp
FacetWP的WPGraphQL集成
Requires
- php: >=7.4
- axepress/wp-graphql-plugin-boilerplate: ^0.1.0
Requires (Dev)
- axepress/wp-graphql-cs: ^2.0.0-beta
- axepress/wp-graphql-stubs: ^1.13
- codeception/lib-innerbrowser: ^1.0
- codeception/module-asserts: ^1.0
- codeception/module-cli: ^1.0
- codeception/module-db: ^1.0
- codeception/module-filesystem: ^1.0
- codeception/module-phpbrowser: ^1.0
- codeception/module-rest: ^1.0
- codeception/module-webdriver: ^1.0
- codeception/phpunit-wrapper: ^9.0
- codeception/util-universalframework: ^1.0
- lucatume/wp-browser: <3.5
- php-coveralls/php-coveralls: ^2.5
- phpcompatibility/php-compatibility: dev-develop as 9.99.99
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.8
- szepeviktor/phpstan-wordpress: ^1.1
- wp-cli/wp-cli-bundle: ^2.8.1
- wp-graphql/wp-graphql-testcase: ~2.3
README
FacetWP的WPGraphQL
为FacetWP添加WPGraphQL支持。
概述
此插件通过图形模式公开配置好的分面。一旦为类型注册,就可用查询。有效负载包括分面选项和信息以及与帖子类型数据的连接。这允许对返回的数据集进行标准GraphQL分页。
此插件已与SearchWP进行测试并功能正常。
系统要求
- PHP 7.4-8.1.x
- WordPress 5.4.1+
- WPGraphQL 1.6.0+ (推荐1.9.0+)
- FacetWP 4.0+
快速安装
重要
请确保您从发行页面下载wp-graphql-facetwp.zip
文件,而不是源代码(zip)
文件或存储库的克隆。
如果您想使用源代码,您需要在插件文件夹内运行composer install
来安装所需的依赖项。
使用Composer
composer require hsimah-services/wp-graphql-facetwp
更新和版本控制
随着我们向1.0版本发布迈进,我们需要引入许多重大更改。我们将尽最大努力将多个重大更改组合在一个版本中,以便于开发者保持其项目的更新。
在我们达到v1.0之前,我们使用的是SemVer的修改版本,其中
- v0.x:"主"版本。这些版本引入了新功能,并且可能包含对PHP API或GraphQL模式的重大更改
- v0.x.y:"次"版本。这些版本引入了新功能和增强功能,并修复了错误。它们不包含重大更改。
- v0.x.y.z:"补丁"版本。这些版本仅用于解决前一个版本的问题。
开发和支持
FacetWP的WPGraphQL最初由Hamish Blake创建。维护和开发现在由AxePress Development提供。社区贡献是欢迎的和鼓励的。
基本支持是免费的,在此存储库中以及在WPGraphQL Slack的#facetwp
频道中。
提供优先支持和定制开发的对象是AxePress Development赞助者。
使用方法
将分面注册到WPGraphQL
假设分面已经配置。
要将FacetWP查询注册到WordPress文章类型(例如post
)的WPGraphQL模式中,只需调用以下函数
// Register facet for Posts add_action( 'graphql_facetwp_init', function () { register_graphql_facet_type( 'post' ); } );
这将创建一个在RootQuery
上的WPGraphQL postFacet
字段。负载包括查询的facets
集合和posts
连接。连接是一个标准的WPGraphQL连接,支持分页和服务器端排序。连接负载仅包括过滤后的文章。
示例查询
注意 这不是添加到模式中的所有GraphQL字段和类型的完整列表。请参阅WPGraphiQL IDE以获取更多查询及其文档。
query GetPostsByFacet( $query: FacetQueryArgs, $after: String, $search: String, $orderBy: [PostObjectsConnectionOrderbyInput] ) { postFacet( where: { status: PUBLISH, query: $query # The query arguments are determined by the Facet type. } ) { facets { # The facet configuration selected name label choices { value label count } } posts ( # The results of the facet query. Can be filtered by WPGraphQL connection where args first: 10, after: $after, where: { search: $search, orderby: $orderBy} # The `orderby` arg is ignored if using the Sort facet. ) { pageInfo { hasNextPage endCursor } nodes { title excerpt } } } }
WooCommerce支持
可以使用以下配置添加对WooCommerce产品的支持
// This is the same as all CPTs. add_action( 'graphql_facetwp_init', function () { register_graphql_facet_type( 'product' ); }); // This is required because WooGQL uses a custom connection resolver. add_filter( 'facetwp_graphql_facet_connection_config', function ( array $default_graphql_config, array $facet_config ) { $type = $config['type']; $use_graphql_pagination = \WPGraphQL\FacetWP\Registry\FacetRegistry::use_graphql_pagination(); return array_merge( $default_graphql_config, [ 'connectionArgs' => \WPGraphQL\WooCommerce\Connection\Products::get_connection_args(), 'resolveNode' => function ( $node, $_args, $context ) use ( $type ) { return $context->get_loader( $type )->load_deferred( $node->ID ); }, 'resolve' => function ( $source, $args, $context, $info ) use ( $type, $use_graphql_pagination ) { // If we're using FWP's offset pagination, we need to override the connection args. if ( ! $use_graphql_pagination ) { $args['first'] = $source['pager']['per_page']; } $resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $source, $args, $context, $info, $type ); // Override the connection results with the FWP results. if( ! empty( $source['results'] ) ) { $resolver->->set_query_arg( 'post__in', $source['results'] ); } // Use post__in when delegating sorting to FWP. if ( ! empty( $source['is_sort'] ) ) { $resolver->set_query_arg( 'orderby', 'post__in' ); } elseif( 'product' === $type ) { // If we're relying on WPGQL to sort, we need to to handle WooCommerce meta. $resolver = Products::set_ordering_query_args( $resolver, $args ); } return $resolver ->get_connection(); }, ] ); }, 100, 2 );
限制
目前该插件仅测试过使用复选框、单选按钮和排序facets类型。对其他类型的支持正在开发中。
测试
- 更新您的
.env
文件以符合测试环境规范。 - 运行
composer install-test-env
以创建测试环境。 - 使用Codeception运行您的测试套件。例如,
vendor/bin/codecept run wpunit
将运行所有WPUnit测试。