Skip to content

Commit 041091e

Browse files
authored
Merge pull request #220 from wp-graphql/release/v0.4.1
Release v0.4.1
2 parents d89f5c3 + 8afc1e9 commit 041091e

25 files changed

+808
-273
lines changed

README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Requires PHP: 5.6
77
Requires WooCommerce: 3.0.0
88
Requires WPGraphQL: 0.6.0+
99
Works with WPGraphQL-JWT-Authentication: 0.4.0+
10-
Stable tag: 0.4.0
10+
Stable tag: 0.4.1
1111
License: GPL-3
1212
License URI: https://www.gnu.org/licenses/gpl-3.0.html
1313
Maintained at: https://github.com/wp-graphql/wp-graphql-woocommerce

includes/class-type-registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
3838
\WPGraphQL\WooCommerce\Type\WPEnum\Post_Type_Orderby_Enum::register();
3939
\WPGraphQL\WooCommerce\Type\WPEnum\Products_Orderby_Enum::register();
4040
\WPGraphQL\WooCommerce\Type\WPEnum\Orders_Orderby_Enum::register();
41+
\WPGraphQL\WooCommerce\Type\WPEnum\Id_Type_Enums::register();
4142

4243
// InputObjects.
4344
\WPGraphQL\WooCommerce\Type\WPInputObject\Customer_Address_Input::register();

