Wp-graphql-woocommerce: Product post_type should be set to `show_in_graphql`

Created on 10 Jun 2019  路  2Comments  路  Source: wp-graphql/wp-graphql-woocommerce

Describe the bug
Currently, the Product Type is registered to the Schema without respect to post_type registry.

Since Products in WooCommerce are Post Types, we should use the Post Type registry to set the post_type to show_in_graphql instead of registering the Product type without connection to the Post Type it represents.

By extending the Post Type registry, other plugins can know what Post Types are exposed to GraphQL.

For example, the WPGraphQL for ACF plugin checks for all post types that are set to show_in_graphql using the following:

get_post_types( 'show_in_graphql' => true ) and the product post type isn't included because the Post Type registry has no knowledge that the Product post_type is exposed to GraphQL.

We should make use of the internal WordPress registries as much as possible and extend them so that other tools in the ecosystem can benefit as much as possible.

bug design notes enhancement failing integration

All 2 comments

Solving to this issue will require changes in both WPGraphQL and WooGraphQL.

WPGraphQL

  • A filter needs to be added here to allow for opting out of the WPGraphQL's default WP_Post schema definition.

WooGraphQL

  • A filter must be add here to add product, product_variation, shop_coupon, shop_order, and shop_order_refund to the allow_post_types.
  • A post field for resolving the WP_Post object must be added to the Coupon, Order, Product, ProductVariation, and Refund models.

I bought the WPGraphQL ACF plugin and was disappointed it didnt work with WooCommerce. This snippet worked for me in most places I needed it. https://gist.github.com/jake-101/5c930e5af129ecd9f003cac9c7f13273

<?php
add_action( 'graphql_register_types', function() {
  register_graphql_field( 'Product', 'tech_specs', [
     'type' => 'String',
     'description' => __( 'tech specs', 'wp-graphql' ),
     'resolve' => function( $post ) {
         $id = $post->ID;
       $specs =      get_field( "tech_specs",$id);
         $enc = strip_tags($specs,"<p>,<br>,<ul>,<li>");
       return $enc;
     }
  ] );
} );
?>
Was this page helpful?
0 / 5 - 0 ratings