d34dman / drupal-jsonapi-params

A package to manage json-api params
ISC License
59 stars 8 forks source link

how do i generate short result for simple query? #29

Closed laurencefass closed 2 years ago

laurencefass commented 2 years ago

Ive just started testing this.

The readme states:

"...it also tries to optimise the query by using the short form, whenever possible."

I have constructed a very simple query but I seem to be getting the long result.

apiParams
.addFilter('title', 'cat', 'CONTAINS')
.addFields('node--article', ['body', 'title'])

which generates:

filter[title][condition][value]=cat&filter[title][condition][operator]=CONTAINS&fields[node--article]=body,title

which can be more efficiently constructed as follows to give same result.

filter[title][operator]=CONTAINS&filter[title][value]=cat&fields[node--article]=body,title
d34dman commented 2 years ago

Dear @laurencefass ,

The rules of shortcuts are used from the following documentation on Drupal.org https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/filtering#shortcuts.

apiParams
.addFilter('title', 'cat', 'CONTAINS')
.addFields('node--article', ['body', 'title'])

The above code should generate something like below,

filter[title][condition][path]=title&filter[title][condition][value]=cat&filter[title][condition][operator]=CONTAINS&fields[node--article]=body,title

i.e. it will also contain filter[title][condition][path]=title& part.

The reason it is not "shortened" is due to the use of "CONTAINS" operator. "Shortcuts" are applied only for equality ("="). Could you please point me to the documentation where it says there are other ways to shorten the query?

laurencefass commented 2 years ago

heres a working example of the short form above. works with CONTAINS. https://latest.syntapse.co.uk/jsonapi/node/article?filter[title][operator]=CONTAINS&filter[title][value]=cat&fields[node--article]=body,title

d34dman commented 2 years ago

@laurencefass ,

Interesting, it does seems like "condition" can be omitted. I would be happy to work on the optimisation if it is officially documented.

Let me share this issue with Drupal's slack and get other's opinion about it.

d34dman commented 2 years ago

Update: Am discussing this in the slack channel https://drupal.slack.com/archives/C5A70F7D1/p1644928776753089 to get more input on this topic.

d34dman commented 2 years ago

@laurencefass , I have created an issue on drupal.org https://www.drupal.org/project/drupal/issues/3264769 Based on if the shortcuts are officially supported or not, would consider adding support in this library.

Hope I got your Drupal user name correct while mentioning credits.

d34dman commented 2 years ago

Apparently there is indeed documentation on Drupal.org about shortening of query. More details here : https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/filtering#s-7-filter-for-nodes-where-title-contains-foo

This is a bug because, the library lies that it would create a short query, but it doest -.-".

d34dman commented 2 years ago

This has been fixed in the latest release 2.0.0