Files
wp-graphql-woocommerce/includes/connection/class-product-attributes.php
T
Geoff TaylorandGitHub 22b03e42b4 fix: "Product" interfaces shared fields fix (#901)
* fix: "Product" interface applied to all sub Product interfaces for more optimal schema shape

* fix: more inconsistencies fixed.

* feat: "ProductWithAttributes" interface radded to "ProductWithVariations" interface
2024-11-06 11:35:14 -05:00

92 lines
2.5 KiB
PHP

<?php
/**
* Connection type - ProductAttributes
*
* Registers connections to ProductAttribute
*
* @package WPGraphQL\WooCommerce\Connection
* @since 0.0.1
*/
namespace WPGraphQL\WooCommerce\Connection;
use GraphQL\Error\Error;
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.
*
* @return void
*/
public static function register_connections() {
// From Product to LocalProductAttribute.
register_graphql_connection(
self::get_connection_config(
[
'fromType' => 'Product',
'toType' => 'LocalProductAttribute',
'fromFieldName' => 'localAttributes',
'connectionArgs' => [],
]
)
);
// From Product to GlobalProductAttribute.
register_graphql_connection(
self::get_connection_config(
[
'fromType' => 'Product',
'toType' => 'GlobalProductAttribute',
'fromFieldName' => 'globalAttributes',
'connectionArgs' => [],
]
)
);
}
/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults.
*
* @param array $args - Connection configuration.
* @throws \GraphQL\Error\Error If the "fromType" or "toType" is not provided.
*
* @return array
*/
public static function get_connection_config( $args = [] ): array {
if ( ! isset( $args['fromType'] ) ) {
throw new Error( __( 'The "fromType" is required for the ProductAttributes connection.', 'wp-graphql-woocommerce' ) );
}
if ( ! isset( $args['toType'] ) ) {
throw new Error( __( 'The "toType" is required for the ProductAttributes connection.', 'wp-graphql-woocommerce' ) );
}
return array_merge(
[
'fromFieldName' => 'attributes',
'connectionArgs' => [],
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Product_Attribute_Connection_Resolver();
// 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
);
}
}