99
1010use Magento \CatalogGraphQl \Model \ProductDataProvider ;
1111use Magento \Framework \Exception \LocalizedException ;
12+ use Magento \Framework \Exception \NoSuchEntityException ;
1213use Magento \Framework \GraphQl \Config \Element \Field ;
14+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
1315use Magento \Framework \GraphQl \Query \ResolverInterface ;
1416use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
1517use Magento \Sales \Api \Data \OrderItemInterface ;
18+ use Psr \Log \LoggerInterface ;
1619
1720/**
1821 * Fetches the Product data according to the GraphQL schema
@@ -23,9 +26,11 @@ class ProductResolver implements ResolverInterface
2326 * ProductResolver Constructor
2427 *
2528 * @param ProductDataProvider $productDataProvider
29+ * @param LoggerInterface $logger
2630 */
2731 public function __construct (
28- private readonly ProductDataProvider $ productDataProvider
32+ private readonly ProductDataProvider $ productDataProvider ,
33+ private readonly LoggerInterface $ logger
2934 ) {
3035 }
3136
@@ -38,11 +43,21 @@ public function resolve(
3843 ResolveInfo $ info ,
3944 ?array $ value = null ,
4045 ?array $ args = null
41- ) {
42- if (!(($ value ['model ' ] ?? null ) instanceof OrderItemInterface)) {
46+ ): ?array {
47+ $ orderItem = $ value ['model ' ] ?? null ;
48+ if (!$ orderItem instanceof OrderItemInterface) {
4349 throw new LocalizedException (__ ('"model" value should be specified ' ));
4450 }
4551
46- return $ this ->productDataProvider ->getProductDataById ((int )$ value ['model ' ]->getProductId ());
52+ try {
53+ return $ this ->productDataProvider ->getProductDataById ((int )$ orderItem ->getProductId ());
54+ } catch (NoSuchEntityException | GraphQlNoSuchEntityException $ exception ) {
55+ $ this ->logger ->error (
56+ sprintf ('ProductResolver: Product not found. Exception: %s ' , $ exception ->getMessage ()),
57+ ['exception ' => $ exception , 'arguments ' => $ args ]
58+ );
59+
60+ return null ;
61+ }
4762 }
4863}
0 commit comments