core icon indicating copy to clipboard operation
core copied to clipboard

Filters not applied on GraphQL

Open jannes-io opened this issue 6 years ago • 16 comments

API Platform version(s) affected: 2.5.4

Description
Filters are not applied at all when using GraphQL, the arguments are added to the GQL schema but they don't do anything.

How to reproduce
services.yaml

  entity.property_filter:
    autowire: false
    autoconfigure: false
    parent: api_platform.doctrine.orm.search_filter
    arguments: [{ property: 'exact' }]
    tags: [ api_platform.filter ]

entity.yaml

resources:
  App\Component\...:
    ...
    graphql:
      collection_query:
        filters: ['entity.property_filter']

GQL query returns the entire collection without filters:

query {
  entities(property: "value") {
    id
  }
}

Additional Context
It works as intended on REST.

jannes-io avatar May 07 '20 14:05 jannes-io

After some debugging it seems like it is an issue in ResourceMetadata.php on line 301.

    /**
     * Gets an operation attribute, optionally fallback to a resource attribute.
     *
     * @param mixed|null $defaultValue
     */
    private function findOperationAttribute(?array $operations, ?string $operationName, string $key, $defaultValue, bool $resourceFallback)
    {
        if (null !== $operationName && isset($operations[$operationName][$key])) {
            return $operations[$operationName][$key];
        }

        if ($resourceFallback && isset($this->attributes[$key])) {
            return $this->attributes[$key];
        }

        return $defaultValue;
    }

$operations does not have the key collection_query, which is used by GraphQL. While REST uses get, which is a set key. So I'm not sure if this is a configuration issue in my YAML or a bug in this package. If I look at the documentation it looks like this in annotations:

/**
 * @ApiResource(
 *     attributes={
 *         "filters"={"offer.search_filter"}
 *     },
 *     graphql={
 *         "item_query",
 *         "collection_query"={
 *              "filters"={"offer.date_filter"}
 *          },
 *          "delete",
 *          "update",
 *          "create"
 *     }
 * )
 */

Did I convert it wrongly to YAML? If I add $operationName = 'get' as first line, it works as expected btw.

jannes-io avatar May 07 '20 14:05 jannes-io

It should work. I don't see why it doesn't work in your case. Make sure the cache is cleared twice.

alanpoulain avatar May 07 '20 21:05 alanpoulain

Yes cleared cache multiple times, the arguments do pop up in the GQL schema, just don't do anything because of what I showed in my previous comment. The filter apply function is skipped completely because the operation doesn't exist

jannes-io avatar May 07 '20 21:05 jannes-io

The get operation doesn't exist in GraphQL. Could you try to use the attributes key (same level as graphql) to see if it works?

alanpoulain avatar May 08 '20 08:05 alanpoulain

We just tested it with annotations, and then it works fine. Seems like a YAML loader issue? We've always had strange artifacts, like the docs saying that all REST configuration should also work in GraphQL is not the case when using YAML resource files.

We're going to have to convert a lot of YAML to annotations now, maybe that could be something for you all to either look into or to document.

jannes-io avatar May 08 '20 08:05 jannes-io

To answer your question @alanpoulain , it does work on top level attributes so I suppose we could use that too instead of a full conversion to annotations. Thanks for the hint :)

jannes-io avatar May 08 '20 09:05 jannes-io

It's very strange because everything working with annotation should also work with YAML. It's not really documented though. I think there is a problem with how you wrote the YAML but I'm not sure what exactly.

alanpoulain avatar May 08 '20 09:05 alanpoulain

I have posted our exact configuration on #api-platform in Slack. The configuration I posted here just has domain specifics renamed but seems to be the same as far as I can tell.

jannes-io avatar May 08 '20 09:05 jannes-io

You can debug what the problem is in these files:

  • https://github.com/api-platform/core/blob/master/src/Metadata/Property/Factory/ExtractorPropertyMetadataFactory.php
  • https://github.com/api-platform/core/blob/master/src/Metadata/Extractor/YamlExtractor.php

alanpoulain avatar May 08 '20 09:05 alanpoulain

Thanks, I'll have a look at those tonight after work, this issue has put us quite a bit behind schedule in our sprint 😞 .

jannes-io avatar May 08 '20 09:05 jannes-io

any updates?

myshalove avatar May 26 '21 04:05 myshalove

Try to modify your config with the following code:

App\Component\...:
    attributes:
        filters: ['entity.property_filter']

    graphql:
        collection_query:
            filters: [ 'entity.property_filter' ]

It worked for me

myshalove avatar May 26 '21 07:05 myshalove

It's fine to close this at this point. It's been a year. We've been using a forked version where we did specific fixes for our needs.

jannes-io avatar May 26 '21 22:05 jannes-io

graphql: 
      collection_query: 
            filters: [ 'document.search_filter' ]

filters still not working(. YAML files

ilyamon avatar May 19 '22 19:05 ilyamon

graphql: 
      collection_query: 
            filters: [ 'document.search_filter' ]

filters still not working(. YAML files

Same here.

durimjusaj avatar Jun 22 '22 13:06 durimjusaj

<graphql>
    <operation name="collection_query">
        <attribute name="filters">
            <attribute>App\Filters\PointFilter</attribute>
        </attribute>
    </operation>
</graphql>

Same with xml, quite annoying when using Sylius

Mathieu33260 avatar Sep 08 '23 12:09 Mathieu33260