Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL: add support for filters #1633

Merged
merged 3 commits into from Jan 18, 2018
Merged

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Jan 8, 2018

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR todo

This is the last missing part for full GraphQL support (and to release the first beta of 2.2): filters support.

This PR allows to use any API Platform filter (builtin or not) both with REST and GraphQL. It comes with a refactoring of the filter subsystem to be decoupled from the HTTP layer. This also means that it's now now easy to add support for other transport layers (e.g. RabbitMQ), and different HTTP abstractions than Symfony's HttpFoundation (e.g. PSR-7).

Example:

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource
 * @ApiFilter(SearchFilter::class, strategy="partial")
 * @ORM\Entity
 */
class Person
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column
     */
    private $name;
}

Then you can run this GraphQL query:

{
  people(name: "Kévin") {
    edges {
      node {
        id
        name
      }
    }
  }
} 

It also works for any level of nested collections.

TODO:

  • Fix unit tests


@createSchema
@dropSchema
Scenario: Retrieve a collection filtered using the date filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Scenario does not correspond to what is done.

@sroze
Copy link
Contributor

sroze commented Jan 9, 2018

Nice!

$filterType = \in_array($value['type'], Type::$builtinTypes, true) ? new Type($value['type'], $nullable) : new Type('object', $nullable, $value['type']);
$graphqlFilterType = $this->convertType($filterType);

if ('[]' === $newKey = substr($key, -2)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be in a dedicated method (in FilterInterface?)? Or maybe a boolean returned by getDescription (like $value['is_property_list']?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, but has it was done this way for historical reason (and we definitely need this code for existing custom filters), I propose to add the new key in a new PR.

@dunglas dunglas merged commit 63d555a into api-platform:master Jan 18, 2018
@dunglas dunglas deleted the graphql-filters branch January 18, 2018 17:19
hoangnd25 pushed a commit to hoangnd25/core that referenced this pull request Feb 23, 2018
*/
abstract protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null);
abstract protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null/*, array $context = []*/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas The commented out $context in apply and filterProperty is a bit confusing. I see that AbstractContextAwareFilter extends from this class and overrides apply introducing array $context = [], and then calls $this->filterProperty(..., $context);.
How about filterProperty, should it also be overridden in AbstractContextAwareFilter with adding array $context = []?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good catch I think it should indeed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @meyerbaptiste reminds us, we need PHP 7.2 for this: https://3v4l.org/ktgkR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants