2019-03-21 21:43:24 -04:00
|
|
|
<?php
|
2019-03-22 16:36:34 -04:00
|
|
|
/**
|
|
|
|
|
* Connection type - ProductAttributes
|
|
|
|
|
*
|
|
|
|
|
* Registers connections to ProductAttribute
|
|
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Connection
|
2019-03-22 16:36:34 -04:00
|
|
|
* @since 0.0.1
|
|
|
|
|
*/
|
2019-03-21 21:43:24 -04:00
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Connection;
|
2019-03-21 21:43:24 -04:00
|
|
|
|
2024-11-06 11:35:14 -05:00
|
|
|
use GraphQL\Error\Error;
|
2020-02-27 21:30:21 -03:00
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
2021-08-11 14:26:09 -04:00
|
|
|
use WPGraphQL\WooCommerce\Data\Connection\Product_Attribute_Connection_Resolver;
|
2019-03-21 21:43:24 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Product_Attributes
|
|
|
|
|
*/
|
|
|
|
|
class Product_Attributes {
|
|
|
|
|
/**
|
2020-02-27 21:30:21 -03:00
|
|
|
* Registers the various connections from other Types to ProductAttribute.
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-03-21 21:43:24 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_connections() {
|
2026-03-23 14:55:56 -04:00
|
|
|
// From RootQuery to GlobalProductAttribute.
|
2019-12-18 15:11:59 -05:00
|
|
|
register_graphql_connection(
|
|
|
|
|
self::get_connection_config(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2026-03-23 14:55:56 -04:00
|
|
|
'fromType' => 'RootQuery',
|
|
|
|
|
'toType' => 'GlobalProductAttribute',
|
|
|
|
|
'fromFieldName' => 'productAttributes',
|
2023-07-19 22:10:22 +03:00
|
|
|
]
|
2019-12-18 15:11:59 -05:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-23 14:55:56 -04:00
|
|
|
// From ProductCategory to GlobalProductAttribute.
|
2019-12-18 15:11:59 -05:00
|
|
|
register_graphql_connection(
|
|
|
|
|
self::get_connection_config(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2026-03-23 14:55:56 -04:00
|
|
|
'fromType' => 'ProductCategory',
|
|
|
|
|
'toType' => 'GlobalProductAttribute',
|
|
|
|
|
'fromFieldName' => 'productAttributes',
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-12-18 15:11:59 -05:00
|
|
|
)
|
|
|
|
|
);
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given an array of $args, this returns the connection config, merging the provided args
|
2020-02-27 21:30:21 -03:00
|
|
|
* with the defaults.
|
2019-03-21 21:43:24 -04:00
|
|
|
*
|
2019-03-27 11:36:31 -04:00
|
|
|
* @param array $args - Connection configuration.
|
2024-11-06 11:35:14 -05:00
|
|
|
* @throws \GraphQL\Error\Error If the "fromType" or "toType" is not provided.
|
|
|
|
|
*
|
2019-03-21 21:43:24 -04:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
public static function get_connection_config( $args = [] ): array {
|
2024-11-06 11:35:14 -05:00
|
|
|
if ( ! isset( $args['fromType'] ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new Error( __( 'The "fromType" is required for the ProductAttributes connection.', 'graphql-for-ecommerce' ) );
|
2024-11-06 11:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! isset( $args['toType'] ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new Error( __( 'The "toType" is required for the ProductAttributes connection.', 'graphql-for-ecommerce' ) );
|
2024-11-06 11:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-27 21:30:21 -03:00
|
|
|
return array_merge(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-02-27 21:30:21 -03:00
|
|
|
'fromFieldName' => 'attributes',
|
2024-11-06 11:35:14 -05:00
|
|
|
'connectionArgs' => [],
|
2023-07-19 22:56:42 +03:00
|
|
|
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
|
2026-03-23 14:55:56 -04:00
|
|
|
$resolver = new Product_Attribute_Connection_Resolver( $source, $args, $context, $info );
|
|
|
|
|
|
|
|
|
|
return $resolver->get_connection();
|
2020-02-27 21:30:21 -03:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2020-02-27 21:30:21 -03:00
|
|
|
$args
|
2019-03-21 21:43:24 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|