includes/mutation/class-checkout.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static function get_output_fields() {
9797
return is_user_logged_in() ? new Customer( get_current_user_id() ) : null;
9898
},
9999
),
100-
'result' => array(
100+
'result' => array(
101101
'type' => 'String',
102102
'resolve' => function( $payload ) {
103103
return $payload['result'];
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* Register *IdTypeEnum
4+
*
5+
* @package \WPGraphQL\WooCommerce\Type\WPEnum
6+
* @since 0.0.1
7+
*/
8+
9+
namespace WPGraphQL\WooCommerce\Type\WPEnum;
10+
11+
/**
12+
* Class - Id_Type_Enums
13+
*/
14+
class Id_Type_Enums {
15+
16+
/**
17+
* Register the Enum used for setting the field to identify WC crud objects by
18+
*
19+
* @access public
20+
* @return void
21+
*/
22+
public static function register() {
23+
register_graphql_enum_type(
24+
'CouponIdTypeEnum',
25+
array(
26+
'description' => __( 'The Type of Identifier used to fetch a single Coupon. Default is ID.', 'wp-graphql' ),
27+
'values' => array(
28+
'id' => self::get_value( 'id' ),
29+
'database_id' => self::get_value( 'database_id' ),
30+
'code' => array(
31+
'name' => 'CODE',
32+
'value' => 'code',
33+
'description' => __( 'Coupon code.', 'wp-graphql-woocommerce' ),
34+
),
35+
),
36+
)
37+
);
38+
39+
register_graphql_enum_type(
40+
'OrderIdTypeEnum',
41+
array(
42+
'description' => __( 'The Type of Identifier used to fetch a single Order. Default is ID.', 'wp-graphql' ),
43+
'values' => array(
44+
'id' => self::get_value( 'id' ),
45+
'database_id' => self::get_value( 'database_id' ),
46+
'order_number' => array(
47+
'name' => 'ORDER_NUMBER',
48+
'value' => 'order_number',
49+
'description' => __( 'Order number.', 'wp-graphql-woocommerce' ),
50+
),
51+
),
52+
)
53+
);
54+
55+
register_graphql_enum_type(
56+
'ProductIdTypeEnum',
57+
array(
58+
'description' => __( 'The Type of Identifier used to fetch a single Product. Default is ID.', 'wp-graphql' ),
59+
'values' => array(
60+
'id' => self::get_value( 'id' ),
61+
'database_id' => self::get_value( 'database_id' ),
62+
'slug' => self::get_value( 'slug' ),
63+
'sku' => array(
64+
'name' => 'SKU',
65+
'value' => 'sku',
66+
'description' => __( 'Unique store identifier for product.', 'wp-graphql-woocommerce' ),
67+
),
68+
),
69+
)
70+
);
71+
72+
register_graphql_enum_type(
73+
'ProductVariationIdTypeEnum',
74+
array(
75+
'description' => __( 'The Type of Identifier used to fetch a single ProductVariation. Default is ID.', 'wp-graphql' ),
76+
'values' => array(
77+
'id' => self::get_value( 'id' ),
78+
'database_id' => self::get_value( 'database_id' ),
79+
),
80+
)
81+
);
82+
83+
register_graphql_enum_type(
84+
'RefundIdTypeEnum',
85+
array(
86+
'description' => __( 'The Type of Identifier used to fetch a single Refund. Default is ID.', 'wp-graphql' ),
87+
'values' => array(
88+
'id' => self::get_value( 'id' ),
89+
'database_id' => self::get_value( 'database_id' ),
90+
),
91+
)
92+
);
93+
94+
register_graphql_enum_type(
95+
'ShippingMethodIdTypeEnum',
96+
array(
97+
'description' => __( 'The Type of Identifier used to fetch a single Shipping Method. Default is ID.', 'wp-graphql' ),
98+
'values' => array(
99+
'id' => self::get_value( 'id' ),
100+
'database_id' => self::get_value( 'database_id' ),
101+
),
102+
)
103+
);
104+
}
105+
106+
/**
107+
* Returns Enum Value definition.
108+
*
109+
* @param string $value Enumeration value being retrieved.
110+
* @return array
111+
*/
112+
private static function get_value( $value ) {
113+
switch ( $value ) {
114+
case 'slug':
115+
return array(
116+
'name' => 'SLUG',
117+
'value' => 'slug',
118+
'description' => __(
119+
'Identify a resource by the slug. Available to non-hierarchcial Types where the slug is a unique identifier.',
120+
'wp-graphql-woocommerce'
121+
),
122+
);
123+
case 'id':
124+
return array(
125+
'name' => 'ID',
126+
'value' => 'global_id',
127+
'description' => __( 'Identify a resource by the (hashed) Global ID.', 'wp-graphql-woocommerce' ),
128+
);
129+
case 'database_id':
130+
return array(
131+
'name' => 'DATABASE_ID',
132+
'value' => 'database_id',
133+
'description' => __( 'Identify a resource by the Database ID.', 'wp-graphql-woocommerce' ),
134+
);
135+
case 'uri':
136+
return array(
137+
'name' => 'URI',
138+
'value' => 'uri',
139+
'description' => __( 'Identify a resource by the URI.', 'wp-graphql-woocommerce' ),
140+
);
141+
}
142+
}
143+
}

includes/type/interface/class-product-attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static function get_fields() {
6060
return ! empty( $attribute->get_name() ) ? $attribute->get_name() : null;
6161
},
6262
),
63-
'options' => array(
63+
'options' => array(
6464
'type' => array( 'list_of' => 'String' ),
6565
'description' => __( 'Attribute options', 'wp-graphql-woocommerce' ),
6666
'resolve' => function ( $attribute ) {

includes/type/interface/class-product.php

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,91 @@ public static function register_interface( &$type_registry ) {
5555
'type' => 'Product',
5656
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
5757
'args' => array(
58-
'id' => array(
59-
'type' => array( 'non_null' => 'ID' ),
58+
'id' => array(
59+
'type' => array( 'non_null' => 'ID' ),
60+
'description' => __( 'The ID for identifying the product', 'wp-graphql-woocommerce' ),
61+
),
62+
'idType' => array(
63+
'type' => 'ProductIdTypeEnum',
64+
'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
6065
),
6166
),
6267
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
63-
$id_components = Relay::fromGlobalId( $args['id'] );
64-
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
65-
throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
68+
$id = isset( $args['id'] ) ? $args['id'] : null;
69+
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
70+
71+
$product_id = null;
72+
switch ( $id_type ) {
73+
case 'sku':
74+
$product_id = \wc_get_product_id_by_sku( $id );
75+
break;
76+
case 'slug':
77+
$post = get_page_by_path( $id, OBJECT, 'product' );
78+
$product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
79+
break;
80+
case 'database_id':
81+
$product_id = absint( $id );
82+
break;
83+
case 'global_id':
84+
default:
85+
$id_components = Relay::fromGlobalId( $id );
86+
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
87+
throw new UserError( __( 'The "global ID" is invalid', 'wp-graphql-woocommerce' ) );
88+
}
89+
$product_id = absint( $id_components['id'] );
90+
break;
91+
}
92+
93+
if ( empty( $product_id ) ) {
94+
/* translators: %1$s: ID type, %2$s: ID value */
95+
throw new UserError( sprintf( __( 'No product ID was found corresponding to the %1$s: %2$s' ), $id_type, $id ) );
96+
} elseif ( get_post( $product_id )->post_type !== 'product' ) {
97+
/* translators: %1$s: ID type, %2$s: ID value */
98+
throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s' ), $id_type, $id ) );
6699
}
67-
$product_id = absint( $id_components['id'] );
68-
return Factory::resolve_crud_object( $product_id, $context );
100+
101+
$product = Factory::resolve_crud_object( $product_id, $context );
102+
103+
return $product;
69104
},
70105
)
71106
);
72107

73-
$post_by_args = array(
74-
'id' => array(
75-
'type' => 'ID',
76-
'description' => __( 'Get the product by its global ID', 'wp-graphql-woocommerce' ),
77-
),
78-
'productId' => array(
79-
'type' => 'Int',
80-
'description' => __( 'Get the product by its database ID', 'wp-graphql-woocommerce' ),
81-
),
82-
'slug' => array(
83-
'type' => 'String',
84-
'description' => __( 'Get the product by its slug', 'wp-graphql-woocommerce' ),
85-
),
86-
'sku' => array(
87-
'type' => 'String',
88-
'description' => __( 'Get the product by its sku', 'wp-graphql-woocommerce' ),
89-
),
90-
);
91-
108+
/**
109+
* DEPRECATED
110+
*
111+
* Will be removed in v0.5.x.
112+
*/
92113
register_graphql_field(
93114
'RootQuery',
94115
'productBy',
95116
array(
96-
'type' => 'Product',
97-
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
98-
'args' => $post_by_args,
99-
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
117+
'type' => 'Product',
118+
'isDeprecated' => true,
119+
'deprecationReason' => __(
120+
'This query has been deprecation, and will be removed in v0.5.x. Please use "product(id: value, idType: DATABASE_ID|SLUG|SKU)" instead',
121+
'wp-graphql-woocommerce'
122+
),
123+
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
124+
'args' => array(
125+
'id' => array(
126+
'type' => 'ID',
127+
'description' => __( 'Get the product by its global ID', 'wp-graphql-woocommerce' ),
128+
),
129+
'productId' => array(
130+
'type' => 'Int',
131+
'description' => __( 'Get the product by its database ID', 'wp-graphql-woocommerce' ),
132+
),
133+
'slug' => array(
134+
'type' => 'String',
135+
'description' => __( 'Get the product by its slug', 'wp-graphql-woocommerce' ),
136+
),
137+
'sku' => array(
138+
'type' => 'String',
139+
'description' => __( 'Get the product by its sku', 'wp-graphql-woocommerce' ),
140+
),
141+
),
142+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
100143
$product_id = 0;
101144
$id_type = '';
102145
if ( ! empty( $args['id'] ) ) {

0 commit comments

Comments
 (0)