type = GraphQL::type($baseType->getName()); } /** * Rename the relation to something else. */ public function renameTo(string $name): static { $this->field = $name; return $this; } /** * Mark the relation as not nullable. */ public function notNullable(): static { $this->nullable = false; return $this; } /** * Get the name used to query through the type. * By default, the relation name is used. */ public function getName(): string { return $this->field ?? $this->relationName; } /** * Get the relation name in the model. */ public function getRelationName(): string { return $this->relationName; } /** * Resolve the arguments of the sub-query. * * @return Argument[] */ protected function arguments(): array { $arguments = []; $type = $this->baseType; if ($this->paginationType() !== PaginationType::NONE && $type instanceof BaseType) { $arguments[] = $this->resolveFilterArguments($type->fieldClasses()); } if ($this->paginationType() !== PaginationType::NONE) { $arguments[] = new FirstArgument(true); $arguments[] = new PageArgument(); if ($type instanceof BaseType) { $arguments[] = new SortArgument($type); } } if ($type instanceof BaseType && in_array(new DeletedAtField(), $type->fieldClasses())) { $arguments[] = new TrashedArgument(); } return Arr::flatten($arguments); } /** * @return array> */ public function args(): array { return collect($this->arguments()) ->mapWithKeys(fn (Argument $argument): array => [ $argument->name => [ 'name' => $argument->name, 'type' => $argument->getType(), ...(is_null($argument->getDefaultValue()) ? [] : ['defaultValue' => $argument->getDefaultValue()]), ], ]) ->toArray(); } public function getBaseType(): BaseType|BaseUnion { return $this->baseType; } /** * Resolve the relation. * * @param array $args */ public function resolve(Model $root, array $args): mixed { /** @var Collection $collection */ $collection = $root->{$this->getRelationName()}; $first = Arr::get($args, 'first'); $page = Arr::get($args, 'page'); return new LengthAwarePaginator( $collection->forPage($page, $first), $collection->count(), $first, $page ); } abstract public function type(): Type; /** * The pagination type. */ abstract public function paginationType(): PaginationType; }