You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the moment Binder will return all fields of a model when doing a GET request. In a recent project, we have models with over 50+ fields (I wish I was exaggerating). Often we need to fetch some relations of that model as well, which also have around 30 fields. In the frontend we often only actually use some of these fields.
So I want some way of specifying which fields the API returns, e.g. api/animal/?field=name,kind. By default it would just work like before, no breaking change here.
GraphQL fixes this exact issue, but switching to it now would be a huge refactor. However, we should get some inspiration from this.
I have some suggestions:
Mimicking GraphQL
An example query in GraphQL:
{
hero {
namefriends {
name
}
}
}
GraphQL doesn't use entity url's like api/hero but instead one url, /graphql. That would be too much of a switch so I suggest ignoring the root model (hero) for us is best;
name
friends {
name
}
Since we need to pass this is in a GET parameter, it would need to be URL encoded. We can also strip all whitespace: .replace(/\s+/g, ' ').
The final query to the backend would be: api/hero/?field=name%20friends%20%7B%20name%20%7D
This syntax also allows filtering (note that the filter key "name_startswith" is not part of the spec, it's just something I made up):
friends(name_startswith: "Henk") {
name
}
Making up our own syntax
Something like this perhaps:
api/hero/?field=name,friends(name)
Note that ?with=friends wouldn't be necessary here; this is implied. Also every relation should always include id. As a last point this should remove the need for m2m_fields (so it finally fixes #14).
The text was updated successfully, but these errors were encountered:
SpaceK33z
changed the title
Only fetch specific fields
Reduce data overload by letting frontend specify what fields it wants
Nov 22, 2017
At the moment Binder will return all fields of a model when doing a
GET
request. In a recent project, we have models with over 50+ fields (I wish I was exaggerating). Often we need to fetch some relations of that model as well, which also have around 30 fields. In the frontend we often only actually use some of these fields.So I want some way of specifying which fields the API returns, e.g.
api/animal/?field=name,kind
. By default it would just work like before, no breaking change here.GraphQL fixes this exact issue, but switching to it now would be a huge refactor. However, we should get some inspiration from this.
I have some suggestions:
Mimicking GraphQL
An example query in GraphQL:
GraphQL doesn't use entity url's like
api/hero
but instead one url,/graphql
. That would be too much of a switch so I suggest ignoring the root model (hero
) for us is best;Since we need to pass this is in a GET parameter, it would need to be URL encoded. We can also strip all whitespace:
.replace(/\s+/g, ' ')
.The final query to the backend would be:
api/hero/?field=name%20friends%20%7B%20name%20%7D
This syntax also allows filtering (note that the filter key "name_startswith" is not part of the spec, it's just something I made up):
Making up our own syntax
Something like this perhaps:
api/hero/?field=name,friends(name)
Note that
?with=friends
wouldn't be necessary here; this is implied. Also every relation should always includeid
. As a last point this should remove the need form2m_fields
(so it finally fixes #14).The text was updated successfully, but these errors were encountered: