scrnhq /
laravel-bakery
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bakery\Mutations; |
||
| 4 | |||
| 5 | use Bakery\Utils\Utils; |
||
| 6 | use Illuminate\Support\Str; |
||
| 7 | use Bakery\Eloquent\ModelSchema; |
||
| 8 | use Bakery\Support\TypeRegistry; |
||
| 9 | use Bakery\Types\Definitions\RootType; |
||
| 10 | |||
| 11 | abstract class EloquentMutation extends Mutation |
||
| 12 | { |
||
| 13 | use Concerns\QueriesModel; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var \Bakery\Eloquent\ModelSchema |
||
| 17 | */ |
||
| 18 | protected $modelSchema; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var \Illuminate\Database\Eloquent\Model |
||
| 22 | */ |
||
| 23 | protected $model; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * EloquentMutation constructor. |
||
| 27 | * |
||
| 28 | * @param \Bakery\Support\TypeRegistry $registry |
||
| 29 | * @param \Bakery\Eloquent\ModelSchema $modelSchema |
||
| 30 | */ |
||
| 31 | public function __construct(TypeRegistry $registry, ModelSchema $modelSchema = null) |
||
| 32 | { |
||
| 33 | parent::__construct($registry); |
||
| 34 | |||
| 35 | if ($modelSchema) { |
||
| 36 | $this->modelSchema = $modelSchema; |
||
| 37 | } elseif (is_string($this->modelSchema)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 38 | $this->modelSchema = $this->registry->getModelSchema($this->modelSchema); |
||
| 39 | } |
||
| 40 | |||
| 41 | Utils::invariant( |
||
| 42 | $this->modelSchema instanceof ModelSchema, |
||
| 43 | 'Model schema on '.get_class($this).' should be an instance of '.ModelSchema::class |
||
| 44 | ); |
||
| 45 | |||
| 46 | $this->model = $this->modelSchema->getModel(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the name of the Mutation, if no name is specified fall back |
||
| 51 | * on a name based on the class name. |
||
| 52 | * |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | public function name(): string |
||
| 56 | { |
||
| 57 | if (isset($this->name)) { |
||
| 58 | return $this->name; |
||
| 59 | } |
||
| 60 | |||
| 61 | return Str::camel(Str::before(class_basename($this), 'Mutation')); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The type of the Mutation. |
||
| 66 | * |
||
| 67 | * @return RootType |
||
| 68 | */ |
||
| 69 | public function type(): RootType |
||
| 70 | { |
||
| 71 | return $this->registry->type($this->modelSchema->typename())->nullable(false); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The arguments for the Mutation. |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function args(): array |
||
| 80 | { |
||
| 81 | $inputTypeName = Utils::typename($this->name()).'Input'; |
||
| 82 | |||
| 83 | return [ |
||
| 84 | 'input' => $this->registry->type($inputTypeName)->nullable(false), |
||
| 85 | ]; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return \Bakery\Eloquent\ModelSchema |
||
| 90 | */ |
||
| 91 | public function getModelSchema(): ModelSchema |
||
| 92 | { |
||
| 93 | return $this->modelSchema; |
||
| 94 | } |
||
| 95 | } |
||
| 96 |