Skip to content

Commit 01da289

Browse files
ArnoudThibautdunglas
authored andcommitted
[graphql] Add graphql filter with multiple values (#2070)
* add falling test for filter with multiple values * update graphql filter to support multiple values * replace array prefix by _list suffix
1 parent 681e4b5 commit 01da289

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

features/graphql/filters.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,28 @@ Feature: Collections filtering
154154
And the header "Content-Type" should be equal to "application/json"
155155
And the JSON node "data.dummies.edges[0].node.name" should be equal to "Dummy #2"
156156
And the JSON node "data.dummies.edges[1].node.name" should be equal to "Dummy #1"
157+
158+
@createSchema
159+
Scenario: Retrieve a collection filtered using the related search filter with two values and exact strategy
160+
Given there are 3 dummy objects with relatedDummy
161+
When I send the following GraphQL request:
162+
"""
163+
{
164+
dummies(relatedDummy_name_list: ["RelatedDummy #1", "RelatedDummy #2"]) {
165+
edges {
166+
node {
167+
id
168+
name
169+
relatedDummy {
170+
name
171+
}
172+
}
173+
}
174+
}
175+
}
176+
"""
177+
Then the response status code should be 200
178+
And the header "Content-Type" should be equal to "application/json"
179+
And the JSON node "data.dummies.edges" should have 2 element
180+
And the JSON node "data.dummies.edges[0].node.relatedDummy.name" should be equal to "RelatedDummy #1"
181+
And the JSON node "data.dummies.edges[1].node.relatedDummy.name" should be equal to "RelatedDummy #2"

src/GraphQl/Resolver/Factory/CollectionResolverFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,16 @@ private function getSubresource(string $rootClass, array $rootResolvedFields, ar
162162
private function getNormalizedFilters(array $args): array
163163
{
164164
$filters = $args;
165+
165166
foreach ($filters as $name => $value) {
166167
if (\is_array($value)) {
168+
if (strpos($name, '_list')) {
169+
$name = substr($name, 0, \strlen($name) - \strlen('_list'));
170+
}
167171
$filters[$name] = $this->getNormalizedFilters($value);
168-
continue;
169172
}
170173

171-
if (strpos($name, '_')) {
174+
if (\is_string($name) && strpos($name, '_')) {
172175
// Gives a chance to relations/nested fields.
173176
$filters[str_replace('_', '.', $name)] = $value;
174177
}

src/GraphQl/Type/SchemaBuilder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,15 @@ private function getResourceFieldConfiguration(string $resourceClass, ResourceMe
231231
$filterType = \in_array($value['type'], Type::$builtinTypes, true) ? new Type($value['type'], $nullable) : new Type('object', $nullable, $value['type']);
232232
$graphqlFilterType = $this->convertType($filterType, false, null, $depth);
233233

234-
if ('[]' === $newKey = substr($key, -2)) {
235-
$key = $newKey;
234+
if ('[]' === substr($key, -2)) {
236235
$graphqlFilterType = GraphQLType::listOf($graphqlFilterType);
236+
$key = substr($key, 0, -2).'_list';
237237
}
238238

239239
parse_str($key, $parsed);
240+
if (array_key_exists($key, $parsed) && \is_array($parsed[$key])) {
241+
$parsed = [$key => ''];
242+
}
240243
array_walk_recursive($parsed, function (&$value) use ($graphqlFilterType) {
241244
$value = $graphqlFilterType;
242245
});

0 commit comments

Comments
 (0)