Skip to content

Commit 47905a9

Browse files
committed
refactor: Product_Attribute_Connection_Resolver class refactor to be more consistent with contemporaries
1 parent 32f8ba2 commit 47905a9

File tree

3 files changed

+174
-18
lines changed

3 files changed

+174
-18
lines changed

includes/connection/class-product-attributes.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public static function register_connections() {
4848
]
4949
)
5050
);
51+
52+
// From RootQuery to GlobalProductAttribute.
53+
register_graphql_connection(
54+
self::get_connection_config(
55+
[
56+
'fromType' => 'RootQuery',
57+
'toType' => 'GlobalProductAttribute',
58+
'fromFieldName' => 'productAttributes',
59+
'connectionArgs' => [],
60+
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
61+
$resolver = new Product_Attribute_Connection_Resolver( $source, $args, $context, $info );
62+
63+
return $resolver->get_connection();
64+
},
65+
]
66+
)
67+
);
5168
}
5269

5370
/**
@@ -73,16 +90,21 @@ public static function get_connection_config( $args = [] ): array {
7390
'fromFieldName' => 'attributes',
7491
'connectionArgs' => [],
7592
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
76-
$resolver = new Product_Attribute_Connection_Resolver();
93+
7794
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
7895
switch ( $info->fieldName ) {
7996
case 'globalAttributes':
80-
return $resolver->resolve( $source, $args, $context, $info, 'global' );
97+
$resolver = new Product_Attribute_Connection_Resolver( $source, $args, $context, $info, 'global' );
98+
break;
8199
case 'localAttributes':
82-
return $resolver->resolve( $source, $args, $context, $info, 'local' );
100+
$resolver = new Product_Attribute_Connection_Resolver( $source, $args, $context, $info, 'local' );
101+
break;
83102
default:
84-
return $resolver->resolve( $source, $args, $context, $info );
103+
$resolver = new Product_Attribute_Connection_Resolver( $source, $args, $context, $info );
104+
break;
85105
}
106+
107+
return $resolver->get_connection();
86108
},
87109
],
88110
$args

includes/data/connection/class-product-attribute-connection-resolver.php

Lines changed: 144 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,59 @@
2121
* Class Product_Attribute_Connection_Resolver
2222
*/
2323
class Product_Attribute_Connection_Resolver {
24+
25+
/**
26+
* The source from the field calling the connection.
27+
*
28+
* @var \WPGraphQL\WooCommerce\Model\Product|null
29+
*/
30+
protected $source;
31+
32+
/**
33+
* The args input on the field calling the connection.
34+
*
35+
* @var ?array<string,mixed>
36+
*/
37+
protected $args;
38+
39+
/**
40+
* The AppContext for the GraphQL Request
41+
*
42+
* @var \WPGraphQL\AppContext
43+
*/
44+
protected $context;
45+
46+
/**
47+
* The ResolveInfo for the GraphQL Request
48+
*
49+
* @var \GraphQL\Type\Definition\ResolveInfo
50+
*/
51+
protected $info;
52+
53+
/**
54+
* The attribute type.
55+
*
56+
* @var string
57+
*/
58+
protected $type;
59+
60+
/**
61+
* Product_Attribute_Connection_Resolver constructor.
62+
*
63+
* @param \WPGraphQL\WooCommerce\Model\Product|null $source Source node.
64+
* @param array $args Connection arguments.
65+
* @param \WPGraphQL\AppContext $context AppContext object.
66+
* @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo object.
67+
* @param string $type Attribute type.
68+
*/
69+
public function __construct( $source, array $args, AppContext $context, ResolveInfo $info, string $type = null ) {
70+
$this->source = $source;
71+
$this->args = $args;
72+
$this->context = $context;
73+
$this->info = $info;
74+
$this->type = $type;
75+
}
76+
2477
/**
2578
* Builds Product attribute items
2679
*
@@ -33,25 +86,82 @@ class Product_Attribute_Connection_Resolver {
3386
*
3487
* @throws \GraphQL\Error\UserError Invalid product attribute enumeration value.
3588
* @return array
89+
*
90+
* @deprecated TBD
3691
*/
3792
private function get_items( $attributes, $source, $args, $context, $info, $type = null ) {
93+
_deprecated_function( __METHOD__, 'TBD', static::class . '::build_nodes_from_product_attributes()' );
94+
95+
$this->source = $source;
96+
$this->args = $args;
97+
$this->context = $context;
98+
$this->info = $info;
99+
$this->type = $type;
100+
101+
return $this->build_nodes_from_source_attributes();
102+
}
103+
104+
/**
105+
* Creates connection
106+
*
107+
* @param mixed $source Connection source Model instance.
108+
* @param array $args Connection arguments.
109+
* @param \WPGraphQL\AppContext $context AppContext object.
110+
* @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo object.
111+
* @param string $type Attribute type.
112+
*
113+
* @return array|null
114+
*
115+
* @deprecated TBD
116+
*/
117+
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info, $type = null ) {
118+
_deprecated_function( __METHOD__, 'TBD', static::class . '::get_connection()' );
119+
120+
$this->source = $source;
121+
$this->args = $args;
122+
$this->context = $context;
123+
$this->info = $info;
124+
$this->type = $type;
125+
126+
return $this->get_connection();
127+
}
128+
129+
/**
130+
* Builds connection nodes from source product's attributes.
131+
*
132+
* @return array
133+
*/
134+
private function build_nodes_from_source_attributes() {
38135
$items = [];
136+
if ( ! $this->source ) {
137+
return $items;
138+
}
139+
140+
$attributes = $this->source->attributes;
141+
142+
143+
if ( empty( $attributes ) ) {
144+
return $items;
145+
}
146+
39147
foreach ( $attributes as $attribute_name => $data ) {
40148
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
41149
$data->_relay_id = base64_encode(
42150
$attribute_name
43151
. GLOBAL_ID_DELIMITER
44-
. $source->ID
152+
. $this->source->ID
45153
. GLOBAL_ID_DELIMITER
46154
. $data->get_name()
47155
);
48156
$items[] = $data;
49157
}
50158

51-
$type = ! empty( $args['where']['type'] ) ? $args['where']['type'] : $type;
159+
$attribute_type = ! empty( $this->args['where'] ) && ! empty( $this->args['where']['type'] )
160+
? $this->args['where']['type']
161+
: $this->type;
52162

53-
if ( ! is_null( $type ) ) {
54-
switch ( $type ) {
163+
if ( ! is_null( $attribute_type ) ) {
164+
switch ( $attribute_type ) {
55165
case 'local':
56166
$items = array_filter(
57167
$items,
@@ -77,20 +187,25 @@ static function ( $item ) {
77187
}
78188

79189
/**
80-
* Creates connection
190+
* Builds connection nodes from woocommerce global product attributes.
81191
*
82-
* @param mixed $source Connection source Model instance.
83-
* @param array $args Connection arguments.
84-
* @param \WPGraphQL\AppContext $context AppContext object.
85-
* @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo object.
86-
* @param string $type Attribute type.
192+
* @return array
193+
*/
194+
private function build_nodes_from_global_attributes() {
195+
// TODO: Implement this method.
196+
return [];
197+
}
198+
199+
/**
200+
* Builds connection from nodes array.
201+
*
202+
* @param array $nodes Array of connection nodes.
87203
*
88204
* @return array|null
89205
*/
90-
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info, $type = null ) {
91-
$attributes = $this->get_items( $source->attributes, $source, $args, $context, $info, $type );
92-
93-
$connection = Relay::connectionFromArray( $attributes, $args );
206+
private function build_connection( $nodes = [] ) {
207+
$connection = $this->build_connection( $nodes );
208+
$connection = Relay::connectionFromArray( $nodes, $this->args );
94209
$nodes = [];
95210
if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) {
96211
foreach ( $connection['edges'] as $edge ) {
@@ -100,4 +215,19 @@ public function resolve( $source, array $args, AppContext $context, ResolveInfo
100215
$connection['nodes'] = ! empty( $nodes ) ? $nodes : null;
101216
return ! empty( $attributes ) ? $connection : null;
102217
}
218+
219+
/**
220+
* Constructs the connection.
221+
*
222+
* @return array|null
223+
*/
224+
public function get_connection() {
225+
if ( ! $this->source ) {
226+
$attributes = $this->build_nodes_from_global_attributes();
227+
} else {
228+
$attributes = $this->build_nodes_from_source_attributes();
229+
}
230+
231+
return $this->build_connection( $attributes );
232+
}
103233
}

tests/wpunit/ProductAttributeQueriesTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,8 @@ public function testProductAttributeMatchesVariationAttributeCounterpart() {
235235
[ $this->expectedField( 'product.id', $this->toRelayId( 'post', $product_id ) ) ]
236236
);
237237
}
238+
239+
public function testProductAttributesQuery() {
240+
241+
}
238242
}

0 commit comments

Comments
 (0)