Skip to content

Commit

Permalink
fix: localAttributes and globalAttributes filtering and returned wron…
Browse files Browse the repository at this point in the history
…g 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.
  • Loading branch information
creative-andrew committed Jul 4, 2023
1 parent 9fbf259 commit fedde04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
27 changes: 19 additions & 8 deletions includes/connection/class-product-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@ class Product_Attributes {
*/
public static function register_connections() {
// From Product to ProductAttribute.
register_graphql_connection( self::get_connection_config() );
register_graphql_connection(
self::get_connection_config()
);

// From Product to LocalProductAttribute.
register_graphql_connection(
self::get_connection_config(
[
'toType' => 'LocalProductAttribute',
'fromFieldName' => 'localAttributes',
]
'toType' => 'LocalProductAttribute',
'fromFieldName' => 'localAttributes',
'connectionArgs' => [],
],
)
);

// From Product to GlobalProductAttribute.
register_graphql_connection(
self::get_connection_config(
[
'toType' => 'GlobalProductAttribute',
'fromFieldName' => 'globalAttributes',
'toType' => 'GlobalProductAttribute',
'fromFieldName' => 'globalAttributes',
'connectionArgs' => [],
]
)
);
Expand All @@ -65,8 +69,15 @@ public static function get_connection_config( $args = [] ): array {
'connectionArgs' => self::get_connection_args(),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Product_Attribute_Connection_Resolver();

return $resolver->resolve( $source, $args, $context, $info );
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
switch ( $info->fieldName ) {
case 'globalAttributes':
return $resolver->resolve( $source, $args, $context, $info, 'global' );
case 'localAttributes':
return $resolver->resolve( $source, $args, $context, $info, 'local' );
default:
return $resolver->resolve( $source, $args, $context, $info );
}
},
],
$args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class Product_Attribute_Connection_Resolver {
* @param array $args Connection arguments.
* @param AppContext $context AppContext object.
* @param ResolveInfo $info ResolveInfo object.
* @param string $type Attribute type.
*
* @throws UserError Invalid product attribute enumeration value.
* @return array
*/
private function get_items( $attributes, $source, $args, $context, $info ) {
private function get_items( $attributes, $source, $args, $context, $info, $type = null ) {
$items = [];
foreach ( $attributes as $attribute_name => $data ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
Expand All @@ -48,8 +49,10 @@ private function get_items( $attributes, $source, $args, $context, $info ) {
$items[] = $data;
}

if ( ! empty( $args['type'] ) ) {
switch ( $args['type'] ) {
$type = ! empty( $args['where']['type'] ) ? $args['where']['type'] : $type;

if ( ! is_null( $type ) ) {
switch ( $type ) {
case 'local':
$items = array_filter(
$items,
Expand Down Expand Up @@ -81,11 +84,12 @@ function( $item ) {
* @param array $args Connection arguments.
* @param AppContext $context AppContext object.
* @param ResolveInfo $info ResolveInfo object.
* @param string $type Attribute type.
*
* @return array|null
*/
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
$attributes = $this->get_items( $source->attributes, $source, $args, $context, $info );
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info, $type = null ) {
$attributes = $this->get_items( $source->attributes, $source, $args, $context, $info, $type );

$connection = Relay::connectionFromArray( $attributes, $args );
$nodes = [];
Expand Down

0 comments on commit fedde04

Please sign in to comment.