orbitjs / orbit

Composable data framework for ambitious web applications.
https://orbitjs.com
MIT License
2.33k stars 133 forks source link

JsonAPI filter operator #559

Closed JohnWhitleyEkkoSense closed 5 years ago

JohnWhitleyEkkoSense commented 5 years ago

Hello,

Thank you for your work on this library. We are looking at creating a robust client side API, and would like to use orbit. We have a working proof-of-concept and have been impressed with many aspects including the structure, the speed of development of our client side code that orbit provides; and the thinking around stores and coordinators.

We are now looking at the filtering parameters being passed up to the server: it appears that we can only send the "equal" filter operator to JsonAPI. When we attempt to pass a different filter operator:

const pendingRooms = store.query(q => q.findRecords('room').filter({
  attribute: 'length',
  op: 'lt',
  value: 1000
});

we get: Query expression parse error: Filter operation lt not recognized for JSONAPISource.

Many thanks in anticipation of your input.

dgeb commented 5 years ago

Hi @JohnWhitleyEkkoSense - Thanks for giving orbit a try and for your kind words.

The (overly) simple answer to your question is that usage of lt is not defined by the JSON:API spec, so it is a bit tricky to define this behavior only on the client-side with no guarantee that servers will understand how to interpret these filters.

There is good news on the horizon though. JSON:API v1.1, which is in final draft now, supports the concept of "profiles" which allow for more opinionated interpretation of the spec. This will unlock the ability for clients and servers to share an understanding of filters and other under-specified areas of the base spec. For example, there's already an issue open (#479) to consider supporting Drupal's concept of "fancy filters". If that doesn't work for you, you can also define your own filter profile and be able to tweak the JSONAPISource to support it ...

Which brings me to my last point: extensibility. We're revamping the standard orbit sources for v0.16 to better support custom interpretations of requests. You'll soon be able to more easily override query and update operators for sources. I expect to get a beta release of v0.16 out soon so you can experiment with it.

dgeb commented 5 years ago

It's now possible to create a custom JSONAPIURLBuilder class and pass that in as an option to your JSONAPISource.

You can customize the buildFilterParam method to account for whatever filtering scheme your server supports.

As I mentioned earlier, I'll be glad to include some advanced filtering behaviors in the base implementation once there's an associated JSON:API profile (or two) that defines those behaviors.

bradjones1 commented 3 years ago

For anyone looking for something like this (e.g., for Drupal's fancy filters) in the meantime:

https://gist.github.com/bradjones1/18ff9ebdb85455f781a2a212577653ce#file-jsonapi-url-builder-js-L10-L24