Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Product type #7

Merged
merged 8 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,14 @@ install_db() {

# create database
RESULT=`mysql -u $DB_USER --password="$DB_PASS" --skip-column-names -e "SHOW DATABASES LIKE '$DB_NAME'"$EXTRA`
if [ "$RESULT" != $DB_NAME ]; then
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
fi

RESULT_2=`mysql -u $DB_USER --password="$DB_PASS" --skip-column-names -e "SHOW DATABASES LIKE '$DB_SERVE_NAME'"$EXTRA`
if [ "$RESULT_2" != $DB_SERVE_NAME ]; then
mysqladmin create $DB_SERVE_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
fi
if [ "$RESULT" != $DB_NAME ]; then
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
fi
}

configure_wordpress() {
cd $WP_CORE_DIR
wp config create --dbname="$DB_SERVE_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASS" --dbhost="$DB_HOST" --skip-check --force=true
wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASS" --dbhost="$DB_HOST" --skip-check --force=true
wp core install --url=wp.test --title="WPGraphQL WooCommerce Tests" --admin_user=admin --admin_password=password [email protected]
wp rewrite structure '/%year%/%monthnum%/%postname%/'
}
Expand All @@ -172,17 +167,21 @@ setup_woocommerce() {
wp plugin install wordpress-importer --activate
echo "Installing & Activating WooCommerce"
wp plugin install woocommerce --activate
wp import $WP_CORE_DIR/wp-content/plugins/woocommerce/sample-data/sample_products.xml --authors=skip
wp import $WP_CORE_DIR/wp-content/plugins/woocommerce/sample-data/sample_products.xml --authors=$PLUGIN_DIR/bin/usermap.csv --path=$WP_CORE_DIR
}

setup_wpgraphql() {
echo "Cloning WPGraphQL"
git clone https://github.com/wp-graphql/wp-graphql.git $WP_CORE_DIR/wp-content/plugins/wp-graphql
if [ ! -d $WP_CORE_DIR/wp-content/plugins/wp-graphql ]; then
echo "Cloning WPGraphQL"
git clone https://github.com/wp-graphql/wp-graphql.git $WP_CORE_DIR/wp-content/plugins/wp-graphql
fi
echo "Activating WPGraphQL"
wp plugin activate wp-graphql

echo "Cloning WPGraphQL-JWT-Authentication"
git clone https://github.com/wp-graphql/wp-graphql-jwt-authentication.git $WP_CORE_DIR/wp-content/plugins/wp-graphql-jwt-authentication
if [ ! -d $WP_CORE_DIR/wp-content/plugins/wp-graphql-jwt-authentication ]; then
echo "Cloning WPGraphQL-JWT-Authentication"
git clone https://github.com/wp-graphql/wp-graphql-jwt-authentication.git $WP_CORE_DIR/wp-content/plugins/wp-graphql-jwt-authentication
fi
echo "Activating WPGraphQL-JWT-Authentication"
wp plugin activate wp-graphql-jwt-authentication
}
Expand Down
2 changes: 2 additions & 0 deletions bin/usermap.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
old_user_login,new_user_login
ryanr14,admin
20 changes: 20 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
commands:
- Codeception\Command\GenerateWPUnit
- Codeception\Command\GenerateWPRestApi
- Codeception\Command\GenerateWPRestController
- Codeception\Command\GenerateWPRestPostTypeController
- Codeception\Command\GenerateWPAjax
- Codeception\Command\GenerateWPCanonical
- Codeception\Command\GenerateWPXMLRPC
params:
- .env
4 changes: 4 additions & 0 deletions src/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ public static function load() {
*/
add_action( 'graphql_register_types', [ '\WPGraphQL\Extensions\WooCommerce\Type\Object\Coupon', 'register' ], 10 );
add_action( 'graphql_register_types', [ '\WPGraphQL\Extensions\WooCommerce\Connection\Coupons', 'register_connections' ], 10 );
add_action( 'graphql_register_types', [ '\WPGraphQL\Extensions\WooCommerce\Type\Object\Product', 'register' ], 10 );
add_action( 'graphql_register_types', [ '\WPGraphQL\Extensions\WooCommerce\Connection\Products', 'register_connections' ], 10 );
add_action( 'graphql_register_types', [ '\WPGraphQL\Extensions\WooCommerce\Connection\ProductCategories', 'register_connections' ], 10 );
add_action( 'graphql_register_types', [ '\WPGraphQL\Extensions\WooCommerce\Connection\ProductTags', 'register_connections' ], 10 );
}
}
57 changes: 57 additions & 0 deletions src/Connection/ProductCategories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace WPGraphQL\Extensions\WooCommerce\Connection;

use WPGraphQL\Extensions\WooCommerce\Data\Factory;

/**
* Class ProductCategories
*
* This class organizes the registration of connections to ProductCategories
*
* @package WPGraphQL\Connection
*/
class ProductCategories {

/**
* Registers the various connections from other Types to Coupons
*/
public static function register_connections() {
/**
* Type connections
*/
register_graphql_connection( self::get_connection_config() );
register_graphql_connection( self::get_connection_config( [
'fromType' => 'Coupon',
'fromFieldName' => 'productCategories',
] ) );
register_graphql_connection( self::get_connection_config( [
'fromType' => 'Coupon',
'fromFieldName' => 'excludedProductCategories',
] ) );
}

/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults
*
* @access public
* @param array $args
*
* @return array
*/
public static function get_connection_config( $args = [] ) {
$defaults = [
'fromType' => 'Product',
'toType' => 'ProductCategory',
'fromFieldName' => 'categories',
'connectionArgs' => [],
'resolve' => function ( $root, $args, $context, $info ) {
return Factory::resolve_product_category_connection( $root, $args, $context, $info );
},
];

return array_merge( $defaults, $args );
}

}
49 changes: 49 additions & 0 deletions src/Connection/ProductTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace WPGraphQL\Extensions\WooCommerce\Connection;

