Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Filter does not filter collection by nested relation field in GraphQL #1714

Closed
k911 opened this issue Feb 19, 2018 · 2 comments
Closed

Comments

@k911
Copy link
Contributor

k911 commented Feb 19, 2018

Hi!
I've been recently testing GraphQL endpoint provided by this amazing framework.
So I've encountered on follwing SearchFilter issue.

I've following entities (simplified):
// src/Entity/Article.php

/**
 * @ApiResource(
 *     attributes={
 *         "filters": {"app.article.search_filter", "app.article.group_filter"},
 *         "normalization_context": {"groups": {"ArticleRead"}},
 *     },
 *     graphql={
 *         "query": {
 *             "filters": {"app.article.search_filter"},
 *             "normalization_context": {"groups": {"ArticleRead", "CategoryRead"}},
 *         },
 *     },
 * })
 * @Entity
 */
class Article
{
    /**
     * @ORM\Id
     * @ORM\Column(type="uuid")
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @Groups({"ArticleRead"})
     */
    protected $id;

    ...

    /**
     * @ORM\ManyToOne(targetEntity="Category")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
     *
     * @Assert\NotBlank
     *
     * @Groups({"ArticleRead"})
     */
    protected $category;
}
// src/Entity/Category.php

/**
 * @ApiResource
 * @Entity
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="uuid")
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @Groups({"CategoryRead"})
     */
    protected $id;

    /**
     * @ORM\Column(type="string", unique=true)
     *
     * @Gedmo\Slug(fields={"name"}, separator="-", updatable=true, unique=true)
     *
     * @Groups({"CategoryRead"})
     */
    protected $code;

    /**
     * @ORM\Column(type="string", nullable=false)
     *
     * @Assert\NotBlank
     * @Assert\Length(min="3", max="100")
     *
     * @Groups({"CategoryRead"})
     */
    protected $name;
}
and I've declared following filters (old way):
app.article.search_filter:
        parent: 'api_platform.doctrine.orm.search_filter'
        arguments: [ { category.code: 'exact' } ]
        tags: [ 'api_platform.filter' ]

app.article.group_filter:
        parent: 'api_platform.serializer.group_filter'
        arguments: [ 'group', false, ['CategoryRead'] ]
        tags: [ 'api_platform.filter' ]

Filter works fine using normal REST:
GET /articles?category.code=news&group[]=CategoryRead

but using GraphQL:
GET /graphql?query={articles(category_code:"news"){edges{node{category{code}}}}}

It returns an unfiltered collection:
{
  "data": {
    "articles": {
      "edges": [
        {
          "node": {
            "category": {
              "code": "news"
            }
          }
        },
        {
          "node": {
            "category": {
              "code": "article"
            }
          }
        },
        {
          "node": {
            "category": {
              "code": "announcement"
            }
          }
        }
      ]
   }
}
I've tried also new way via annotation:
// src/Entity/Article.php

/**
 * @ApiFilter(SearchFilter::class, properties={"category.code": "exact"})
 */
class Article {
  ...
}

but the behaviour is exactly the same, in REST context works in GraphQL it does not.
I've checked and filtering works for not nested fields.

Stack:

  • api-platform/core v2.2.1, v2.2.2
  • symfony v4.0.4
  • php 7.1/7.2

Here I provide my test api endpoints, with symfony profiler enabled, so you can see the details, hope it helps:

REST
https://knit-test-api.tk/articles?category.code=news&group%5B%5D=CategoryRead
GraphQL:
https://knit-test-api.tk/graphql?query={articles(category_code:%22news%22){edges{node{category{code}}}}}
Travis CI build with feature tests:
https://travis-ci.org/knit-pk/api-v1-php/jobs/343330287#L708
Git branch (full codebase):
https://github.com/knit-pk/api-v1-php/tree/feature/graphql-filters-test

Thanks for your time.
PS: I appreciate your work very much, and really enjoy developing an API with API Platform!

@lexa-uw
Copy link

lexa-uw commented Mar 1, 2018

+1

mneuhaus pushed a commit to mneuhaus/core that referenced this issue Mar 2, 2018
GraphQL filter properties use a underscore instead of dot to seperate nested fields, which causes some issues like api-platform#1714 where nested filters are not applied. This change replaces the underscore with a dot in the AbstractContextAwareFilter to have one commen syntax further down.

closes api-platform#1714
@k911
Copy link
Contributor Author

k911 commented Mar 2, 2018

Can confirm that #1743 fixes this issue.

antograssiot added a commit to antograssiot/core that referenced this issue Apr 15, 2018
dunglas pushed a commit that referenced this issue Apr 20, 2018
* Allow GraphQL to filter on nested property

fixes #1714, #1867

* Allow ordering on nested property values
teohhanhui pushed a commit to teohhanhui/api-platform-core that referenced this issue May 2, 2018
* Allow GraphQL to filter on nested property

fixes api-platform#1714, api-platform#1867

* Allow ordering on nested property values
teohhanhui pushed a commit to teohhanhui/api-platform-core that referenced this issue May 2, 2018
* Allow GraphQL to filter on nested property

fixes api-platform#1714, api-platform#1867

* Allow ordering on nested property values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants