Skip to content

Commit 0ec6943

Browse files
committed
feat: "product" and "variation" connections added to LineItem type
1 parent 6dbdcb5 commit 0ec6943

File tree

4 files changed

+101
-64
lines changed

4 files changed

+101
-64
lines changed

includes/type/object/class-order-item-type.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use WPGraphQL\AppContext;
1414
use WPGraphQL\WooCommerce\Data\Factory;
15+
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
1516

1617
/**
1718
* Class Order_Item_Type
@@ -201,26 +202,37 @@ public static function register() {
201202
'type' => 'TaxStatusEnum',
202203
'description' => __( 'Line item\'s taxes', 'wp-graphql-woocommerce' ),
203204
),
204-
'product' => array(
205-
'type' => 'Product',
206-
'description' => 'Line item\'s product object',
207-
'resolve' => function( $item, array $args, AppContext $context ) {
208-
// @codingStandardsIgnoreStart
209-
return ! empty( $item->productId )
210-
? Factory::resolve_crud_object( $item->productId, $context )
211-
: null;
212-
// @codingStandardsIgnoreEnd
205+
),
206+
// Connections.
207+
array(
208+
'product' => array(
209+
'toType' => 'Product',
210+
'oneToOne' => true,
211+
'resolve' => function ( $source, array $args, AppContext $context, $info ) {
212+
$id = $source->productId;
213+
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'product' );
214+
215+
return $resolver
216+
->one_to_one()
217+
->set_query_arg( 'p', $id )
218+
->get_connection();
213219
},
214220
),
215-
'variation' => array(
216-
'type' => 'ProductVariation',
217-
'description' => 'Line item\'s product variation object',
218-
'resolve' => function( $item, array $args, AppContext $context ) {
219-
// @codingStandardsIgnoreStart
220-
return ! empty( $item->variationId )
221-
? Factory::resolve_crud_object( $item->variationId, $context )
222-
: null;
223-
// @codingStandardsIgnoreEnd
221+
'variation' => array(
222+
'toType' => 'ProductVariation',
223+
'oneToOne' => true,
224+
'resolve' => function ( $source, array $args, AppContext $context, $info ) {
225+
$id = $source->variationId;
226+
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'product_variation' );
227+
228+
if ( ! $id ) {
229+
return null;
230+
}
231+
232+
return $resolver
233+
->one_to_one()
234+
->set_query_arg( 'p', $id )
235+
->get_connection();
224236
},
225237
),
226238
),
@@ -234,6 +246,7 @@ public static function register() {
234246
array(
235247
'description' => $config[0],
236248
'fields' => self::get_fields( $config[1] ),
249+
'connections' => ! empty( $config[2] ) ? $config[2] : null,
237250
)
238251
);
239252
}

tests/wpunit/CheckoutMutationTest.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,19 @@ private function checkout( $input, $mutation = null ) {
251251
totalTax
252252
taxStatus
253253
product {
254-
... on SimpleProduct {
255-
id
256-
}
257-
... on VariableProduct {
258-
id
259-
}
254+
node {
255+
... on SimpleProduct {
256+
id
257+
}
258+
... on VariableProduct {
259+
id
260+
}
261+
}
260262
}
261263
variation {
262-
id
264+
node {
265+
id
266+
}
263267
}
264268
}
265269
}
@@ -480,10 +484,12 @@ function( $item ) {
480484
'total' => ! empty( $item->get_total() ) ? $item->get_total() : null,
481485
'totalTax' => ! empty( $item->get_total_tax() ) ? $item->get_total_tax() : null,
482486
'taxStatus' => strtoupper( $item->get_tax_status() ),
483-
'product' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ),
487+
'product' => array( 'node' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ) ),
484488
'variation' => ! empty( $item->get_variation_id() )
485489
? array(
486-
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
490+
'node' => array(
491+
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
492+
),
487493
)
488494
: null,
489495
);
@@ -676,10 +682,12 @@ function( $item ) {
676682
'total' => ! empty( $item->get_total() ) ? $item->get_total() : null,
677683
'totalTax' => ! empty( $item->get_total_tax() ) ? $item->get_total_tax() : null,
678684
'taxStatus' => strtoupper( $item->get_tax_status() ),
679-
'product' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ),
685+
'product' => array( 'node' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ) ),
680686
'variation' => ! empty( $item->get_variation_id() )
681687
? array(
682-
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
688+
'node' => array(
689+
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
690+
),
683691
)
684692
: null,
685693
);
@@ -877,10 +885,12 @@ function( $item ) {
877885
'total' => ! empty( $item->get_total() ) ? $item->get_total() : null,
878886
'totalTax' => ! empty( $item->get_total_tax() ) ? $item->get_total_tax() : null,
879887
'taxStatus' => strtoupper( $item->get_tax_status() ),
880-
'product' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ),
888+
'product' => array( 'node' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ) ),
881889
'variation' => ! empty( $item->get_variation_id() )
882890
? array(
883-
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
891+
'node' => array(
892+
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
893+
),
884894
)
885895
: null,
886896
);
@@ -1059,10 +1069,12 @@ function( $item ) {
10591069
'total' => ! empty( $item->get_total() ) ? $item->get_total() : null,
10601070
'totalTax' => ! empty( $item->get_total_tax() ) ? $item->get_total_tax() : null,
10611071
'taxStatus' => strtoupper( $item->get_tax_status() ),
1062-
'product' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ),
1072+
'product' => array( 'node' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ) ),
10631073
'variation' => ! empty( $item->get_variation_id() )
10641074
? array(
1065-
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
1075+
'node' => array(
1076+
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
1077+
),
10661078
)
10671079
: null,
10681080
);
@@ -1217,15 +1229,17 @@ public function testCheckoutMutationWithStripe() {
12171229
totalTax
12181230
taxStatus
12191231
product {
1220-
... on SimpleProduct {
1221-
id
1222-
}
1223-
... on VariableProduct {
1224-
id
1225-
}
1232+
node {
1233+
... on SimpleProduct {
1234+
id
1235+
}
1236+
... on VariableProduct {
1237+
id
1238+
}
1239+
}
12261240
}
12271241
variation {
1228-
id
1242+
node { id }
12291243
}
12301244
}
12311245
}
@@ -1288,10 +1302,12 @@ function( $item ) {
12881302
'total' => ! empty( $item->get_total() ) ? $item->get_total() : null,
12891303
'totalTax' => ! empty( $item->get_total_tax() ) ? $item->get_total_tax() : null,
12901304
'taxStatus' => strtoupper( $item->get_tax_status() ),
1291-
'product' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ),
1292-
'variation' => ! empty( $item->get_variation_id() )
1305+
'product' => array( 'node' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ) ),
1306+
'variation' => ! empty( $item->get_variation_id() )
12931307
? array(
1294-
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
1308+
'node' => array(
1309+
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
1310+
),
12951311
)
12961312
: null,
12971313
);

tests/wpunit/OrderItemQueriesTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,17 @@ public function testLineItemsQuery() {
257257
}
258258
taxStatus
259259
product {
260-
... on SimpleProduct {
261-
id
262-
}
263-
... on VariableProduct {
264-
id
265-
}
260+
node {
261+
... on SimpleProduct {
262+
id
263+
}
264+
... on VariableProduct {
265+
id
266+
}
267+
}
266268
}
267269
variation {
268-
id
270+
node { id }
269271
}
270272
}
271273
}
@@ -301,12 +303,12 @@ function( $item ) {
301303
$this->expectedField( 'totalTax', $this->maybe( $item->get_total_tax(), self::IS_NULL ) ),
302304
$this->expectedField( 'itemDownloads', null ),
303305
$this->expectedField( 'taxStatus', strtoupper( $item->get_tax_status() ) ),
304-
$this->expectedField( 'product.id', $this->toRelayId( 'product', $item->get_product_id() ) ),
306+
$this->expectedField( 'product.node.id', $this->toRelayId( 'product', $item->get_product_id() ) ),
305307
$this->expectedField(
306-
'variation.id',
308+
'variation.node.id',
307309
! empty( $item->get_variation_id() )
308310
? $this->toRelayId( 'product_variation', $item->get_variation_id() )
309-
: null
311+
: self::IS_NULL
310312
),
311313
)
312314
);

tests/wpunit/OrderMutationsTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,17 @@ private function orderMutation( $input, $operation_name = 'createOrder', $input_
187187
totalTax
188188
taxStatus
189189
product {
190-
... on SimpleProduct {
191-
id
192-
}
193-
... on VariableProduct {
194-
id
195-
}
190+
node {
191+
... on SimpleProduct {
192+
id
193+
}
194+
... on VariableProduct {
195+
id
196+
}
197+
}
196198
}
197199
variation {
198-
id
200+
node { id }
199201
}
200202
}
201203
}
@@ -436,10 +438,12 @@ function( $item ) {
436438
'total' => ! empty( $item->get_total() ) ? $item->get_total() : null,
437439
'totalTax' => ! empty( $item->get_total_tax() ) ? $item->get_total_tax() : null,
438440
'taxStatus' => strtoupper( $item->get_tax_status() ),
439-
'product' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ),
441+
'product' => array( 'node' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ) ),
440442
'variation' => ! empty( $item->get_variation_id() )
441443
? array(
442-
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
444+
'node' => array(
445+
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
446+
),
443447
)
444448
: null,
445449
);
@@ -758,10 +762,12 @@ function( $item ) {
758762
'total' => ! empty( $item->get_total() ) ? $item->get_total() : null,
759763
'totalTax' => ! empty( $item->get_total_tax() ) ? $item->get_total_tax() : null,
760764
'taxStatus' => strtoupper( $item->get_tax_status() ),
761-
'product' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ),
765+
'product' => array( 'node' => array( 'id' => $this->product->to_relay_id( $item->get_product_id() ) ) ),
762766
'variation' => ! empty( $item->get_variation_id() )
763767
? array(
764-
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
768+
'node' => array(
769+
'id' => $this->variation->to_relay_id( $item->get_variation_id() ),
770+
),
765771
)
766772
: null,
767773
);

0 commit comments

Comments
 (0)