use WPGraphQL\Extensions\WooCommerce\Data\Factory;

/**
* Class ProductTags
*
* This class organizes the registration of connections to ProductTags
*
* @package WPGraphQL\Connection
*/
class ProductTags {

/**
* Registers the various connections from other Types to Coupons
*/
public static function register_connections() {
/**
* Type connections
*/
register_graphql_connection( self::get_connection_config() );
}

/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults
*
* @access public
* @param array $args
*
* @return array
*/
public static function get_connection_config( $args = [] ) {
$defaults = [
'fromType' => 'Product',
'toType' => 'ProductTag',
'fromFieldName' => 'tags',
'connectionArgs' => [],
'resolve' => function ( $root, $args, $context, $info ) {
return Factory::resolve_product_tag_connection( $root, $args, $context, $info );
},
];

return array_merge( $defaults, $args );
}

}
96 changes: 96 additions & 0 deletions src/Connection/Products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace WPGraphQL\Extensions\WooCommerce\Connection;

use WPGraphQL\Extensions\WooCommerce\Data\Factory;

/**
* Class Products
*
* This class organizes the registration of connections to Coupons
*
* @package WPGraphQL\Connection
*/
class Products {
/**
* Registers the various connections from other Types to Coupons
*/
public static function register_connections() {
/**
* Root connections
*/
register_graphql_connection( self::get_connection_config() );

/**
* Taxonomy connections
*/
register_graphql_connection( self::get_connection_config( [
'fromType' => 'productTag'
] ) );
register_graphql_connection( self::get_connection_config( [
'fromType' => 'productCategory'
] ) );

/**
* Type connections
*/
register_graphql_connection( self::get_connection_config( [
'fromType' => 'Product',
'fromFieldName' => 'upsell'
] ) );
register_graphql_connection( self::get_connection_config( [
'fromType' => 'Product',
'fromFieldName' => 'crossSell'
] ) );
register_graphql_connection( self::get_connection_config( [
'fromType' => 'Product',
'fromFieldName' => 'variations'
] ) );
register_graphql_connection( self::get_connection_config( [
'fromType' => 'Coupon',
'fromFieldName' => 'products'
] ) );
register_graphql_connection( self::get_connection_config( [
'fromType' => 'Coupon',
'fromFieldName' => 'excludedProducts'
] ) );
}

/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults
*
* @access public
* @param array $args
*
* @return array
*/
public static function get_connection_config( $args = [] ) {
$defaults = [
'fromType' => 'RootQuery',
'toType' => 'Product',
'fromFieldName' => 'products',
'connectionArgs' => self::get_connection_args(),
'resolve' => function ( $root, $args, $context, $info ) {
return Factory::resolve_product_connection( $root, $args, $context, $info );
},
];

return array_merge( $defaults, $args );
}

/**
* This returns the connection args for the Product connection
*
* @access public
* @return array
*/
public static function get_connection_args() {
return [
'slug' => [
'type' => 'String',
'description' => __( 'Product slug', 'wp-graphql-woocommerce' ),
]
];
}
}
35 changes: 34 additions & 1 deletion src/Data/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,40 @@ public static function resolve_coupon( $id ) {
public static function resolve_coupon_connection( $source, array $args, $context, ResolveInfo $info ) {
$resolver = new CouponConnectionResolver();
return $resolver->resolve( $source, $args, $context, $info );
}

/**
* Returns the product for the ID
*
* @param int $id ID of the product being retrieved
*
* @throws UserError
* @since 0.0.1
* @return \WP_Product
* @access public
*/
public static function resolve_product( $id ) {
$product = new \WC_Product( $id );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kidunot89 Is the idea to create a product implementation for each type of products? WC_Product_Simple, WC_Product_Variation, etc?

If yes, can we do something to verify the WC_Product and classes that extend WC_Abstract_Legacy_Product?

That would allow WC_Product_Subscription_Variation, WC_Product_Subscription, and other product types to work without having to create their own product implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve_product is only used in the root queries product and productBy, however I will add the ability to filter the resolver of the Product type, if this does not work for other Woo extensions

if ( empty( $product ) ) {
throw new UserError( sprintf( __( 'No product was found with the ID: %1$s', 'wp-graphql-woocommerce' ), $id ) );
}

return $product;
}

public static function resolve_product_connection( $source, array $args, $context, ResolveInfo $info ) {
$resolver = new ProductConnectionResolver();
return $resolver->resolve( $source, $args, $context, $info );
}

}
public static function resolve_product_category_connection ( $source, array $args, $context, ResolveInfo $info ) {
$resolver = new WCTermConnectionResolver( 'product_cat' );
return $resolver->resolve( $source, $args, $context, $info );
}

public static function resolve_product_tag_connection ( $source, array $args, $context, ResolveInfo $info ) {
$resolver = new WCTermConnectionResolver( 'product_tag' );
return $resolver->resolve( $source, $args, $context, $info );
}

}
Loading