Skip to content

Commit fedde04

Browse files
fix: localAttributes and globalAttributes filtering and returned wrong values. (#757)
* fix: Localattribute and globalAttribute filtering. * fixup! fix: Localattribute and globalAttribute filtering. * fixup! fixup! fix: Localattribute and globalAttribute filtering. * fixup! fixup! fixup! fix: Localattribute and globalAttribute filtering.
1 parent 9fbf259 commit fedde04

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

includes/connection/class-product-attributes.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,28 @@ class Product_Attributes {
2626
*/
2727
public static function register_connections() {
2828
// From Product to ProductAttribute.
29-
register_graphql_connection( self::get_connection_config() );
29+
register_graphql_connection(
30+
self::get_connection_config()
31+
);
3032

3133
// From Product to LocalProductAttribute.
3234
register_graphql_connection(
3335
self::get_connection_config(
3436
[
35-
'toType' => 'LocalProductAttribute',
36-
'fromFieldName' => 'localAttributes',
37-
]
37+
'toType' => 'LocalProductAttribute',
38+
'fromFieldName' => 'localAttributes',
39+
'connectionArgs' => [],
40+
],
3841
)
3942
);
4043

4144
// From Product to GlobalProductAttribute.
4245
register_graphql_connection(
4346
self::get_connection_config(
4447
[
45-
'toType' => 'GlobalProductAttribute',
46-
'fromFieldName' => 'globalAttributes',
48+
'toType' => 'GlobalProductAttribute',
49+
'fromFieldName' => 'globalAttributes',
50+
'connectionArgs' => [],
4751
]
4852
)
4953
);
@@ -65,8 +69,15 @@ public static function get_connection_config( $args = [] ): array {
6569
'connectionArgs' => self::get_connection_args(),
6670
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
6771
$resolver = new Product_Attribute_Connection_Resolver();
68-
69-
return $resolver->resolve( $source, $args, $context, $info );
72+
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
73+
switch ( $info->fieldName ) {
74+
case 'globalAttributes':
75+
return $resolver->resolve( $source, $args, $context, $info, 'global' );
76+
case 'localAttributes':
77+
return $resolver->resolve( $source, $args, $context, $info, 'local' );
78+
default:
79+
return $resolver->resolve( $source, $args, $context, $info );
80+
}
7081
},
7182
],
7283
$args

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ class Product_Attribute_Connection_Resolver {
3030
* @param array $args Connection arguments.
3131
* @param AppContext $context AppContext object.
3232
* @param ResolveInfo $info ResolveInfo object.
33+
* @param string $type Attribute type.
3334
*
3435
* @throws UserError Invalid product attribute enumeration value.
3536
* @return array
3637
*/
37-
private function get_items( $attributes, $source, $args, $context, $info ) {
38+
private function get_items( $attributes, $source, $args, $context, $info, $type = null ) {
3839
$items = [];
3940
foreach ( $attributes as $attribute_name => $data ) {
4041
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
@@ -48,8 +49,10 @@ private function get_items( $attributes, $source, $args, $context, $info ) {
4849
$items[] = $data;
4950
}
5051

51-
if ( ! empty( $args['type'] ) ) {
52-
switch ( $args['type'] ) {
52+
$type = ! empty( $args['where']['type'] ) ? $args['where']['type'] : $type;
53+
54+
if ( ! is_null( $type ) ) {
55+
switch ( $type ) {
5356
case 'local':
5457
$items = array_filter(
5558
$items,
@@ -81,11 +84,12 @@ function( $item ) {
8184
* @param array $args Connection arguments.
8285
* @param AppContext $context AppContext object.
8386
* @param ResolveInfo $info ResolveInfo object.
87+
* @param string $type Attribute type.
8488
*
8589
* @return array|null
8690
*/
87-
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
88-
$attributes = $this->get_items( $source->attributes, $source, $args, $context, $info );
91+
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info, $type = null ) {
92+
$attributes = $this->get_items( $source->attributes, $source, $args, $context, $info, $type );
8993

9094
$connection = Relay::connectionFromArray( $attributes, $args );
9195
$nodes = [];

0 commit comments

Comments
 (0)