Files

83 lines
2.1 KiB
PHP
Raw Permalink Normal View History

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
use GraphQL\Error\Error;
2020-02-27 21:30:21 -03:00
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
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() {
// From RootQuery to GlobalProductAttribute.
2019-12-18 15:11:59 -05:00
register_graphql_connection(
self::get_connection_config(
[
'fromType' => 'RootQuery',
'toType' => 'GlobalProductAttribute',
'fromFieldName' => 'productAttributes',
]
2019-12-18 15:11:59 -05:00
)
);
// From ProductCategory to GlobalProductAttribute.
2019-12-18 15:11:59 -05:00
register_graphql_connection(
self::get_connection_config(
[
'fromType' => 'ProductCategory',
'toType' => 'GlobalProductAttribute',
'fromFieldName' => 'productAttributes',
]
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.
* @throws \GraphQL\Error\Error If the "fromType" or "toType" is not provided.
*
2019-03-21 21:43:24 -04:00
* @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.', 'graphql-for-ecommerce' ) );
}
if ( ! isset( $args['toType'] ) ) {
throw new Error( __( 'The "toType" is required for the ProductAttributes connection.', 'graphql-for-ecommerce' ) );
}
2020-02-27 21:30:21 -03:00
return array_merge(
[
2020-02-27 21:30:21 -03:00
'fromFieldName' => 'attributes',
'connectionArgs' => [],
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Product_Attribute_Connection_Resolver( $source, $args, $context, $info );
return $resolver->get_connection();
2020-02-27 21:30:21 -03:00
},
],
2020-02-27 21:30:21 -03:00
$args
2019-03-21 21:43:24 -04:00
);
}
}