ProductRelationResolver.php 899 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace Webkul\BagistoApi\Resolver;
  3. use Webkul\BagistoApi\Models\Product;
  4. /**
  5. * Custom resolver for Product nested relations (upSells, crossSells, relatedProducts, bookingProducts)
  6. * Ensures the relation methods are called instead of querying all products
  7. */
  8. class ProductRelationResolver
  9. {
  10. public function __invoke($object, array $args, $context, $info, $type, $fieldName)
  11. {
  12. if (! $object instanceof Product) {
  13. return null;
  14. }
  15. $relationMap = [
  16. 'upSells' => 'up_sells',
  17. 'crossSells' => 'cross_sells',
  18. 'relatedProducts' => 'related_products',
  19. ];
  20. $relationMethod = $relationMap[$fieldName] ?? null;
  21. if (! $relationMethod || ! method_exists($object, $relationMethod)) {
  22. return null;
  23. }
  24. return $object->{$relationMethod}()->get();
  25. }
  26. }