Hey there!
I am to query an API that has this endpoint to filter a one-to-one relationship:
http://localhost:5000/api/v1.0/posts?include=status&filter=[{"name":"status__status","op":"has","val":"Published"}]
I’ve tried this using Ember:
this.store.query('post', {filter:{"status":{"name":"status","op":"eq","val":"Published"} }});
And this:
this.store.query('post', {filter:{"name":"status","op":"eq","val":"Published"}});
But the resulting query are these:
http://localhost:5000/api/v1.0/posts?filter[status][name]=status&filter[status][op]=eq&filter[status][val]=Published
http://localhost:5000/api/v1.0/posts?filter[name]=status&filter[op]=eq&filter[val]=Published
Which is quite confusing! I am not sure how to create this queries when I have belongsTo and hasMany relationships.
What is the right way to create this query?
I’ve seen this references here and here but I couldn’t figure out how to write the right query.
Is there any other documentation that I could read to understand better how to create these queries?
Thanks in advance!
b-arto
2
Probably (if someone doesn’t give you better solution) you should check adapters JSONAPIAdapter - 3.25 - Ember API Documentation
Thank you! I was hoping to not have to go down there.
I have managed to make this work! I’ll let it detailed here for further reference:
That’s my application adapter:
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import ENV from '../config/environment';
export default class ApplicationAdapter extends JSONAPIAdapter {
host = ENV.APP.api;
namespace = ENV.APP.namespace;
fixFilterQuery(query) {
if (Object.prototype.hasOwnProperty.call(query, 'filter')) {
query.filter = JSON.stringify([query.filter]);
}
return query;
};
query(store, type, query) {
query = this.fixFilterQuery(query);
return super.query(store, type, query);
}
}
And that’s how I am making queries:
let filterOptions = {
name : 'status',
op : 'has',
val : {"name":"status","op":"eq","val":"Booked"}
};
this.store.query('reservation', {sort: "-start_date", include: 'status,type,location', filter: filterOptions, page: {number: params.page, size: params.size}})