Files
wp-graphql-woocommerce/includes/connection/class-variation-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

67 lines
1.7 KiB
PHP

<?php
/**
* Connection type - VariationAttributes
*
* Registers connections to VariationAttribute
*
* @package WPGraphQL\WooCommerce\Connection
* @since 0.0.4
*/
namespace WPGraphQL\WooCommerce\Connection;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Connection\Variation_Attribute_Connection_Resolver;
/**
* Class Product_Attributes
*/
class Variation_Attributes {
/**
* Registers the various connections from other Types to VariationAttribute
*/
public static function register_connections() {
// From ProductVariation.
register_graphql_connection( self::get_connection_config() );
// From product types.
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
foreach ( $product_types as $product_type ) {
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => $product_type,
'fromFieldName' => 'defaultAttributes',
)
)
);
}
}
/**
* 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' => 'ProductVariation',
'toType' => 'VariationAttribute',
'fromFieldName' => 'attributes',
'connectionArgs' => array(),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Variation_Attribute_Connection_Resolver();
return $resolver->resolve( $source, $args, $context, $info );
},
),
$args
);
}
}