Skip to content

Commit 62437fb

Browse files
committed
Describe how to filter on nested properties with GraphQL
1 parent ca5e0e8 commit 62437fb

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

core/filters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
508508
/**
509509
* @ApiResource
510510
* @ApiFilter(OrderFilter::class, properties={"product.releaseDate"})
511-
* @ApiFilter(SearchFilter::class, properties={"product.name": "exact"})
511+
* @ApiFilter(SearchFilter::class, properties={"product.color": "exact"})
512512
*/
513513
class Offer
514514
{

core/graphql.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,67 @@ class Offer
6868
// ...
6969
}
7070
```
71+
72+
### Filtering on Nested Properties
73+
74+
Unlike for REST, all built-in filters support nested properties using the underscore (`_`) syntax instead of the dot (`.`) syntax, e.g.:
75+
76+
```php
77+
<?php
78+
// api/src/Entity/Offer.php
79+
80+
namespace App\Entity;
81+
82+
use ApiPlatform\Core\Annotation\ApiFilter;
83+
use ApiPlatform\Core\Annotation\ApiResource;
84+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
85+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
86+
87+
/**
88+
* @ApiResource
89+
* @ApiFilter(OrderFilter::class, properties={"product.releaseDate"})
90+
* @ApiFilter(SearchFilter::class, properties={"product.color": "exact"})
91+
*/
92+
class Offer
93+
{
94+
// ...
95+
}
96+
```
97+
98+
99+
The above allows you to find offers by their respective product's color like for the REST Api.
100+
You can then filter using the following syntax:
101+
102+
```graphql
103+
{
104+
offers(product_color: "red") {
105+
edges {
106+
node {
107+
id
108+
product {
109+
name
110+
color
111+
}
112+
}
113+
}
114+
}
115+
}
116+
```
117+
118+
Or order your results like:
119+
120+
```graphql
121+
{
122+
offers(order: {product_releaseDate: "DESC"}) {
123+
edges {
124+
node {
125+
id
126+
product {
127+
name
128+
color
129+
}
130+
}
131+
}
132+
}
133+
}
134+
```

0 commit comments

Comments
 (0)