Skip to content

Commit 43d529f

Browse files
committed
"idType" implemented.
1 parent d89f5c3 commit 43d529f

20 files changed

+685
-227
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();
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 resource. 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 resource. 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 resource. 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 resource. 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 resource. 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+
95+
/**
96+
* Returns Enum Value definition.
97+
*
98+
* @param string $value Enumeration value being retrieved.
99+
* @return array
100+
*/
101+
private static function get_value( $value ) {
102+
switch ( $value ) {
103+
case 'slug':
104+
return array(
105+
'name' => 'SLUG',
106+
'value' => 'slug',
107+
'description' => __(
108+
'Identify a resource by the slug. Available to non-hierarchcial Types where the slug is a unique identifier.',
109+
'wp-graphql-woocommerce'
110+
),
111+
);
112+
case 'id':
113+
return array(
114+
'name' => 'ID',
115+
'value' => 'global_id',
116+
'description' => __( 'Identify a resource by the (hashed) Global ID.', 'wp-graphql-woocommerce' ),
117+
);
118+
case 'database_id':
119+
return array(
120+
'name' => 'DATABASE_ID',
121+
'value' => 'database_id',
122+
'description' => __( 'Identify a resource by the Database ID.', 'wp-graphql-woocommerce' ),
123+
);
124+
case 'uri':
125+
return array(
126+
'name' => 'URI',
127+
'value' => 'uri',
128+
'description' => __( 'Identify a resource by the URI.', 'wp-graphql-woocommerce' ),
129+
);
130+
}
131+
}
132+
}

includes/type/interface/class-product.php

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

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-
107+
/**
108+
* DEPRECATED
109+
*
110+
* Will be removed in v0.5.x.
111+
*/
92112
register_graphql_field(
93113
'RootQuery',
94114
'productBy',
95115
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 ) {
116+
'type' => 'Product',
117+
'isDeprecated' => true,
118+
'deprecationReason' => __(
119+
'This query has been deprecation, and will be removed in v0.5.x. Please use "product(id: value, idType: DATABASE_ID|SLUG|SKU)" instead',
120+
'wp-graphql-woocommerce'
121+
),
122+
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
123+
'args' => array(
124+
'id' => array(
125+
'type' => 'ID',
126+
'description' => __( 'Get the product by its global ID', 'wp-graphql-woocommerce' ),
127+
),
128+
'productId' => array(
129+
'type' => 'Int',
130+
'description' => __( 'Get the product by its database ID', 'wp-graphql-woocommerce' ),
131+
),
132+
'slug' => array(
133+
'type' => 'String',
134+
'description' => __( 'Get the product by its slug', 'wp-graphql-woocommerce' ),
135+
),
136+
'sku' => array(
137+
'type' => 'String',
138+
'description' => __( 'Get the product by its sku', 'wp-graphql-woocommerce' ),
139+
),
140+
),
141+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
100142
$product_id = 0;
101143
$id_type = '';
102144
if ( ! empty( $args['id'] ) ) {

0 commit comments

Comments
 (0)