Skip to content

Commit 76cfbf1

Browse files
committed
Allow GraphQL to filter on nested property
fixes #1714, #1867
1 parent 2e0c647 commit 76cfbf1

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

features/graphql/filters.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,27 @@ Feature: Collections filtering
8888
And the JSON node "data.dummies.edges[1].node.relatedDummies.edges" should have 0 elements
8989
And the JSON node "data.dummies.edges[2].node.relatedDummies.edges" should have 1 element
9090
And the JSON node "data.dummies.edges[2].node.relatedDummies.edges[0].node.name" should be equal to "RelatedDummy13"
91+
92+
@createSchema
93+
Scenario: Retrieve a collection filtered using the related search filter
94+
Given there are 1 dummy objects having each 2 relatedDummies
95+
And there are 1 dummy objects having each 3 relatedDummies
96+
When I add "Accept" header equal to "application/hal+json"
97+
And I send a "GET" request to "/dummies?relatedDummies.name=RelatedDummy31"
98+
Then the response status code should be 200
99+
And the response should be in JSON
100+
And the JSON node "_embedded.item" should have 1 element
101+
When I send the following GraphQL request:
102+
"""
103+
{
104+
dummies(relatedDummies_name: "RelatedDummy31") {
105+
edges {
106+
node {
107+
id
108+
}
109+
}
110+
}
111+
}
112+
"""
113+
And the response status code should be 200
114+
And the JSON node "data.dummies.edges" should have 1 element

src/GraphQl/Resolver/Factory/CollectionResolverFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
7777
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
7878
$dataProviderContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true);
7979
$dataProviderContext['attributes'] = $this->fieldsToAttributes($info);
80-
$dataProviderContext['filters'] = $args;
80+
$filters = $args;
81+
foreach ($filters as $name => $value) {
82+
if (strpos($name, '_')) {
83+
// Gives a chance to relations/nested fields
84+
$filters[str_replace('_', '.', $name)] = $value;
85+
}
86+
}
87+
$dataProviderContext['filters'] = $filters;
8188

8289
if (isset($rootClass, $source[$rootProperty = $info->fieldName], $source[ItemNormalizer::ITEM_KEY])) {
8390
$rootResolvedFields = $this->identifiersExtractor->getIdentifiersFromItem(unserialize($source[ItemNormalizer::ITEM_KEY]));

0 commit comments

Comments
 (0)