api-platform / api-platform/core

[GraphQL] NumericFilter and RangeFilter can not be defined on the same property

Open
#3,483 5 comments 0 reactions 0 assignees View on GitHub
GraphQL
Dominant language
PHP
Stars
2.6k
Forks
980
Avg merge
2d 4h
Merged PRs (30d)
49

Description

**API Platform version(s) affected**: 2.5.4

**Description**

In my context, i need to enable both RangeFilter and NumericFilter on the same property to have the following filtering option : less than, equals, between and greater than
In such case, the graphql schema will convert both filters in the same way using the property name to identify the filter.

In the following example
```
/**
* @ApiResource()
* @ApiFilter(RangeFilter::class, properties={"age"})
* @ApiFilter(NumericFilter::class, properties={"age"})
*/
class Dummy
{
/** @var int $age */
private $age;

// ...
}
```
The REST collection endpoint enables both filters with the syntax `/dummies?age=18` and `/dummies?age[gt]=18`

But the GraphQL endpoint enables only the `NumericFilter` with the syntax `dummies(age: "18") { ... }`
(as the `RangeFilter` uses the syntax `dummies(age: {gt: "18"})`)

**How to reproduce**
Here a valid entity declaration

```php
id;
}

public function setId($id): void
{
$this->id = $id;
}

/**
* @var integer
* @ApiProperty
* @ORM\Column(type="integer")
*/
private $age;

public function getAge()
{
return $this->age;
}

public function setAge($age): void
{
$this->age = $age;
}
}
```
**Case 1. Both RangeFilter and NumericFilter**

In the entity definition, enabling both the `RangeFilter` and the `NumericFilter` the following graphql query is available
```
{
dummies(age: 18) {
edges {
node {
id age
}
}
}
}
```
And the following syntax:
```
{
dummies(age: {gt:"18"}) {
edges {
node {
id age
}
}
}
}
```
raises the error:
```
{
"errors": [
{
"message": "Field \"dummies\" argument \"age\" requires type Int, found {gt: \"18\"}.",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 2,
"column": 16
}
]
}
]
}
```

**Case 2. Only RangeFilter**

In the entity definition, enabling only the `RangeFilter` (delete the `NumericFilter` filter declaration) the following graphql query is available
```
{
dummies(age: {gt:"18"}) {
edges {
node {
id age
}
}
}
}
```
And the following syntax:
```
{
dummies(age: 18) {
edges {
node {
id age
}
}
}
}
```
raises the error:
```
{
"errors": [
{
"message": "Field \"dummies\" argument \"age\" requires type DummyFilter_age, found 18.",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 2,
"column": 16
}
]
}
]
}
```

**Possible Solution**

The RangeFilter should supports an `eq` (and `neq`) syntax allowing to use, with one filter, all the cases : less than, between, greater than and equal to

**Additional Context**
Note that using the `NumericFilter` a numerical value should be defined but using the `RangeFilter`, a string should be used to define the value.
Should I open another issue ?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.