hsimah-services/wp-graphql-facetwp

FacetWP的WPGraphQL集成

资助包维护!
AxeWP

安装次数: 70

依赖项: 0

建议者: 0

安全: 0

星标: 34

关注者: 4

分支: 3

开放问题: 3

类型:wordpress-plugin

0.5.0 2024-04-06 17:49 UTC

This package is auto-updated.

Last update: 2024-09-16 13:19:29 UTC


README

Logo

FacetWP的WPGraphQL

FacetWP添加WPGraphQL支持。

Packagist License Packagist Version GitHub commits since latest release (by SemVer) GitHub forks GitHub Repo stars
CodeQuality GitHub Workflow Status Coding Standards

概述

此插件通过图形模式公开配置好的分面。一旦为类型注册,就可用查询。有效负载包括分面选项和信息以及与帖子类型数据的连接。这允许对返回的数据集进行标准GraphQL分页。

此插件已与SearchWP进行测试并功能正常。

系统要求

  • PHP 7.4-8.1.x
  • WordPress 5.4.1+
  • WPGraphQL 1.6.0+ (推荐1.9.0+)
  • FacetWP 4.0+

快速安装

  1. 安装并激活WPGraphQL
  2. 安装并激活FacetWP
  3. 最新版本下载wp-graphql-facetwp.zip文件,上传到您的WordPress安装中,并激活插件。

重要

请确保您从发行页面下载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文档可在此处找到。

将分面注册到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类型。对其他类型的支持正在开发中。

测试

  1. 更新您的.env文件以符合测试环境规范。
  2. 运行composer install-test-env以创建测试环境。
  3. 使用Codeception运行您的测试套件。例如,vendor/bin/codecept run wpunit将运行所有WPUnit测试。