Skip to content

Commit

Permalink
Merge pull request #511 from kidunot89/bugfix/customer-connection-ref…
Browse files Browse the repository at this point in the history
…actored

"Customer_Connection_Resolver" deprecated.
  • Loading branch information
kidunot89 committed Jun 14, 2021
2 parents a98998d + f97664c commit 121a651
Show file tree
Hide file tree
Showing 16 changed files with 903 additions and 829 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -36,7 +36,7 @@ RUN curl -sS https://getcomposer.org/installer | php -- \
ENV PATH "$PATH:~/.composer/vendor/bin"

# Set PHPUnit version.
ARG PHPUNIT_VERSION="<=8.1.*"
ARG PHPUNIT_VERSION="<=8.1"
# Install wp-browser globally
RUN composer global require --optimize-autoloader \
wp-cli/wp-cli-bundle \
Expand Down
15 changes: 15 additions & 0 deletions includes/class-core-schema-filters.php
Expand Up @@ -73,6 +73,14 @@ public static function add_filters() {
10,
3
);

add_filter(
'graphql_dataloader_get_model',
array( '\WPGraphQL\WooCommerce\Data\Loader\WC_Customer_Loader', 'inject_user_loader_models' ),
10,
3
);

add_filter(
'graphql_post_object_connection_query_args',
array( '\WPGraphQL\WooCommerce\Connection\Orders', 'post_object_connection_query_args' ),
Expand Down Expand Up @@ -100,6 +108,13 @@ public static function add_filters() {
10,
7
);

add_filter(
'graphql_map_input_fields_to_wp_user_query',
array( '\WPGraphQL\WooCommerce\Connection\Customers', 'map_input_fields_to_wp_query' ),
10,
6
);
}

/**
Expand Down
167 changes: 123 additions & 44 deletions includes/connection/class-customers.php
Expand Up @@ -11,6 +11,7 @@

use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection\UserConnectionResolver;
use WPGraphQL\WooCommerce\Data\Factory;

/**
Expand All @@ -23,43 +24,62 @@ class Customers {
*/
public static function register_connections() {
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'RootQuery',
'toType' => 'Customer',
'fromFieldName' => 'customers',
)
array(
'fromType' => 'RootQuery',
'toType' => 'Customer',
'fromFieldName' => 'customers',
'connectionArgs' => self::get_connection_args(),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new UserConnectionResolver( $source, $args, $context, $info );

if ( ! self::should_execute() ) {
return array(
'nodes' => array(),
'edges' => array(),
);
}

$resolver->set_query_arg( 'role', 'customer' );

return $resolver->get_connection();
},
)
);

register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Coupon',
'toType' => 'Customer',
'fromFieldName' => 'usedBy',
)
array(
'fromType' => 'Coupon',
'toType' => 'Customer',
'fromFieldName' => 'usedBy',
'connectionArgs' => self::get_connection_args(),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new UserConnectionResolver( $source, $args, $context, $info );

$resolver->set_query_arg( 'include', $source->used_by_ids );
$resolver->set_query_arg( 'role', 'customer' );

if ( ! self::should_execute() ) {
return array();
}

return $resolver->get_connection();
},
)
);
}

/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults
* Confirms the uses has the privileges to query Customer
*
* @param array $args - Connection configuration.
* @return array
* @return bool
*/
public static function get_connection_config( $args ): array {
return array_merge(
array(
'connectionArgs' => self::get_connection_args(),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
return Factory::resolve_customer_connection( $source, $args, $context, $info );
},
),
$args
);
public static function should_execute() {
switch ( true ) {
case current_user_can( 'list_users' ):
return true;
default:
return false;
}
}

