-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Improvement?] find should take both id and query parameters #1326
Comments
#391 and #551 both are related to this. As @wycats mentioned here: #551 (comment), it is certainly an option to just use |
An easier solution might be to implement your own findQuery that uses the /models/id endpoint with query params. App.ApplicationAdapter = DS.RESTAdapter.extend({
findQuery: function(store, type, query) {
if (query.id) {
url = this.buildURL(type.typeKey, query.id);
delete query.id;
} else {
url = this.buildURL(type.typeKey);
}
return this.ajax(url, 'GET', { data: query });
}
}); |
@wycats Makes sense. Works beautifully. That is definitely easier. I suppose the use case is more of the 20% rather than the 80% so it makes sense to be customization rather than a default. |
@wycats The problem with this approach is that the extract method in the serializer still expects the server to return an array. Your solution to #551 makes sense, however, I need "find" functionality (no array) but with "findQuery" functionality (query params). I've looked at writing my own
And in my route:
However, I am now getting this error: |
UPDATE: I have found a workaround to the Route problem via:
Which I suppose will work...do you have an opinion? |
@abobwhite Did you find a clean solution to this? When I try your last update |
@ryanjm I haven't worked toward a cleaner solution yet. Indeed, I had to do a small manipulation in my Calling |
@abobwhite @ryanjm I'm facing the same problem. in my case I must find by id, and pass an additional parameter to request all fields to be included in the response (some are omitted by default), like so:
I tried to do it this way:
But my params were ignored due to this limitation. I could go about implementing as suggested above, but seems like a hack for such a common use case... It would be awesome if Ember would allow for a syntax like This way we keep the same API signature for find, allowing for an optional data object that would be added to the query as query params. What you guys think? |
@nosachamos I definitely agree with you. There are plenty of valid use cases like yours that it would be worth looking into a better solution. I do not have enough time to work on a PR myself but your Another option that @wycats suggested to me back in the fall would be to implement some sort of |
Cool, I'll try to get a PR in later tonight, after work. Cross your fingers, this would be great to have but since I'll have to make changes in a few places in both the store and adapter I'm not too hopeful it will be accepted. While this does not happen a workaround would be to pass the info using request headers instead. Not an option if you are not the owner of the API, but if you are, it's something to consider. |
@nosachamos Good luck. I think there are deep implications in ED with doing something like this. There may be a way to do this in a way that it's the first step of many but doesn't feel like it's half-baked. Something like I mentioned above could be far-reaching. It would be nice to pick some brains on the Ember team to hear some thoughts on this - particularly @wycats, who originally proposed it. |
@abobwhite you're right. Mon and Tue I'll be in a conference with a few members of the core team, so I'll pick their brains on this before submitting anything. Thanks, I'll keep this thread updated with any developments. |
+1 for |
@thomasvanlankveld I agree with you. There should be some standard way to do this. I do not believe it to be an edge case. It's fairly common practice even if it's not the most common scenario. It seems this and other related issues have been dismissed (closed) by core team members because they probably had bigger fish to fry. But currently, all solutions suggested making this work are a hack. I think the ultimate issue at hand here is that if we try That being said, I'm in favor of simply permitting query params on the |
+1 for |
+1 for find('model', id, paramsObj). This is something I find myself needing on many of my endpoints of my API. It just doesn't feel right having to fetch a single record as an array and extract the index I need every time. There is possibly another option, although I don't know how much it might slow things down (and doesn't quite feel right either). A possible solution is to see if any one of the keys in the params object matches the models primaryKey. If so, return a single object instead of an array. But this might be difficult to pull off considering the id parameter can be an array of ids (if I'm not mistaken). |
This will be possible once findQueryOne will be merged (which should be
|
@thaume Great! Glad to see something came out of this. Thanks for putting the time to develop the feature. I hope it gets merged soon. This issue has only been here for 1.5 years! One of the key features needed to make ED really robust. |
Great news @thaume. Thank you for the update. |
@thaume - I was looking for this today. I hope this get in the next beta release. |
I would also need this functionality. Our use case is as follows: When I query an object just by it's ID I get all labels of this object in the default language (which is english). Now when I specify a contentLanguage as query param I get the labels in the corresponding language. But when I want to query a object by it's ID and get it in a special contentLanguage I can't do it. My first thought was also to query like the others mentioned: this.get('store').find('item',id,{contentLanguage: 'de'}).then(function(response) {
// PROCESS SUCCESS
}, function(error) {
// PROCESS ERROR
}); But it does not work! Is there any way to accomplish this behavior without some hack? |
@tschoartschi While this use-case definitely needs to happen (I've been waiting for over 1.5 years for it), I think in your case, you could probably use the HTTP Header: 'Accept-Language' which can be defined in your adapter. On your server-side, you would just read this header and apply logic accordingly, thus removing the need for a query parameter here. |
+1 for fitting into the real world |
Please note that the third parameter to The reason for this not being a plain object for query params is that we want to be able to extend the behavior of Ember Data in the future. First-class support for pagination, include and sorting are a couple of things being discussed. We want to reserve the possibility of having something like |
Another request for this feature. I assumed that I could pass parameters when using A common example is when you need to pass extra data to the server to annotate the result coming back. In my case, I want to annotate the distance to the returned object from the user's position (passed by query params) |
With the advent of JSONAPI and the |
Could it be that this is actually already possible through the |
While arbitrary query parameters are not supported include it is possible to specify the |
Yup. Relevant RFC for anyone wondering. emberjs/rfcs#99 I should be more observant... |
I have a situation where I need to find a model via
/models/{id}
but to also provide query parameters:/models/{id}?key=value
. The reason is that this particular model is under version control on the server. It can get "approved" and become publicly visible but another version can still be edited by the user in a "draft" state. When the user is logged in, they should be able to access the model in that "draft" state. There are two obvious solutions:find({id})
to optionally take query params:find({id}, {/* draft : true */})
/draftModels/{id}
I prefer option 1 but I can see how opinions would be split between the options. Option 1 can be used to provide different information on the same model depending on a specific use-case and the state of the data.
Thoughts?
The text was updated successfully, but these errors were encountered: