Deprecated "By" queries removed. More connection resolver cleanup.

This commit is contained in:
Geoff Taylor
2020-05-11 21:22:28 -04:00
committed by Geoff Taylor
parent 14f5b07e3b
commit 7ffd418bff
23 changed files with 107 additions and 249 deletions
+1 -1
View File
@@ -32,7 +32,7 @@
},
"autoload": {
"files":[
"includes/connection/common-post-type-args.php",
"includes/connection/wc-cpt-connection-args.php",
"includes/functions.php"
],
"psr-4": {
+1 -1
View File
@@ -65,7 +65,7 @@ class Coupons {
*/
public static function get_connection_args(): array {
return array_merge(
get_common_post_type_args(),
get_wc_cpt_connection_args(),
array(
'code' => array(
'type' => 'String',
+1 -1
View File
@@ -72,7 +72,7 @@ class Orders {
switch ( $access ) {
case 'private':
return array_merge(
get_common_post_type_args(),
get_wc_cpt_connection_args(),
array(
'statuses' => array(
'type' => array( 'list_of' => 'OrderStatusEnum' ),
+1 -1
View File
@@ -274,6 +274,6 @@ class Products {
);
}
return array_merge( get_common_post_type_args(), $args );
return array_merge( get_wc_cpt_connection_args(), $args );
}
}
+1 -1
View File
@@ -66,7 +66,7 @@ class Refunds {
*/
public static function get_connection_args(): array {
return array_merge(
get_common_post_type_args(),
get_wc_cpt_connection_args(),
array(
'statuses' => array(
'type' => array( 'list_of' => 'String' ),
@@ -1,6 +1,6 @@
<?php
/**
* Define common arguments for post-type connections.
* Define common connection arguments for CPT connections.
*
* @package WPGraphQL\WooCommerce\Connection
* @since 0.2.2
@@ -9,11 +9,11 @@
namespace WPGraphQL\WooCommerce\Connection;
/**
* Returns argument definitions for argument common on all post-type connections.
* Returns argument definitions for argument common on CPT connections.
*
* @return array
*/
function get_common_post_type_args(): array {
function get_wc_cpt_connection_args(): array {
return array(
'search' => array(
'type' => 'String',
@@ -22,9 +22,9 @@ use WPGraphQL\WooCommerce\Data\Factory;
*/
class Cart_Item_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include shared connection functions.
* Include Db Loader connection common functions.
*/
use WC_Connection_Functions;
use WC_Db_Loader_Common;
const PREFIX = 'CI';
@@ -132,8 +132,7 @@ class Cart_Item_Connection_Resolver extends AbstractConnectionResolver {
/**
* Create cursor for downloadable item node.
*
* @param array $node Cart item.
* @param string $key Cart item key.
* @param string $id Cart item key.
*
* @return string
*/
@@ -151,14 +150,15 @@ class Cart_Item_Connection_Resolver extends AbstractConnectionResolver {
}
/**
* Wrapper for "WC_Connection_Functions::is_valid_cart_item_offset()"
* Check if cart item key is valid by confirming the validity of
* the cart item in the cart encoded into cart item key.
*
* @param integer $offset Post ID.
* @param string $offset Cart item key.
*
* @return bool
*/
public function is_valid_offset( $offset ) {
return $this->is_valid_cart_item_offset( $offset );
return ! empty( $this->source->get_cart_item( $offset ) );
}
/**
@@ -21,9 +21,9 @@ use WPGraphQL\Extension\WooCommerce\Model\Coupon;
*/
class Coupon_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include shared connection functions.
* Include CPT Loader connection common functions.
*/
use WC_Connection_Functions;
use WC_CPT_Loader_Common;
/**
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -18,11 +18,6 @@ use WPGraphQL\WooCommerce\Model\Coupon;
* Class Customer_Connection_Resolver
*/
class Customer_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include shared connection functions.
*/
use WC_Connection_Functions;
/**
* Return the name of the loader to be used with the connection resolver
*
@@ -224,13 +219,21 @@ class Customer_Connection_Resolver extends AbstractConnectionResolver {
}
/**
* Wrapper for "WC_Connection_Functions::is_valid_user_offset()"
* Determine whether or not the the offset is valid, i.e the user corresponding to the offset exists.
* Offset is equivalent to user_id. So this function is equivalent
* to checking if the user with the given ID exists.
*
* @param integer $offset User ID.
* @param integer $offset User ID.
*
* @return bool
*/
public function is_valid_offset( $offset ) {
return $this->is_valid_user_offset( $offset );
global $wpdb;
if ( ! empty( wp_cache_get( $offset, 'users' ) ) ) {
return true;
}
return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS (SELECT 1 FROM $wpdb->users WHERE ID = %d)", $offset ) );
}
}
@@ -23,9 +23,9 @@ use WPGraphQL\WooCommerce\Model\Customer;
*/
class Downloadable_Item_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include shared connection functions.
* Include Db Loader connection common functions.
*/
use WC_Connection_Functions;
use WC_Db_Loader_Common;
const PREFIX = 'DI';
@@ -172,8 +172,7 @@ class Downloadable_Item_Connection_Resolver extends AbstractConnectionResolver {
/**
* Create cursor for downloadable item node.
*
* @param array $node Cart item.
* @param string $key Cart item key.
* @param string $id Downloadable item ID.
*
* @return string
*/
@@ -211,7 +210,7 @@ class Downloadable_Item_Connection_Resolver extends AbstractConnectionResolver {
*
* @return bool
*/
protected function is_valid_model( array $model ) {
protected function is_valid_model( $model ) {
return isset( $model ) && ! empty( $model['download_id'] );
}
}
@@ -21,9 +21,9 @@ use WPGraphQL\WooCommerce\Model\Order;
*/
class Order_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include shared connection functions.
* Include CPT Loader connection common functions.
*/
use WC_Connection_Functions;
use WC_CPT_Loader_Common;
/**
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -24,9 +24,9 @@ use WPGraphQL\WooCommerce\Model\Product_Variation;
*/
class Product_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include shared connection functions.
* Include CPT Loader connection common functions.
*/
use WC_Connection_Functions;
use WC_CPT_Loader_Common;
/**
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -22,9 +22,9 @@ use WPGraphQL\WooCommerce\Model\Refund;
*/
class Refund_Connection_Resolver extends AbstractConnectionResolver {
/**
* Include shared connection functions.
* Include CPT Loader connection common functions.
*/
use WC_Connection_Functions;
use WC_CPT_Loader_Common;
/**
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -1,9 +1,9 @@
<?php
/**
* Defines reusable functions for all connection.
* Defines generic functions for to be used in connections that process creates using the CPT Loader.
*
* @package WPGraphQL\WooCommerce\Data\Connection
* @since 0.3.3
* @since 0.5.0
*/
namespace WPGraphQL\WooCommerce\Data\Connection;
@@ -11,9 +11,9 @@ namespace WPGraphQL\WooCommerce\Data\Connection;
use WPGraphQL\WooCommerce\Data\Factory;
/**
* Trait WC_Connection_Functions
* Trait WC_CPT_Loader_Common
*/
trait WC_Connection_Functions {
trait WC_CPT_Loader_Common {
/**
* Determine whether or not the the offset is valid, i.e the post corresponding to the offset exists.
* Offset is equivalent to post_id. So this function is equivalent
@@ -33,38 +33,6 @@ trait WC_Connection_Functions {
return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS (SELECT 1 FROM $wpdb->posts WHERE ID = %d)", $offset ) );
}
/**
* Determine whether or not the the offset is valid, i.e the cart item corresponding to the offset exists.
* Offset is equivalent to a cart item key. So this function is equivalent
* to checking if the cart item with the given key exists.
*
* @param string $offset Cart item key.
*
* @return bool
*/
public function is_valid_cart_item_offset( $offset ) {
return ! empty( $this->source->get_cart_item( $offset ) );
}
/**
* Determine whether or not the the offset is valid, i.e the user corresponding to the offset exists.
* Offset is equivalent to user_id. So this function is equivalent
* to checking if the user with the given ID exists.
*
* @param integer $offset User ID.
*
* @return bool
*/
public function is_valid_user_offset( $offset ) {
global $wpdb;
if ( ! empty( wp_cache_get( $offset, 'users' ) ) ) {
return true;
}
return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS (SELECT 1 FROM $wpdb->users WHERE ID = %d)", $offset ) );
}
/**
* Sanitizes common post-type connection query input.
*
@@ -148,28 +116,4 @@ trait WC_Connection_Functions {
public function get_cpt_model_by_id( $id ) {
return $this->loader->resolve_model( get_post_type( $id ), $id );
}
/**
* Given an offset and prefix, a cursor is returned
*
* @param string $prefix Salt.
* @param string|integer $offset Connection offset.
*
* @return string
*/
protected function offset_to_cursor( $prefix, $offset ) {
return base64_encode( "{$prefix}:{$offset}" );
}
/**
* Given a valid cursor and prefix, the offset is returned
*
* @param string $prefix Salt.
* @param string $cursor Cursor.
*
* @return string|integer
*/
protected function cursor_to_offset( $prefix, $cursor ) {
return substr( base64_decode( $cursor ), strlen( $prefix . ':' ) );
}
}
@@ -0,0 +1,40 @@
<?php
/**
* Defines generic functions for to be used in connections that process creates using the Db Loader.
*
* @package WPGraphQL\WooCommerce\Data\Connection
* @since 0.5.0
*/
namespace WPGraphQL\WooCommerce\Data\Connection;
use WPGraphQL\WooCommerce\Data\Factory;
/**
* Trait WC_Db_Loader_Common
*/
trait WC_Db_Loader_Common {
/**
* Given an offset and prefix, a cursor is returned
*
* @param string $prefix Salt.
* @param string|integer $offset Connection offset.
*
* @return string
*/
public function offset_to_cursor( $prefix, $offset ) {
return base64_encode( "{$prefix}:{$offset}" );
}
/**
* Given a valid cursor and prefix, the offset is returned
*
* @param string $prefix Salt.
* @param string $cursor Cursor.
*
* @return string|integer
*/
public function cursor_to_offset( $prefix, $cursor ) {
return substr( base64_decode( $cursor ), strlen( $prefix . ':' ) );
}
}
+12 -8
View File
@@ -145,14 +145,18 @@ class WC_CPT_Loader extends AbstractDataLoader {
$parent_id = null;
// Resolve post author for future capability checks.
if ( 'shop_order' === $post_type ) {
$customer_id = get_post_meta( $key, '_customer_user', true );
if ( ! empty( $customer_id ) ) {
$this->context->getLoader( 'wc_customer' )->buffer( [ $customer_id ] );
}
} elseif ( 'product_variation' === $post_type || 'shop_refund' === $post_type ) {
$parent_id = get_post_field( 'post_parent', $key );
$this->buffer( [ $parent_id ] );
switch ( $post_type ) {
case 'shop_order':
$customer_id = get_post_meta( $key, '_customer_user', true );
if ( ! empty( $customer_id ) ) {
$this->context->getLoader( 'wc_customer' )->buffer( [ $customer_id ] );
}
break;
case 'product_variation':
case 'shop_refund':
$parent_id = get_post_field( 'post_parent', $key );
$this->buffer( [ $parent_id ] );
break;
}
/**
@@ -143,10 +143,6 @@ class WC_Db_Loader extends AbstractDataLoader {
return new Tax_Rate( $rate );
} else {
return null;
// throw new UserError(
// /* translators: %s: Tax rate ID */
// sprintf( __( 'No Tax Rate assigned to ID %s was found ', 'wp-graphql-woocommerce' ), $id )
// );
}
}
}
-69
View File
@@ -103,75 +103,6 @@ class Product {
},
)
);
/**
* DEPRECATED
*
* Will be removed in v0.5.x.
*/
register_graphql_field(
'RootQuery',
'productBy',
array(
'type' => 'Product',
'isDeprecated' => true,
'deprecationReason' => __(
'This query has been deprecation, and will be removed in v0.5.x. Please use "product(id: value, idType: DATABASE_ID|SLUG|SKU)" instead',
'wp-graphql-woocommerce'
),
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
'args' => array(
'id' => array(
'type' => 'ID',
'description' => __( 'Get the product by its global ID', 'wp-graphql-woocommerce' ),
),
'productId' => array(
'type' => 'Int',
'description' => __( 'Get the product by its database ID', 'wp-graphql-woocommerce' ),
),
'slug' => array(
'type' => 'String',
'description' => __( 'Get the product by its slug', 'wp-graphql-woocommerce' ),
),
'sku' => array(
'type' => 'String',
'description' => __( 'Get the product by its sku', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function ( $source, array $args, AppContext $context ) {
$product_id = 0;
$id_type = '';
if ( ! empty( $args['id'] ) ) {
$id_components = Relay::fromGlobalId( $args['id'] );
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
}
$product_id = absint( $id_components['id'] );
$id_type = 'ID';
} elseif ( ! empty( $args['productId'] ) ) {
$product_id = absint( $args['productId'] );
$id_type = 'product ID';
} elseif ( ! empty( $args['slug'] ) ) {
$post = get_page_by_path( $args['slug'], OBJECT, 'product' );
$product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
$id_type = 'slug';
} elseif ( ! empty( $args['sku'] ) ) {
$product_id = \wc_get_product_id_by_sku( $args['sku'] );
$id_type = 'sku';
}
if ( empty( $product_id ) ) {
/* translators: %1$s: ID type, %2$s: ID value */
throw new UserError( sprintf( __( 'No product ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $product_id ) );
} elseif ( get_post( $product_id )->post_type !== 'product' ) {
/* translators: %1$s: ID type, %2$s: ID value */
throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $product_id ) );
}
return Factory::resolve_crud_object( $product_id, $context );
},
)
);
}
/**
@@ -157,69 +157,5 @@ class Coupon_Type {
},
)
);
/**
* DEPRECATED
*
* Will be removed in v0.5.x.
*/
register_graphql_field(
'RootQuery',
'couponBy',
array(
'type' => 'Coupon',
'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
'isDeprecated' => true,
'deprecationReason' => __(
'This query has been deprecation, and will be removed in v0.5.x. Please use "coupon(id: value, idType: DATABASE_ID|CODE)" instead',
'wp-graphql-woocommerce'
),
'args' => array(
'id' => array(
'type' => 'ID',
'description' => __( 'Get the coupon by its global ID', 'wp-graphql-woocommerce' ),
),
'couponId' => array(
'type' => 'Int',
'description' => __( 'Get the coupon by its database ID', 'wp-graphql-woocommerce' ),
),
'code' => array(
'type' => 'String',
'description' => __( 'Get the coupon by its code', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, array $args, AppContext $context ) {
$coupon_id = null;
if ( ! empty( $args['id'] ) ) {
$id_components = Relay::fromGlobalId( $args['id'] );
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
}
$coupon_id = absint( $id_components['id'] );
$id = $args['id'];
$id_type = 'ID';
} elseif ( ! empty( $args['couponId'] ) ) {
$coupon_id = absint( $args['couponId'] );
$id = $args['couponId'];
$id_type = 'DATABASE_ID';
} elseif ( ! empty( $args['code'] ) ) {
$coupon_id = \wc_get_coupon_id_by_code( $args['code'] );
$id = $args['code'];
$id_type = 'CODE';
}
if ( empty( $coupon_id ) ) {
/* translators: %1$s: ID type, %2$s: ID value */
throw new UserError( sprintf( __( 'No coupon ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
} elseif ( get_post( $coupon_id )->post_type !== 'shop_coupon' ) {
/* translators: %1$s: ID type, %2$s: ID value */
throw new UserError( sprintf( __( 'No coupon exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
}
return Factory::resolve_crud_object( $coupon_id, $context );
},
)
);
}
}
+7 -4
View File
@@ -182,8 +182,8 @@ class ProductQueriesTest extends \Codeception\TestCase\WPTestCase {
);
$query = '
query ( $id: ID, $productId: Int, $slug: String, $sku: String ) {
productBy( id: $id, productId: $productId, slug: $slug, sku: $sku ) {
query ( $id: ID!, $idType: ProductIdTypeEnum ) {
product( id: $id, idType: $idType ) {
... on SimpleProduct {
id
image {
@@ -214,11 +214,14 @@ class ProductQueriesTest extends \Codeception\TestCase\WPTestCase {
*
* Test querying product with "productId" argument.
*/
$variables = array( 'productId' => $product );
$variables = array(
'id' => $product,
'idType' => 'DATABASE_ID'
);
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
$expected = array(
'data' => array(
'productBy' => array(
'product' => array(
'id' => $this->helper->to_relay_id( $product ),
'image' => array(
'id' => \GraphQLRelay\Relay::toGlobalId( 'attachment', $attachment_id ),
+2 -1
View File
@@ -43,7 +43,8 @@ return array(
'WPGraphQL\\WooCommerce\\Data\\Connection\\Shipping_Method_Connection_Resolver' => $baseDir . '/includes/data/connection/class-shipping-method-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\Tax_Rate_Connection_Resolver' => $baseDir . '/includes/data/connection/class-tax-rate-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\Variation_Attribute_Connection_Resolver' => $baseDir . '/includes/data/connection/class-variation-attribute-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Connection_Functions' => $baseDir . '/includes/data/connection/trait-wc-connection-functions.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_CPT_Loader_Common' => $baseDir . '/includes/data/connection/trait-wc-cpt-loader-common.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Db_Loader_Common' => $baseDir . '/includes/data/connection/trait-wc-db-loader-common.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Term_Connection_Resolver' => $baseDir . '/includes/data/connection/class-wc-term-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Factory' => $baseDir . '/includes/data/class-factory.php',
'WPGraphQL\\WooCommerce\\Data\\Loader\\WC_CPT_Loader' => $baseDir . '/includes/data/loader/class-wc-cpt-loader.php',
+1 -1
View File
@@ -6,6 +6,6 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'914b07b8cf678ed0b81bfdb5d23b4f2b' => $baseDir . '/includes/connection/common-post-type-args.php',
'944484f100dc1864a5320474d49ebd5a' => $baseDir . '/includes/connection/wc-cpt-connection-args.php',
'45a15019e901000ab8608c03ebff44fb' => $baseDir . '/includes/functions.php',
);
+3 -2
View File
@@ -7,7 +7,7 @@ namespace Composer\Autoload;
class ComposerStaticInit605a02cd7bfd584c6ac9e4f2b4d51817
{
public static $files = array (
'914b07b8cf678ed0b81bfdb5d23b4f2b' => __DIR__ . '/../..' . '/includes/connection/common-post-type-args.php',
'944484f100dc1864a5320474d49ebd5a' => __DIR__ . '/../..' . '/includes/connection/wc-cpt-connection-args.php',
'45a15019e901000ab8608c03ebff44fb' => __DIR__ . '/../..' . '/includes/functions.php',
);
@@ -71,7 +71,8 @@ class ComposerStaticInit605a02cd7bfd584c6ac9e4f2b4d51817
'WPGraphQL\\WooCommerce\\Data\\Connection\\Shipping_Method_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-shipping-method-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\Tax_Rate_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-tax-rate-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\Variation_Attribute_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-variation-attribute-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Connection_Functions' => __DIR__ . '/../..' . '/includes/data/connection/trait-wc-connection-functions.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_CPT_Loader_Common' => __DIR__ . '/../..' . '/includes/data/connection/trait-wc-cpt-loader-common.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Db_Loader_Common' => __DIR__ . '/../..' . '/includes/data/connection/trait-wc-db-loader-common.php',
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Term_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-wc-term-connection-resolver.php',
'WPGraphQL\\WooCommerce\\Data\\Factory' => __DIR__ . '/../..' . '/includes/data/class-factory.php',
'WPGraphQL\\WooCommerce\\Data\\Loader\\WC_CPT_Loader' => __DIR__ . '/../..' . '/includes/data/loader/class-wc-cpt-loader.php',