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

Filters in sort #11

Closed
webard opened this issue Feb 27, 2022 · 6 comments
Closed

Filters in sort #11

webard opened this issue Feb 27, 2022 · 6 comments
Labels
pkg: graphql scope: feat New feature or request wontfix This will not be worked on

Comments

@webard
Copy link

webard commented Feb 27, 2022

Hi,
look at this query:

query {
  categories(
    first: 100
    sort: [
      {
        translation: {
          name: DESC
        }
      }
    ]
    filter: {
      translation: {
        where: {
          language_id: {
            equal: "en-GB"
          }
        }
      }
    }
  ) {
    data {
      id
      translation(
        language_id: "en-GB"
      ) {
        name
      }
    }
  }
}

As you see, I filter product that have language "en-GB" in translations, and sort them by language.name. But my first language in table is "pl", so results are sorted by first language in table.

One of proposal is extend @sortby directive to accept simple "where" condition:

query {
  categories(
    first: 100
    sort: [
      {
        translation: {
          where: {
          language_id: {
            equal: "en-GB"
          }
        }
          name: DESC
        }
      }
    ]
    filter: {
      translation: {
        where: {
          language_id: {
            equal: "en-GB"
          }
        }
      }
    }
  ) {
    data {
      id
      translation(
        language_id: "en-GB"
      ) {
        name
      }
    }
  }
}

or make search in @searchby directive:

query {
  categories(
    first: 100
    sort: [
      {
        translation: {
          name: DESC
        }
      }
    ]
    filter: {
      translation: {
        where: {
          language_id: {
            equal: "en-GB"
            sort: true
          }
        }
      }
    }
  ) {
    data {
      id
      translation(
        language_id: "en-GB"
      ) {
        name
      }
    }
  }
}

What do you think?

@LastDragon-ru
Copy link
Owner

Seems each category has multiple translations? This case, unfortunately, is not yet supported. But I'm not sure that fully understand what are you trying to achieve... So maybe you can provide minimal db structure, graphql schema, and expected results?

@LastDragon-ru LastDragon-ru added the scope: feat New feature or request label Feb 27, 2022
@webard
Copy link
Author

webard commented Feb 27, 2022

Yes, each category has multiple translations. First query from post above generates this SQL query:

select
  *
from
  `categories`
where
  (
    exists (
      select
        *
      from
        `category_translations`
      where
        `categories`.`id` = `category_translations`.`category_id`
        and `category_translations`.`language_id` = 'en-GB'
    )
  )
order by
  (
    select
      `category_translations`.`name`
    from
      `category_translations`
    where
      `categories`.`id` = `category_translations`.`category_id`
    limit
      1
  ) desc
limit
  100 offset 0

As you can see, in WHERE clause are conditions to fetch only en-gb translations, but in ORDER clause there is only joining translations by id, default ordered by primary key. So, if language "en-gb" is not created firstly, there is sorting by first matched row (eg. pl language, or any other).

@LastDragon-ru
Copy link
Owner

In the master branch (and the next major version) you can try to add Condition operator (or better a subclass because it will allow to redefine extra field name) into extra operators for @sortBy (same as for Random) and technically it may work 😀

@webard
Copy link
Author

webard commented Nov 27, 2022

Hello,

so, if I understand correctly, in Condition operator I should detect that in @searchby is used "translations" table/filter, get the language_id value and provide it to @sortby condition?

Ps. you do awesome job, thank you!

@LastDragon-ru
Copy link
Owner

Nope, I meant something like

    'sort_by'   => [
        /**
         * Operators
         * ---------------------------------------------------------------------
         *
         * You can (re)define types and supported operators here.
         *
         * @see Operator
         */
        'operators' => [
            SortByOperators::Extra => [
                \LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator\Condition::class,
            ],
        ],
    ],

but, as I realized now, it unfortunately will not work :( So you need to create a custom operator that will create a custom type, you can check Relation as example.

@LastDragon-ru
Copy link
Owner

It is possible to achieve by custom types/operators, but I'm not sure the package should support it out of the box. Sorry. So I will close it for now.

@LastDragon-ru LastDragon-ru closed this as not planned Won't fix, can't repro, duplicate, stale Apr 8, 2023
@LastDragon-ru LastDragon-ru added the wontfix This will not be worked on label Apr 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: graphql scope: feat New feature or request wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants