title: Return collection with custom resolver/data provider on ApiPlatform with GraphQL
body: <p>I work with PHP - Symfony 5. </p>
  
  <p>I would like to return a collection with graphQL on Api Platform but I have a problem : </p>
  
  <pre><code>      "debugMessage": "Service \"App\\DataProvider\\CompaniesCollectionDataProvider\" not found: the container inside \"Symfony\\Component\\DependencyInjection\\Argument\\ServiceLocator\" is a smaller service locator that only knows about the \"App\\Resolver\\CheckRecruiterQueryResolver\", \"App\\Resolver\\ForgotRecruiterQueryResolver\" and \"App\\Resolver\\JobBoard\\FindOffersByCompanyQueryResolver\" services.",
  </code></pre>
  
  <p>First I had created a resolver but looking at the documentation, I realized that it was necessary to make a data provider.</p>
  
  <p>Unfortunately that did not solve the problem, I still have the same error message...</p>
  
  <p>My custom data Provider : </p>
  
  <pre><code>namespace App\DataProvider;
  
  
  use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  
  use App\Entity\Candidate;
  use App\Entity\Company;
  use Doctrine\Persistence\ManagerRegistry;
  
  class CompaniesCollectionDataProvider implements CollectionDataProviderInterface
  {
  
      private $doctrine;
  
      public function __construct(ManagerRegistry $doctrine)
      {
          $this-&gt;doctrine = $doctrine;
      }
  
      public function getCollection
      (string $resourceClass, string $operationName = null, array $context = [])
      {
          $candidateId = $context['filters']['candidate_id'];
  
          $candidate = $this-&gt;doctrine-&gt;getRepository(Candidate::class)
              -&gt;findOneBy(['id' =&gt; $candidateId]);
  
          if(empty($candidate-&gt;getPosition()))
          {
              $companies =  $this-&gt;doctrine-&gt;getRepository(Company::class)
                  -&gt;findByStoreIsNational();
              // return companies where stores related = is_national
  
              // getCollection doit retourner un array
              return $companies;
  
          }
          $companies =  $this-&gt;doctrine-&gt;getRepository(Company::class)
              -&gt;findByStoreIsNational();
  
          return $companies;
      }
  }
  </code></pre>
  
  <p>My entity : </p>
  
  <pre><code>&lt;?php
  
  namespace App\Entity;
  
  use ApiPlatform\Core\Annotation\ApiResource;
  use App\DataProvider\CompaniesCollectionDataProvider;
  
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
  use Gedmo\Mapping\Annotation as Gedmo;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
  /**
   * @ApiResource(graphql={
   *     "search"={
   *         "collection_query"=CompaniesCollectionDataProvider::class,
   *         "args"={
   *             "candidate_id"={"type"="Int!", "description"="Candidate_id"}
   *         }
   *     },
   *     "create", "update", "delete", "collection_query", "item_query"
   * })
   *
   * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
   * @ORM\Entity(repositoryClass="App\Repository\CompanyRepository")
   * @ORM\Table(name="companies")
   * @UniqueEntity("name")
   */
  class Company
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $name;
  
      /**
       * @ORM\OneToMany(targetEntity="App\Entity\Media", mappedBy="company")
       */
      private $logo;
  
      /**
       * @ORM\Column(type="datetime", nullable=true)
       */
      private $createdAt;
  
      /**
       * @ORM\Column(type="datetime", nullable=true)
       */
      private $updatedAt;
  
      /**
       * @ORM\Column(type="datetime", nullable=true)
       */
      private $deletedAt;
  
      /**
       * @ORM\OneToMany(targetEntity="App\Entity\Store", mappedBy="company")
       */
      private $stores;
  
  
      public function __construct()
      {
          $this-&gt;stores = new ArrayCollection();
          $this-&gt;stories = new ArrayCollection();
          $this-&gt;logo = new ArrayCollection();
      }
  
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
  
      public function getName(): ?string
      {
          return $this-&gt;name;
      }
  
      public function setName(string $name): self
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      public function getLogo(): ?string
      {
          return $this-&gt;logo[0];
      }
  
      public function setLogo(string $logo): self
      {
          $this-&gt;logo = $logo;
  
          return $this;
      }
  
      public function getCreatedAt(): ?\DateTimeInterface
      {
          return $this-&gt;createdAt;
      }
  
      public function setCreatedAt(\DateTimeInterface $createdAt): self
      {
          $this-&gt;createdAt = $createdAt;
  
          return $this;
      }
  
      public function getUpdatedAt(): ?\DateTimeInterface
      {
          return $this-&gt;updatedAt;
      }
  
      public function setUpdatedAt(\DateTimeInterface $updatedAt): self
      {
          $this-&gt;updatedAt = $updatedAt;
  
          return $this;
      }
  
      public function getDeletedAt(): ?\DateTimeInterface
      {
          return $this-&gt;deletedAt;
      }
  
      public function setDeletedAt(?\DateTimeInterface $deletedAt): self
      {
          $this-&gt;deletedAt = $deletedAt;
  
          return $this;
      }
  
      /**
       * @return Collection|Store[]
       */
      public function getStores(): Collection
      {
          return $this-&gt;stores;
      }
  
      public function addStore(Store $store): self
      {
          if (!$this-&gt;stores-&gt;contains($store)) {
              $this-&gt;stores[] = $store;
              $store-&gt;setCompany($this);
          }
  
          return $this;
      }
  
      public function removeStore(Store $store): self
      {
          if ($this-&gt;stores-&gt;contains($store)) {
              $this-&gt;stores-&gt;removeElement($store);
              // set the owning side to null (unless already changed)
              if ($store-&gt;getCompany() === $this) {
                  $store-&gt;setCompany(null);
              }
          }
  
          return $this;
      }
  
      public function addLogo(Media $logo): self
      {
          if (!$this-&gt;logo-&gt;contains($logo)) {
              $this-&gt;logo[] = $logo;
              $logo-&gt;setCompany($this);
          }
  
          return $this;
      }
  
      public function removeLogo(Media $logo): self
      {
          if ($this-&gt;logo-&gt;contains($logo)) {
              $this-&gt;logo-&gt;removeElement($logo);
              // set the owning side to null (unless already changed)
              if ($logo-&gt;getCompany() === $this) {
                  $logo-&gt;setCompany(null);
              }
          }
  
          return $this;
      }
  }
  </code></pre>
  
  <p>My repository : </p>
  
  <pre><code>class CompanyRepository extends AbstractRepository
  {
      public function __construct(ManagerRegistry $registry)
      {
          parent::__construct($registry, Company::class);
      }
  
      public function findByStoreIsNational() {
          return $this-&gt;createQueryBuilder('c')
              -&gt;leftJoin('c.stores', 's')
              -&gt;where('s.isNational = true')
              -&gt;getQuery()
              -&gt;getResult();
      }
  </code></pre>
  
  <p>I thank you in advance for your help,</p>
  
  <p>Regards,</p>
  
  <p>Romain</p>