Core: Graphql: can't filter entity collection by ids

Created on 5 Sep 2018  路  7Comments  路  Source: api-platform/core

Hello!

Looks like I found a bug. If I add filter to the entity this way:

/**
 * @ApiResource()
 * @ApiFilter(SearchFilter::class, properties={"id": "exact"})
 */
class Entity

Then I receive the following schema:
Entity(first: Int, after: String, id: Int, id_list: [Int])

When I apply filter by id, it's ignoring and returns me the full collection.
Also I believe that the id must string like /entities/1 instead of integer.

Or I'm doing something wrong?

GraphQL bug

Most helpful comment

Hello,

This behavior is quite strange but it's caused by the normalizeValues function of the SearchFilter.
With REST operations the values passed to the filter aren't typed and are all strings so there is no problem if you pass to this filter values that are in fact numerical string values.
But with graphQl the values are typed.

For me you have two solutions that depend on your needs :

  • First is to use the NumericFilter but this filter doesn't allow to pass multiple values so you won't have the id_list argument. (I'm working on it)
  • Second is to extends the built-in search filter and override the normalizeValues function in order to not discard values that aren't strings.

All 7 comments

Hi!

I thing I have the same bug here

For context:

Added api-platform to existing project, trying to replace existing custom graphql implementation with api-platform implementation.
When adding a filter to an entity, it works on the REST API but doesn't on the graphql api.

Information recollected:

  • Project works over third party database
  • Simple queries works great.
  • Added a filter to entity with filter service declaration:
# config/services.yaml
services:
    entity.search_filter:
        parent: 'api_platform.doctrine.orm.search_filter'
        arguments: [{entityID: 'exact', anotherEntityID: 'exact'}]
        tags: [{ name: 'api_platform.filter', id: 'entity.search_filter'}]
        #tags:  [ 'api_platform.filter' ]
        autowire: false
        autoconfigure: false
        public: false


namespace App\Entity;

 * @ApiResource(
 *     attributes={
 *          "filters"={"entity.search_filter"}
 *     },
 *     graphql={
 *         "query"={
 *              "filters"={"entity.search_filter"}
 *          },
 *          "delete",
 *          "update",
 *          "create"
 *     }
 * )
 */
class Entity {
  • Filter works perfect with REST API
  • On Graphql API, generated query is the same as a normal query with the default pagination of 30 results
  • Tryed to filter with NumericFilter and works perfect, but can't pass an array of values, I don't know if it's possible
  • If I try to filter with

* @ApiFilter(SearchFilter::class, properties={"entityID": "exact"})

it gives me error:

(1/1) InvalidArgumentException
The filter class "SearchFilter" does not implement "ApiPlatform\Core\Api\FilterInterface".

in ApiFilter.php line 61

I thing is unrelated

Do you need more info? There's a way to make a temporary fix?

Lots of thanks, I thinks it's an amazing project

Hello,

This behavior is quite strange but it's caused by the normalizeValues function of the SearchFilter.
With REST operations the values passed to the filter aren't typed and are all strings so there is no problem if you pass to this filter values that are in fact numerical string values.
But with graphQl the values are typed.

For me you have two solutions that depend on your needs :

  • First is to use the NumericFilter but this filter doesn't allow to pass multiple values so you won't have the id_list argument. (I'm working on it)
  • Second is to extends the built-in search filter and override the normalizeValues function in order to not discard values that aren't strings.

Please do not use the NumericFilter for ids

I think this type conversion should be handled at the GraphQL layer. ~As far as the filters are concerned, all values are strings.~ (Not really true since https://github.com/api-platform/core/pull/1633)

I think the way to fix this is to introduce the IriFilter and deprecate using the SearchFilter for IRIs / ids.

Related: https://github.com/api-platform/core/issues/319

LMK if the issue remains after #2277

LMK if the issue remains after #2277

Possible need of backport on branch 2.2 : https://github.com/api-platform/core/issues/2308

Was this page helpful?
0 / 5 - 0 ratings