-
-
Notifications
You must be signed in to change notification settings - Fork 935
GraphQL: add support for filters #1633
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| Feature: Collections filtering | ||
| In order to retrieve subsets of collections | ||
| As an API consumer | ||
| I need to be able to set filters | ||
|
|
||
| @createSchema | ||
| @dropSchema | ||
| Scenario: Retrieve a collection filtered using the boolean filter | ||
| Given there is 1 dummy object with dummyBoolean true | ||
| And there is 1 dummy object with dummyBoolean false | ||
| When I send the following GraphQL request: | ||
| """ | ||
| { | ||
| dummies(dummyBoolean: false) { | ||
| edges { | ||
| node { | ||
| id | ||
| dummyBoolean | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| Then the JSON node "data.dummies.edges" should have 1 element | ||
| And the JSON node "data.dummies.edges[0].node.dummyBoolean" should be false | ||
|
|
||
| @createSchema | ||
| @dropSchema | ||
| Scenario: Retrieve a collection filtered using the date filter | ||
| Given there are 3 dummy objects with dummyDate | ||
| When I send the following GraphQL request: | ||
| """ | ||
| { | ||
| dummies(dummyDate: {after: "2015-04-02"}) { | ||
| edges { | ||
| node { | ||
| id | ||
| dummyDate | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| Then the JSON node "data.dummies.edges" should have 1 element | ||
| And the JSON node "data.dummies.edges[0].node.dummyDate" should be equal to "2015-04-02T00:00:00+00:00" | ||
|
|
||
| @createSchema | ||
| @dropSchema | ||
| Scenario: Retrieve a collection filtered using the search filter | ||
| Given there are 10 dummy objects | ||
| When I send the following GraphQL request: | ||
| """ | ||
| { | ||
| dummies(name: "#2") { | ||
| edges { | ||
| node { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| Then the JSON node "data.dummies.edges" should have 1 element | ||
| And the JSON node "data.dummies.edges[0].node.id" should be equal to "/dummies/2" | ||
|
|
||
| @createSchema | ||
| @dropSchema | ||
| Scenario: Retrieve a collection filtered using the search filter | ||
| Given there are 3 dummy objects having each 3 relatedDummies | ||
| When I send the following GraphQL request: | ||
| """ | ||
| { | ||
| dummies { | ||
| edges { | ||
| node { | ||
| id | ||
| relatedDummies(name: "RelatedDummy13") { | ||
| edges { | ||
| node { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| And the JSON node "data.dummies.edges[0].node.relatedDummies.edges" should have 0 elements | ||
| And the JSON node "data.dummies.edges[1].node.relatedDummies.edges" should have 0 elements | ||
| And the JSON node "data.dummies.edges[2].node.relatedDummies.edges" should have 1 element | ||
| And the JSON node "data.dummies.edges[2].node.relatedDummies.edges[0].node.name" should be equal to "RelatedDummy13" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Bridge/Doctrine/Orm/Filter/AbstractContextAwareFilter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter; | ||
|
|
||
| use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
| use Doctrine\ORM\QueryBuilder; | ||
|
|
||
| abstract class AbstractContextAwareFilter extends AbstractFilter implements ContextAwareFilterInterface | ||
| { | ||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) | ||
| { | ||
| if (!isset($context['filters']) || !\is_array($context['filters'])) { | ||
| parent::apply($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| foreach ($context['filters'] as $property => $value) { | ||
| $this->filterProperty($property, $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Bridge/Doctrine/Orm/Filter/ContextAwareFilterInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter; | ||
|
|
||
| use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
| use Doctrine\ORM\QueryBuilder; | ||
|
|
||
| /** | ||
| * Context aware filter. | ||
| * | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| interface ContextAwareFilterInterface extends FilterInterface | ||
| { | ||
| /** | ||
| * Applies the filter. | ||
| */ | ||
| public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dunglas The commented out
$contextinapplyandfilterPropertyis a bit confusing. I see thatAbstractContextAwareFilterextends from this class and overridesapplyintroducingarray $context = [], and then calls$this->filterProperty(..., $context);.How about filterProperty, should it also be overridden in
AbstractContextAwareFilterwith addingarray $context = []?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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