@@ -33,6 +33,11 @@ class Core_Schema_Filters {
3333 * Register filters
3434 */
3535 public static function add_filters () {
36+ // Registers WooCommerce CPTs.
37+ add_filter ( 'register_post_type_args ' , array ( __CLASS__ , 'register_post_types ' ), 10 , 2 );
38+ add_filter ( 'graphql_post_entities_allowed_post_types ' , array ( __CLASS__ , 'skip_type_registry ' ), 10 );
39+ add_filter ( 'graphql_union_resolve_type ' , array ( __CLASS__ , 'graphql_union_resolve_type ' ), 10 , 3 );
40+
3641 // Registers WooCommerce taxonomies.
3742 add_filter ( 'register_taxonomy_args ' , array ( __CLASS__ , 'register_taxonomy_args ' ), 10 , 2 );
3843
@@ -102,10 +107,72 @@ public static function customer_loader( $context ) {
102107 return self ::$ customer_loader ;
103108 }
104109
110+ /**
111+ * Registers WooCommerce post-types to be used in GraphQL schema
112+ *
113+ * @param array $args - allowed post-types.
114+ * @param string $post_type - name of taxonomy being checked.
115+ *
116+ * @return array
117+ */
118+ public static function register_post_types ( $ args , $ post_type ) {
119+ if ( 'product ' === $ post_type ) {
120+ $ args ['show_in_graphql ' ] = true ;
121+ $ args ['graphql_single_name ' ] = 'Product ' ;
122+ $ args ['graphql_plural_name ' ] = 'Products ' ;
123+ $ args ['skip_graphql_type_registry ' ] = true ;
124+ }
125+ if ( 'product_variation ' === $ post_type ) {
126+ $ args ['show_in_graphql ' ] = true ;
127+ $ args ['graphql_single_name ' ] = 'ProductVariation ' ;
128+ $ args ['graphql_plural_name ' ] = 'ProductVariations ' ;
129+ $ args ['skip_graphql_type_registry ' ] = true ;
130+ }
131+ if ( 'shop_coupon ' === $ post_type ) {
132+ $ args ['show_in_graphql ' ] = true ;
133+ $ args ['graphql_single_name ' ] = 'Coupon ' ;
134+ $ args ['graphql_plural_name ' ] = 'Coupons ' ;
135+ $ args ['skip_graphql_type_registry ' ] = true ;
136+ }
137+ if ( 'shop_order ' === $ post_type ) {
138+ $ args ['show_in_graphql ' ] = true ;
139+ $ args ['graphql_single_name ' ] = 'Order ' ;
140+ $ args ['graphql_plural_name ' ] = 'Orders ' ;
141+ $ args ['skip_graphql_type_registry ' ] = true ;
142+ }
143+ if ( 'shop_order_refund ' === $ post_type ) {
144+ $ args ['show_in_graphql ' ] = true ;
145+ $ args ['graphql_single_name ' ] = 'Refund ' ;
146+ $ args ['graphql_plural_name ' ] = 'Refunds ' ;
147+ $ args ['skip_graphql_type_registry ' ] = true ;
148+ }
149+
150+ return $ args ;
151+ }
152+
153+ /**
154+ * Filters "allowed_post_types" and removed Woocommerce CPTs.
155+ *
156+ * @param array $post_types Post types registered in GraphQL schema.
157+ *
158+ * @return array
159+ */
160+ public static function skip_type_registry ( $ post_types ) {
161+ return array_diff (
162+ $ post_types ,
163+ get_post_types (
164+ [
165+ 'show_in_graphql ' => true ,
166+ 'skip_graphql_type_registry ' => true ,
167+ ]
168+ )
169+ );
170+ }
171+
105172 /**
106173 * Registers WooCommerce taxonomies to be used in GraphQL schema
107174 *
108- * @param array $args - allowed post-types .
175+ * @param array $args - allowed taxonomies .
109176 * @param string $taxonomy - name of taxonomy being checked.
110177 *
111178 * @return array
0 commit comments