/**
Expand All @@ -69,42 +89,101 @@ public static function get_connection_config( $args ): array {
*/
public static function get_connection_args(): array {
return array(
'search' => array(
'search' => array(
'type' => 'String',
'description' => __( 'Limit results to those matching a string.', 'wp-graphql-woocommerce' ),
),
'exclude' => array(
'exclude' => array(
'type' => array( 'list_of' => 'Int' ),
'description' => __( 'Ensure result set excludes specific IDs.', 'wp-graphql-woocommerce' ),
),
'include' => array(
'include' => array(
'type' => array( 'list_of' => 'Int' ),
'description' => __( 'Limit result set to specific ids.', 'wp-graphql-woocommerce' ),
),
'email' => array(
'email' => array(
'type' => 'String',
'description' => __( 'Limit result set to resources with a specific email.', 'wp-graphql-woocommerce' ),
),
'role' => array(
'type' => 'UserRoleEnum',
'description' => __( 'Limit result set to resources with a specific role.', 'wp-graphql-woocommerce' ),
),
'roleIn' => array(
'type' => array( 'list_of' => 'UserRoleEnum' ),
'description' => __( 'Limit result set to resources with a specific group of roles.', 'wp-graphql-woocommerce' ),
),
'roleNotIn' => array(
'type' => array( 'list_of' => 'UserRoleEnum' ),
'description' => __( 'Limit result set to resources not within a specific group of roles.', 'wp-graphql-woocommerce' ),
),
'orderby' => array(
'orderby' => array(
'type' => 'CustomerConnectionOrderbyEnum',
'description' => __( 'Order results by a specific field.', 'wp-graphql-woocommerce' ),
),
'order' => array(
'order' => array(
'type' => 'OrderEnum',
'description' => __( 'Order of results.', 'wp-graphql-woocommerce' ),
),
);
}

/**
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
* from a GraphQL Query to the WP_Query
*
* @param array $query_args The mapped query arguments.
* @param array $where_args Query "where" args.
* @param mixed $source The query results for a query calling this.
* @param array $args All of the arguments for the query (not just the "where" args).
* @param AppContext $context The AppContext object.
* @param ResolveInfo $info The ResolveInfo object.
*
* @return array Query arguments.
*/
public static function map_input_fields_to_wp_query( $query_args, $where_args, $source, $args, $context, $info ) {
$key_mapping = array(
'search' => 'search',
'exclude' => 'exclude',
'include' => 'include',
);

foreach ( $key_mapping as $key => $field ) {
if ( ! empty( $where_args[ $key ] ) ) {
$query_args[ $field ] = $where_args[ $key ];
}
}

// Filter by email.
if ( ! empty( $where_args['email'] ) ) {
$query_args['search'] = $where_args['email'];
$query_args['search_columns'] = array( 'user_email' );
}

/**
* Map the orderby inputArgs to the WP_Query
*/
if ( ! empty( $where_args['orderby'] ) ) {
$query_args['orderby'] = $where_args['orderby'];
}

/**
* Map the orderby inputArgs to the WP_Query
*/
if ( ! empty( $where_args['order'] ) ) {
$query_args['order'] = $where_args['order'];
}

/**
* Filter the input fields
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
* from a GraphQL Query to the WP_Query
*
* @param array $args The mapped query arguments
* @param array $where_args Query "where" args
* @param mixed $source The query results for a query calling this
* @param array $all_args All of the arguments for the query (not just the "where" args)
* @param AppContext $context The AppContext object
* @param ResolveInfo $info The ResolveInfo object
*/
$query_args = apply_filters(
'graphql_map_input_fields_to_customer_query',
$query_args,
$where_args,
$source,
$args,
$context,
$info
);

return $query_args;
}
}
1 change: 0 additions & 1 deletion includes/connection/class-products.php
Expand Up @@ -11,7 +11,6 @@

use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Connection\Product_Connection_Resolver;
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;

/**
Expand Down
Expand Up @@ -21,10 +21,6 @@
* Class Cart_Item_Connection_Resolver
*/
class Cart_Item_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include Db Loader connection common functions.
*/
use WC_Db_Loader_Common;

/**
* Return the name of the loader to be used with the connection resolver
Expand Down
Expand Up @@ -16,6 +16,8 @@

/**
* Class Customer_Connection_Resolver
*
* @deprecated v0.10.0
*/
class Customer_Connection_Resolver extends AbstractConnectionResolver {
/**
Expand Down

0 comments on commit 121a651

Please sign in to comment.