php – filter query on multiple fields in single query-ThrowExceptions

Exception or error:

My setup is Symfony 5 with the latest API-Platform version running on PHP 7.3.
So I would like to be able to query both on name and username (maybe even email).
Do I need to write a custom resolver?

This is what I’ve tried so far but this results in a WHERE name = $name AND username = $name.

query SearchUsers ($name: String!) {
  users(name: $name, username: $name) {
    edges {
       cursor
       node {
         id
         username
         email
         avatar
       }
     }
  }
}

My entity:

/**
 * @ApiResource
 * @ApiFilter(SearchFilter::class, properties={
 *   "name": "ipartial",
 *   "username": "ipartial",
 *   "email": "ipartial",
 * })
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="Domain\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class User
{
  private $name;
  private $username;
  private $email;
  // ... code omitted ...
}
How to solve:

The OR condition in the search filter is not handled by default in API Platform, you need a custom filter to do this (https://api-platform.com/docs/core/filters/#creating-custom-filters).

See also: https://github.com/api-platform/core/issues/2400.

Leave a Reply

Your email address will not be published. Required fields are marked *