yahoo / elide

Elide is a Java library that lets you stand up a GraphQL/JSON-API web service with minimal effort.
https://elide.io
Other
1k stars 229 forks source link

How to filter by attribute? #668

Closed DerKlassiker closed 6 years ago

DerKlassiker commented 6 years ago

I have a question regarding filtering. Let's say I have this class as below.

public class Employee {
   private String id;
   private int age;
   private String address;
}

How can I filter by age for example only filter employees whose age is 30? Based on the documentation, it seems that

"/employee?filter[age]=30"

But it won't work.

DerKlassiker commented 6 years ago

@DennisMcWherter : Do you know by any chance?

aklish commented 6 years ago

Try "/employee?filter[employee.age]=30"

Aaron

On Mon, Jun 4, 2018 at 9:42 PM, DerKlassiker notifications@github.com wrote:

@DennisMcWherter https://github.com/DennisMcWherter : Do you know by any chance?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394563103, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QWCWoDDVBJIxgxzzY8Xz4OGSE0a3ks5t5fAfgaJpZM4UaEV5 .

DerKlassiker commented 6 years ago

@aklish : Won't work

aklish commented 6 years ago

Can you elaborate? It should. There are lots of similar examples here:

https://github.com/yahoo/elide/blob/master/elide-integration-tests/src/test/groovy/com/yahoo/elide/tests/FilterIT.groovy#L1337

Is age a computed or real attribute?

Aaron

On Mon, Jun 4, 2018 at 9:56 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : Won't work

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394565280, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QXp2rcbN4W3bcqtbaWWn4s-zFq80ks5t5fNtgaJpZM4UaEV5 .

aklish commented 6 years ago

Another possibility - your employee class has all fields listed as private. That could also be the problem.

Elide doesn't expose private fields.

Aaron

On Mon, Jun 4, 2018 at 9:59 PM, Aaron Klish aklish@gmail.com wrote:

Can you elaborate? It should. There are lots of similar examples here:

https://github.com/yahoo/elide/blob/master/elide- integration-tests/src/test/groovy/com/yahoo/elide/tests/ FilterIT.groovy#L1337

Is age a computed or real attribute?

Aaron

On Mon, Jun 4, 2018 at 9:56 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : Won't work

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394565280, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QXp2rcbN4W3bcqtbaWWn4s-zFq80ks5t5fNtgaJpZM4UaEV5 .

aklish commented 6 years ago

I also highly recommend all models to use public property accessors (getAge, setAge) instead of fields.

