Filters not applied on GraphQL
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.
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.
It should work. I don't see why it doesn't work in your case. Make sure the cache is cleared twice.
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
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?
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.
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 :)
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.
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.
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
Thanks, I'll have a look at those tonight after work, this issue has put us quite a bit behind schedule in our sprint 😞 .
any updates?
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
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.
graphql:
collection_query:
filters: [ 'document.search_filter' ]
filters still not working(. YAML files
graphql: collection_query: filters: [ 'document.search_filter' ]filters still not working(. YAML files
Same here.
<graphql>
<operation name="collection_query">
<attribute name="filters">
<attribute>App\Filters\PointFilter</attribute>
</attribute>
</operation>
</graphql>
Same with xml, quite annoying when using Sylius