Skip to content

Commit df8e25c

Browse files
authored
Merge pull request #243 from wp-graphql/connection-updates
Updates to the Connection Classes
2 parents ef50f41 + ead84c2 commit df8e25c

19 files changed

+298
-258
lines changed

.phpcs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<!-- Rules: Check PHP version compatibility -->
2424
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
25-
<config name="testVersion" value="5.6-"/>
25+
<config name="testVersion" value="7.0-"/>
2626
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
2727
<rule ref="PHPCompatibilityWP"/>
2828

README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: kidunot89, ranaaterning, jasonbahl, saleebm
33
Tags: GraphQL, WooCommerce, WPGraphQL
44
Requires at least: 4.9
55
Tested up to: 5.2
6-
Requires PHP: 5.6
6+
Requires PHP: 7.0
77
Requires WooCommerce: 3.0.0
88
Requires WPGraphQL: 0.6.0+
99
Works with WPGraphQL-JWT-Authentication: 0.4.0+

includes/connection/class-cart-items.php

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
namespace WPGraphQL\WooCommerce\Connection;
1212

13+
use GraphQL\Type\Definition\ResolveInfo;
14+
use WPGraphQL\AppContext;
1315
use WPGraphQL\WooCommerce\Data\Factory;
1416

1517
/**
1618
* Class - Cart_Items
1719
*/
1820
class Cart_Items {
21+
1922
/**
2023
* Registers the various connections from other Types to CartItem
2124
*/
@@ -26,63 +29,61 @@ public static function register_connections() {
2629

2730
/**
2831
* Given an array of $args, this returns the connection config, merging the provided args
29-
* with the defaults
32+
* with the defaults.
3033
*
31-
* @access public
3234
* @param array $args - Connection configuration.
33-
*
3435
* @return array
3536
*/
36-
public static function get_connection_config( $args = array() ) {
37-
$defaults = array(
38-
'fromType' => 'Cart',
39-
'toType' => 'CartItem',
40-
'fromFieldName' => 'contents',
41-
'connectionArgs' => self::get_connection_args(),
42-
'connectionFields' => array(
43-
'itemCount' => array(
44-
'type' => 'Int',
45-
'description' => __( 'Total number of items in the cart.', 'wp-graphql-woocommerce' ),
46-
'resolve' => function( $source ) {
47-
if ( empty( $source['edges'] ) ) {
48-
return 0;
49-
}
37+
public static function get_connection_config( $args = array() ): array {
38+
return array_merge(
39+
array(
40+
'fromType' => 'Cart',
41+
'toType' => 'CartItem',
42+
'fromFieldName' => 'contents',
43+
'connectionArgs' => self::get_connection_args(),
44+
'connectionFields' => array(
45+
'itemCount' => array(
46+
'type' => 'Int',
47+
'description' => __( 'Total number of items in the cart.', 'wp-graphql-woocommerce' ),
48+
'resolve' => function( $source ) {
49+
if ( empty( $source['edges'] ) ) {
50+
return 0;
51+
}
5052

51-
$items = array_values( $source['edges'][0]['source']->get_cart() );
52-
$count = 0;
53-
foreach ( $items as $item ) {
54-
$count += $item['quantity'];
55-
}
53+
$items = array_values( $source['edges'][0]['source']->get_cart() );
54+
if ( empty( $items ) ) {
55+
return 0;
56+
}
5657

57-
return $count;
58-
},
59-
),
60-
'productCount' => array(
61-
'type' => 'Int',
62-
'description' => __( 'Total number of different products in the cart', 'wp-graphql-woocommerce' ),
63-
'resolve' => function( $source ) {
64-
if ( empty( $source['edges'] ) ) {
65-
return 0;
66-
}
58+
return array_sum( array_column( $items, 'quantity' ) );
59+
},
60+
),
61+
'productCount' => array(
62+
'type' => 'Int',
63+
'description' => __( 'Total number of different products in the cart', 'wp-graphql-woocommerce' ),
64+
'resolve' => function( $source ) {
65+
if ( empty( $source['edges'] ) ) {
66+
return 0;
67+
}
6768

68-
$items = array_values( $source['edges'][0]['source']->get_cart() );
69-
return count( $items );
70-
},
69+
return count( array_values( $source['edges'][0]['source']->get_cart() ) );
70+
},
71+
),
7172
),
73+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
74+
return Factory::resolve_cart_item_connection( $source, $args, $context, $info );
75+
},
7276
),
73-
'resolve' => function ( $source, $args, $context, $info ) {
74-
return Factory::resolve_cart_item_connection( $source, $args, $context, $info );
75-
},
77+
$args
7678
);
77-
return array_merge( $defaults, $args );
7879
}
7980

8081
/**
81-
* Returns array of where args
82+
* Returns array of where args.
8283
*
8384
* @return array
8485
*/
85-
public static function get_connection_args() {
86+
public static function get_connection_args(): array {
8687
return array(
8788
'needsShipping' => array(
8889
'type' => 'Boolean',

includes/connection/class-coupons.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
namespace WPGraphQL\WooCommerce\Connection;
1111

12+
use GraphQL\Type\Definition\ResolveInfo;
13+
use WPGraphQL\AppContext;
1214
use WPGraphQL\WooCommerce\Data\Factory;
1315

1416
/**
1517
* Class - Coupons
1618
*/
1719
class Coupons {
20+
1821
/**
1922
* Registers the various connections from other Types to Coupon
2023
*/
@@ -35,35 +38,35 @@ public static function register_connections() {
3538

3639
/**
3740
* Given an array of $args, this returns the connection config, merging the provided args
38-
* with the defaults
41+
* with the defaults.
3942
*
40-
* @access public
4143
* @param array $args - Connection configuration.
42-
*
4344
* @return array
4445
*/
45-
public static function get_connection_config( $args = array() ) {
46-
$defaults = array(
47-
'fromType' => 'RootQuery',
48-
'toType' => 'Coupon',
49-
'fromFieldName' => 'coupons',
50-
'connectionArgs' => self::get_connection_args(),
51-
'resolveNode' => function( $id, $args, $context, $info ) {
52-
return Factory::resolve_crud_object( $id, $context );
53-
},
54-
'resolve' => function ( $source, $args, $context, $info ) {
55-
return Factory::resolve_coupon_connection( $source, $args, $context, $info );
56-
},
46+
public static function get_connection_config( $args = array() ): array {
47+
return array_merge(
48+
array(
49+
'fromType' => 'RootQuery',
50+
'toType' => 'Coupon',
51+
'fromFieldName' => 'coupons',
52+
'connectionArgs' => self::get_connection_args(),
53+
'resolveNode' => function( $id, array $args, AppContext $context ) {
54+
return Factory::resolve_crud_object( $id, $context );
55+
},
56+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
57+
return Factory::resolve_coupon_connection( $source, $args, $context, $info );
58+
},
59+
),
60+
$args
5761
);
58-
return array_merge( $defaults, $args );
5962
}
6063

6164
/**
62-
* Returns array of where args
65+
* Returns array of where args.
6366
*
6467
* @return array
6568
*/
66-
public static function get_connection_args() {
69+
public static function get_connection_args(): array {
6770
return array_merge(
6871
get_common_post_type_args(),
6972
array(

includes/connection/class-customers.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
namespace WPGraphQL\WooCommerce\Connection;
1111

12+
use GraphQL\Type\Definition\ResolveInfo;
13+
use WPGraphQL\AppContext;
1214
use WPGraphQL\WooCommerce\Data\Factory;
1315

1416
/**
1517
* Class - Customers
1618
*/
1719
class Customers {
20+
1821
/**
1922
* Registers the various connections from other Types to Customer
2023
*/
@@ -28,6 +31,7 @@ public static function register_connections() {
2831
)
2932
)
3033
);
34+
3135
register_graphql_connection(
3236
self::get_connection_config(
3337
array(
@@ -43,30 +47,30 @@ public static function register_connections() {
4347
* Given an array of $args, this returns the connection config, merging the provided args
4448
* with the defaults
4549
*
46-
* @access public
4750
* @param array $args - Connection configuration.
48-
*
4951
* @return array
5052
*/
51-
public static function get_connection_config( $args ) {
52-
$defaults = array(
53-
'connectionArgs' => self::get_connection_args(),
54-
'resolveNode' => function( $id, $args, $context, $info ) {
55-
return Factory::resolve_customer( $id, $context );
56-
},
57-
'resolve' => function ( $source, $args, $context, $info ) {
58-
return Factory::resolve_customer_connection( $source, $args, $context, $info );
59-
},
53+
public static function get_connection_config( $args ): array {
54+
return array_merge(
55+
array(
56+
'connectionArgs' => self::get_connection_args(),
57+
'resolveNode' => function( $id, array $args, AppContext $context ) {
58+
return Factory::resolve_customer( $id, $context );
59+
},
60+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
61+
return Factory::resolve_customer_connection( $source, $args, $context, $info );
62+
},
63+
),
64+
$args
6065
);
61-
return array_merge( $defaults, $args );
6266
}
6367

6468
/**
65-
* Returns array of where args
69+
* Returns array of where args.
6670
*
6771
* @return array
6872
*/
69-
public static function get_connection_args() {
73+
public static function get_connection_args(): array {
7074
return array(
7175
'search' => array(
7276
'type' => 'String',

includes/connection/class-downloadable-items.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
namespace WPGraphQL\WooCommerce\Connection;
1212

13+
use GraphQL\Type\Definition\ResolveInfo;
14+
use WPGraphQL\AppContext;
1315
use WPGraphQL\WooCommerce\Data\Factory;
1416

1517
/**
1618
* Class - Downloadable_Items
1719
*/
1820
class Downloadable_Items {
21+
1922
/**
2023
* Registers the various connections from other Types to DownloadableItem
2124
*/
@@ -33,32 +36,32 @@ public static function register_connections() {
3336

3437
/**
3538
* Given an array of $args, this returns the connection config, merging the provided args
36-
* with the defaults
39+
* with the defaults.
3740
*
38-
* @access public
3941
* @param array $args - Connection configuration.
40-
*
4142
* @return array
4243
*/
43-
public static function get_connection_config( $args = array() ) {
44-
$defaults = array(
45-
'fromType' => 'Order',
46-
'toType' => 'DownloadableItem',
47-
'fromFieldName' => 'downloadableItems',
48-
'connectionArgs' => self::get_connection_args(),
49-
'resolve' => function ( $source, $args, $context, $info ) {
50-
return Factory::resolve_downloadable_item_connection( $source, $args, $context, $info );
51-
},
44+
public static function get_connection_config( $args = array() ): array {
45+
return array_merge(
46+
array(
47+
'fromType' => 'Order',
48+
'toType' => 'DownloadableItem',
49+
'fromFieldName' => 'downloadableItems',
50+
'connectionArgs' => self::get_connection_args(),
51+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
52+
return Factory::resolve_downloadable_item_connection( $source, $args, $context, $info );
53+
},
54+
),
55+
$args
5256
);
53-
return array_merge( $defaults, $args );
5457
}
5558

5659
/**
57-
* Returns array of where args
60+
* Returns array of where args.
5861
*
5962
* @return array
6063
*/
61-
public static function get_connection_args() {
64+
public static function get_connection_args(): array {
6265
return array(
6366
'active' => array(
6467
'type' => 'Boolean',

includes/connection/class-order-items.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010

1111
namespace WPGraphQL\WooCommerce\Connection;
1212

13+
use GraphQL\Type\Definition\ResolveInfo;
14+
use WPGraphQL\AppContext;
1315
use WPGraphQL\WooCommerce\Data\Factory;
1416

1517
/**
1618
* Class - Order_Items
1719
*/
1820
class Order_Items {
21+
1922
/**
20-
* Registers connection
23+
* Registers connections.
2124
*/
2225
public static function register_connections() {
2326
// From Order.
@@ -63,24 +66,23 @@ public static function register_connections() {
6366
* Given an array of $args, this returns the connection config, merging the provided args
6467
* with the defaults
6568
*
66-
* @access public
6769
* @param array $args - Connection configuration.
68-
*
6970
* @return array
7071
*/
71-
public static function get_connection_config( $args = array() ) {
72+
public static function get_connection_config( $args = array() ): array {
7273
$defaults = array(
7374
'fromType' => 'Order',
7475
'toType' => 'LineItem',
7576
'fromFieldName' => 'lineItems',
7677
'connectionArgs' => array(),
77-
'resolveNode' => function( $item, $args, $context, $info ) {
78+
'resolveNode' => function( $item ) {
7879
return Factory::resolve_order_item( $item );
7980
},
80-
'resolve' => function ( $source, $args, $context, $info ) {
81+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
8182
return Factory::resolve_order_item_connection( $source, $args, $context, $info );
8283
},
8384
);
85+
8486
return array_merge( $defaults, $args );
8587
}
8688
}

0 commit comments

Comments
 (0)