|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Orm\Filter\ComparisonFilter; |
| 17 | +use ApiPlatform\Doctrine\Orm\Filter\ExactFilter; |
| 18 | +use ApiPlatform\Doctrine\Orm\Filter\PartialSearchFilter; |
| 19 | +use ApiPlatform\Doctrine\Orm\Filter\SortFilter; |
| 20 | +use ApiPlatform\Metadata\ApiResource; |
| 21 | +use ApiPlatform\Metadata\GraphQl\Query; |
| 22 | +use ApiPlatform\Metadata\GraphQl\QueryCollection; |
| 23 | +use ApiPlatform\Metadata\QueryParameter; |
| 24 | +use Doctrine\Common\Collections\ArrayCollection; |
| 25 | +use Doctrine\Common\Collections\Collection; |
| 26 | +use Doctrine\ORM\Mapping as ORM; |
| 27 | +use Symfony\Component\Serializer\Attribute as Serializer; |
| 28 | +use Symfony\Component\TypeInfo\Type\BuiltinType; |
| 29 | +use Symfony\Component\TypeInfo\TypeIdentifier; |
| 30 | + |
| 31 | +/** |
| 32 | + * Parameter-based (QueryParameter) mirror of the DummyCar <-> DummyCarColor relationship, |
| 33 | + * used to prove GraphQL filter-argument parity between the canonical |
| 34 | + * Operation::getParameters() path and the legacy Operation::getFilters() path. |
| 35 | + */ |
| 36 | +#[ApiResource( |
| 37 | + normalizationContext: ['groups' => ['graphql_filtered']], |
| 38 | + graphQlOperations: [ |
| 39 | + new Query(), |
| 40 | + new QueryCollection( |
| 41 | + parameters: [ |
| 42 | + 'name' => new QueryParameter(filter: new ExactFilter()), |
| 43 | + 'colors.prop' => new QueryParameter(filter: new PartialSearchFilter(), property: 'colors.prop'), |
| 44 | + 'colors.price' => new QueryParameter(filter: new ComparisonFilter(new ExactFilter()), property: 'colors.price', nativeType: new BuiltinType(TypeIdentifier::INT)), |
| 45 | + 'order[:property]' => new QueryParameter(filter: new SortFilter()), |
| 46 | + ], |
| 47 | + ), |
| 48 | + ], |
| 49 | +)] |
| 50 | +#[ORM\Entity] |
| 51 | +class GraphQlFilteredResource |
| 52 | +{ |
| 53 | + #[ORM\Id] |
| 54 | + #[ORM\GeneratedValue] |
| 55 | + #[ORM\Column(type: 'integer')] |
| 56 | + #[Serializer\Groups(['graphql_filtered'])] |
| 57 | + private ?int $id = null; |
| 58 | + |
| 59 | + #[ORM\Column(type: 'string')] |
| 60 | + #[Serializer\Groups(['graphql_filtered'])] |
| 61 | + private string $name = ''; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var Collection<int, GraphQlFilteredResourceColor> |
| 65 | + */ |
| 66 | + #[ORM\OneToMany(targetEntity: GraphQlFilteredResourceColor::class, mappedBy: 'resource')] |
| 67 | + #[Serializer\Groups(['graphql_filtered'])] |
| 68 | + private Collection $colors; |
| 69 | + |
| 70 | + public function __construct() |
| 71 | + { |
| 72 | + $this->colors = new ArrayCollection(); |
| 73 | + } |
| 74 | + |
| 75 | + public function getId(): ?int |
| 76 | + { |
| 77 | + return $this->id; |
| 78 | + } |
| 79 | + |
| 80 | + public function getName(): string |
| 81 | + { |
| 82 | + return $this->name; |
| 83 | + } |
| 84 | + |
| 85 | + public function setName(string $name): void |
| 86 | + { |
| 87 | + $this->name = $name; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return Collection<int, GraphQlFilteredResourceColor> |
| 92 | + */ |
| 93 | + public function getColors(): Collection |
| 94 | + { |
| 95 | + return $this->colors; |
| 96 | + } |
| 97 | + |
| 98 | + public function setColors(Collection $colors): void |
| 99 | + { |
| 100 | + $this->colors = $colors; |
| 101 | + } |
| 102 | + |
| 103 | + public function addColor(GraphQlFilteredResourceColor $color): void |
| 104 | + { |
| 105 | + if (!$this->colors->contains($color)) { |
| 106 | + $this->colors->add($color); |
| 107 | + $color->setResource($this); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments