Attribute.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Webkul\BagistoApi\Models;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Get;
  6. use ApiPlatform\Metadata\GetCollection;
  7. use ApiPlatform\Metadata\GraphQl\Query;
  8. use ApiPlatform\Metadata\GraphQl\QueryCollection;
  9. use GraphQL\Error\UserError;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Model as EloquentModel;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
  14. use Webkul\BagistoApi\State\CursorAwareCollectionProvider;
  15. /**
  16. * Simple Attribute model for GraphQL without TranslatableModel
  17. * This is just for input/output, not for actual database operations
  18. */
  19. #[ApiResource(
  20. shortName: 'Attribute',
  21. description: 'Product attribute resource',
  22. routePrefix: '/api/shop',
  23. graphQlOperations: [
  24. new QueryCollection(provider: CursorAwareCollectionProvider::class),
  25. new Query(resolver: BaseQueryItemResolver::class),
  26. ],
  27. operations: [
  28. new GetCollection(
  29. uriTemplate: '/attributes'
  30. ),
  31. new Get(
  32. uriTemplate: '/attributes/{id}'
  33. ),
  34. ]
  35. )]
  36. class Attribute extends \Webkul\Attribute\Models\Attribute
  37. {
  38. #[ApiProperty(readableLink: true, description: 'Current locale translation')]
  39. public function getTranslation(?string $locale = null, ?bool $withFallback = null): ?Model
  40. {
  41. return $this->translation;
  42. }
  43. public static function boot()
  44. {
  45. parent::boot();
  46. static::creating(function (EloquentModel $model) {
  47. if (static::where('code', $model->code)->exists()) {
  48. throw new UserError(__('bagistoapi::app.graphql.attribute.code-already-exists'));
  49. }
  50. });
  51. }
  52. #[ApiProperty(readableLink: true, writable: false, readable: true)]
  53. public function options(): HasMany
  54. {
  55. return $this->hasMany(AttributeOption::class);
  56. }
  57. /**
  58. * Get the attribute options with support for GraphQL args
  59. * Returns a Closure so API Platform's GraphQL ResourceFieldResolver
  60. * can invoke it with the GraphQL args: ($source, $args, $context).
  61. */
  62. #[ApiProperty(writable: false, readable: true)]
  63. public function getOptions()
  64. {
  65. return function ($source, array $args = [], $context = null) {
  66. if (isset($args['first']) && is_numeric($args['first'])) {
  67. return $this->options()->limit((int) $args['first'])->get();
  68. }
  69. if (empty($args) && $this->relationLoaded('options')) {
  70. return $this->options;
  71. }
  72. return $this->options()->get();
  73. };
  74. }
  75. /**
  76. * API Platform identifier
  77. */
  78. #[ApiProperty(identifier: true, writable: false)]
  79. public function getId(): int
  80. {
  81. return $this->id;
  82. }
  83. }