Skip to content
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

Reduce data overload by letting frontend specify what fields it wants #84

Open
SpaceK33z opened this issue Nov 22, 2017 · 0 comments
Open

Comments

@SpaceK33z
Copy link
Contributor

SpaceK33z commented 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:

{
  hero {
    name
    friends {
      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).

@SpaceK33z SpaceK33z changed the title Only fetch specific fields Reduce data overload by letting frontend specify what fields it wants Nov 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant