Skip to content

Describe how to filter on nested properties with GraphQL #472

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 2 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
/**
* @ApiResource
* @ApiFilter(OrderFilter::class, properties={"product.releaseDate"})
* @ApiFilter(SearchFilter::class, properties={"product.name": "exact"})
* @ApiFilter(SearchFilter::class, properties={"product.color": "exact"})
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dunglas the actual example is off.

just bellow it states The above allows you to find offers by their respective product's color: http://localhost:8000/api/offers?product.color=red 

*/
class Offer
{
Expand Down
63 changes: 63 additions & 0 deletions core/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,66 @@ class Offer
// ...
}
```

### Filtering on Nested Properties

Unlike for REST, all built-in filters support nested properties using the underscore (`_`) syntax instead of the dot (`.`) syntax, e.g.:

```php
<?php
// api/src/Entity/Offer.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;

/**
* @ApiResource
* @ApiFilter(OrderFilter::class, properties={"product.releaseDate"})
* @ApiFilter(SearchFilter::class, properties={"product.color": "exact"})
*/
class Offer
{
// ...
}
```

The above allows you to find offers by their respective product's color like for the REST Api.
You can then filter using the following syntax:

```graphql
{
offers(product_color: "red") {
edges {
node {
id
product {
name
color
}
}
}
}
}
```

Or order your results like:

```graphql
{
offers(order: {product_releaseDate: "DESC"}) {
edges {
node {
id
product {
name
color
}
}
}
}
}
```