All of our tests are on models with properties. In theory, fields could work - but it is not supported 100% (see https://github.com/yahoo/elide/issues/644)

Aaron

On Mon, Jun 4, 2018 at 10:00 PM, Aaron Klish aklish@gmail.com wrote:

Another possibility - your employee class has all fields listed as private. That could also be the problem.

Elide doesn't expose private fields.

Aaron

On Mon, Jun 4, 2018 at 9:59 PM, Aaron Klish aklish@gmail.com wrote:

Can you elaborate? It should. There are lots of similar examples here:

https://github.com/yahoo/elide/blob/master/elide-integration -tests/src/test/groovy/com/yahoo/elide/tests/FilterIT.groovy#L1337

Is age a computed or real attribute?

Aaron

On Mon, Jun 4, 2018 at 9:56 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : Won't work

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394565280, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QXp2rcbN4W3bcqtbaWWn4s-zFq80ks5t5fNtgaJpZM4UaEV5 .

DerKlassiker commented 6 years ago

@aklish : it is a real attribute and have public property accessors. Not working. Still trying to figure out why it is not working.

DerKlassiker commented 6 years ago

@aklish : I got "InvalidPredicateException: Invalid query parameter: filter[age]" error

aklish commented 6 years ago

Try changing age to employee.age

On Mon, Jun 4, 2018 at 10:11 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : I got "InvalidPredicateException: Invalid query parameter: filter[age]" error

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394567364, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QTTWdwHXob2z5huqlIwMpDv8YSJgks5t5fblgaJpZM4UaEV5 .

DerKlassiker commented 6 years ago

@aklish : tried both. none of them working :(

aklish commented 6 years ago

FYI, here is the code where Elide scans the model for fields to expose:

https://github.com/yahoo/elide/blob/master/elide-core/src/main/java/com/yahoo/elide/core/EntityBinding.java#L119

It only includes public fields.

Aaron

On Mon, Jun 4, 2018 at 10:13 PM, Aaron Klish aklish@gmail.com wrote:

Try changing age to employee.age

On Mon, Jun 4, 2018 at 10:11 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : I got "InvalidPredicateException: Invalid query parameter: filter[age]" error

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394567364, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QTTWdwHXob2z5huqlIwMpDv8YSJgks5t5fblgaJpZM4UaEV5 .

aklish commented 6 years ago

Did you fix the model to add public getAge and public setAge?

Aaron

On Mon, Jun 4, 2018 at 10:14 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : tried both. none of them working :(

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394567832, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QcOVCy_ENzUg_LjXHLGr8EzRYnY0ks5t5febgaJpZM4UaEV5 .

DerKlassiker commented 6 years ago

Yes. any other possibilities?

aklish commented 6 years ago

Can you send me link to your repo? Did you add @Include annotation on the model? Is it a JPA entity (@Entity)?

You need both annotations on the class. Otherwise Elide doesn't expose it.

On Mon, Jun 4, 2018 at 10:17 PM, DerKlassiker notifications@github.com wrote:

Yes. any other possibilities?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394568240, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QcvwsbK3qP11byaa3xYgVfxnNVoIks5t5fg7gaJpZM4UaEV5 .

DerKlassiker commented 6 years ago

Yes, have both, include and entity annotation.

Unfortunately, I can't send you the link to the repo as it is corporate. But it is basically like this.

@Entity
@Include(rootLevel = true)
@Table(name = "employees")
public class Employee {
   private String id;
   private int age;

   public void setAge(int age) {
      this.age = age;
   }

   public int getAge() {
      return age;
   }

   private String address;
}
aklish commented 6 years ago

Ok - one other theory. Elide supports two filter dialects - RSQL and the one you are using.

In elide 4.0, RSQL became the default. It has a different syntax. The other mechanism might be disabled by default (I'll have to check in code).

Let me get the format for that filter..one sec...

On Mon, Jun 4, 2018 at 10:22 PM, DerKlassiker notifications@github.com wrote:

Yes, have both, include and entity annotation.

Unfortunately, I can't send you the link to the repo as it is corporate. But it is basically like this.

@Entity @Include(rootLevel = true) @Table(name = "employees") public class Employee { private String id; private int age;

public void setAge(int age) { this.age = age; }

public int getAge() { return age; }

private String address; }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394568925, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QbRfjbYTc8xXVV0WVyHmMikiM0GCks5t5fl0gaJpZM4UaEV5 .

aklish commented 6 years ago

Try filter[employee]=age==30

On Mon, Jun 4, 2018 at 10:23 PM, Aaron Klish aklish@gmail.com wrote:

Ok - one other theory. Elide supports two filter dialects - RSQL and the one you are using.

In elide 4.0, RSQL became the default. It has a different syntax. The other mechanism might be disabled by default (I'll have to check in code).

Let me get the format for that filter..one sec...

On Mon, Jun 4, 2018 at 10:22 PM, DerKlassiker notifications@github.com wrote:

Yes, have both, include and entity annotation.

Unfortunately, I can't send you the link to the repo as it is corporate. But it is basically like this.

@Entity @Include(rootLevel = true) @Table(name = "employees") public class Employee { private String id; private int age;

public void setAge(int age) { this.age = age; }

public int getAge() { return age; }

private String address; }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394568925, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QbRfjbYTc8xXVV0WVyHmMikiM0GCks5t5fl0gaJpZM4UaEV5 .

DerKlassiker commented 6 years ago

@aklish : it is working! Thank you so much!

aklish commented 6 years ago

No problem. Make sure you adjust your other properties to public getter/setters too (including the ID). Take care.

Aaron

On Mon, Jun 4, 2018 at 10:28 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : it is working! Thank you so much!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394569808, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QfldB_bGKrr4o3T6yOI5Du1BC1NQks5t5frjgaJpZM4UaEV5 .

aklish commented 6 years ago

Looks like elide standalone (if you are using it) only enables RSQL filters by default:

https://github.com/yahoo/elide/blob/master/elide-standalone/src/main/java/com/yahoo/elide/standalone/config/ElideStandaloneSettings.java#L60-L61

It is still possible to enable the other dialect in code if you prefer that format.

Aaron

On Mon, Jun 4, 2018 at 10:29 PM, Aaron Klish aklish@gmail.com wrote:

No problem. Make sure you adjust your other properties to public getter/setters too (including the ID). Take care.

Aaron

On Mon, Jun 4, 2018 at 10:28 PM, DerKlassiker notifications@github.com wrote:

@aklish https://github.com/aklish : it is working! Thank you so much!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yahoo/elide/issues/668#issuecomment-394569808, or mute the thread https://github.com/notifications/unsubscribe-auth/AGp_QfldB_bGKrr4o3T6yOI5Du1BC1NQks5t5frjgaJpZM4UaEV5 .

DerKlassiker commented 6 years ago

@aklish : Thank you very much for your help once again. Could you tell me how to sort by age too? The documentation doesn't really seem clear to me.

aklish commented 6 years ago

sort=age for ascending OR sort=-age for descending

aklish commented 6 years ago

Can I close the issue?

DerKlassiker commented 6 years ago

@aklish : Thank you!