graphiti-api / spraypaint.js

Graphiti Client / Javascript ORM / JSONAPI
MIT License
107 stars 69 forks source link

Allow fetching as POST requests #76

Open shaungrady opened 4 years ago

shaungrady commented 4 years ago

Given that the URL as passed in as a string to beforeFilters, it's unable to be modified.

The use case for us is that we have an endpoint that we need to use HTTP POSTs for reads due to very long filter strings. beforeFilters allows the HTTP method to be changed to POST, and allows for translating the query string to a POST body, but we're unable to strip the query string from the URL.

I'd imagine the fix for this would be nesting the url and options filter args under an object, which would be a breaking change. I'd be happy to write a PR if it's decided that this would be an acceptable feature.

Another approach might be to check to see if the method has changed from GET to POST/PUT/PATCH and do that translation and query string removal automatically.

richmolj commented 4 years ago

Hey @shaungrady, thanks for the great write-up. This has come up before. I wonder if we might instead add this as a static boolean property on the model class. scope.ts already has a reference to this.model, so this.model.klass.whateverThePropertyIsNamed should get us what we need. Then it's just a matter of swapping this code depending on the property value.

I like this approach because it's a simple flag we can point to whenever the use case comes up, instead of requiring middleware and breaking compatibility. What do you think?

shaungrady commented 4 years ago

I think that approach makes a lot of sense 👌 Maybe a flag named getAsPost to stay consistent with the SpraypaintBase.patchAsPost flag?

richmolj commented 4 years ago

How about fetchAsPost?

shaungrady commented 4 years ago

What serialization behavior for the body would we be looking for?

Do we want to re-parse the query params—note the stringified array…

{ "data": {
    "filters": {
        "foo": "bar,baz"
    }
}}

… or use the straight JsonapiQueryParams format?

{ "data": {
    "filters": {
        "foo": ["bar", "baz"]
    }
}}