Files
wp-graphql-woocommerce/includes/connection/class-product-attributes.php
T
a7714d0cd2 Updates to be compatible with WPGraphQL v1.6.1 (#537)
* fix: Huge refactor across schema and unit tests

* - add shipping_method loader in class-core-schema-filters.php

* - Update class-wc-db-loader.php to respect the new SHIPPING_METHOD loader with a `load_shipping_method_from_id()` function

* - Convert Shipping_Method_Connection_Resolver to extend AbstractConnectionResolver

* - Replace resolver for Shipping Methods connection. Remove use of `resolveNode` and replace the resolver with the new connection class `get_connection()` call

* chore: Factory class refactored.

* - update the function that registers Product Types to the Schema to use the value from the `get_enabled_product_types()` as the Type name and the key as the fieldName

* chore: PaymentGatewayQueriesTest refactored.

* chore: WPCS compliance met

* chore: Syntax error fixed.

Co-authored-by: Jason Bahl <jasonbahl@mac.com>
2021-08-11 14:26:09 -04:00

88 lines
2.1 KiB
PHP

<?php
/**
* Connection type - ProductAttributes
*
* Registers connections to ProductAttribute
*
* @package WPGraphQL\WooCommerce\Connection
* @since 0.0.1
*/
namespace WPGraphQL\WooCommerce\Connection;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Connection\Product_Attribute_Connection_Resolver;
/**
* Class Product_Attributes
*/
class Product_Attributes {
/**
* Registers the various connections from other Types to ProductAttribute.
*/
public static function register_connections() {
// From Product to ProductAttribute.
register_graphql_connection( self::get_connection_config() );
// From Product to LocalProductAttribute.
register_graphql_connection(
self::get_connection_config(
array(
'toType' => 'LocalProductAttribute',
'fromFieldName' => 'localAttributes',
)
)
);
// From Product to GlobalProductAttribute.
register_graphql_connection(
self::get_connection_config(
array(
'toType' => 'GlobalProductAttribute',
'fromFieldName' => 'globalAttributes',
)
)
);
}
/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults.
*
* @param array $args - Connection configuration.
* @return array
*/
public static function get_connection_config( $args = array() ): array {
return array_merge(
array(
'fromType' => 'Product',
'toType' => 'ProductAttribute',
'fromFieldName' => 'attributes',
'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 );
},
),
$args
);
}
/**
* Returns array of where args.
*
* @return array
*/
public static function get_connection_args(): array {
return array(
'type' => array(
'type' => 'ProductAttributeTypesEnum',
'description' => __( 'Filter results by attribute scope.', 'wp-graphql-woocommerce' ),
),
);
}
}