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

Fix for #130 - Adds support for Boolean property accessors with 'get' prefix in PropertyDataFetcher #164

Merged
merged 4 commits into from Aug 20, 2016

Conversation

ayhanap
Copy link
Contributor

@ayhanap ayhanap commented Jun 15, 2016

As mentioned in #130 some languages and frameworks uses "get" prefix for Booleantypes. This was also the case for me. I use Lombok for generating accessors which also uses this convention. Even IDE's generate getters for Boolean with "get"

The prefix 'is' generally used for primitive boolean, Lombok has a option to disable this but not for Boolean. So I think this functionality should be a part of PropertyDataFetcher. Otherwise it will be error prone or some unnecessary configuration/code for these fields.

}
}

private Object getPropertyViaGetterUsingPrefix(Object object, GraphQLOutputType outputType, String prefix) throws NoSuchMethodException {
String getterName = prefix + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
try {
Method method = object.getClass().getMethod(getterName);
return method.invoke(object);

} catch (NoSuchMethodException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just not have a catch block for NoSuchMethodException?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, just removed the unnecessary catch block for NoSuchMethodException

@markphilpot
Copy link
Contributor

Thanks for the PR! Let me know if the comments make sense.

@aschrijver
Copy link
Contributor

aschrijver commented Aug 19, 2016

It could also have an additional clause with a search for any property that contains propertyName, and return the first match. This way you will catch any reasonable naming convention (only not the completely unrelated names).

(BTW Many new codes do not use is and get prefixes any more, but what fits with the domain language and fluent coding e.g. shouldUpdateAccount, succeeded, canSubtractValue, etc.)

@dminkovsky
Copy link
Contributor

Yes, thank you for this PR! Let's fix this, given the demand.

What do you think about this: https://github.com/dminkovsky/graphql-java/blob/8efb230b58224488c335cc8409c17a73f0657b51/src/main/java/graphql/schema/PropertyDataFetcher.java

I think it might be clearer?

@ayhanap
Copy link
Contributor Author

ayhanap commented Aug 19, 2016

Thanks for review @markphilpot The current flow to find property data is as follows

  1. Try accessor methods. If the property is boolean use is getter methods instead of get.
  2. If no method exists then .. If the property is boolean, try get accessor method also.
  3. If no method exists then .. Get property data with field access.
    Before this PR the current flow was 1 then 3, without 2.
    So the code in your inline comments does not fulfill the requirements. Therefore I suggest you to check working code and let me know if this makes sense.

@ayhanap
Copy link
Contributor Author

ayhanap commented Aug 19, 2016

@dminkovsky Looks simpler and more explanatory indeed.

@dminkovsky
Copy link
Contributor

Whoops didn't mean to overload getPropertyViaGetter there. One of those should be renamed.

@dminkovsky
Copy link
Contributor

@ayhanap cool. If you like that more, feel free to update your PR with that variant and we can see what other people think. Will be glad to close this one.

@ayhanap
Copy link
Contributor Author

ayhanap commented Aug 19, 2016

Updated the PR with @dminkovsky's cleaner suggestion.

@dminkovsky
Copy link
Contributor

dminkovsky commented Aug 20, 2016

@ayhanap cool, thanks. I think there's also @markphilpot's question regarding that caught-and-then-immediately-thrown NoSuchMethodException in getPropertyViaGetterUsingPrefix(). It does seem that it's not necessary?

@ayhanap
Copy link
Contributor Author

ayhanap commented Aug 20, 2016

Sorry missed that. Fixed that.

@aschrijver
Copy link
Contributor

aschrijver commented Aug 20, 2016

How do you think about earlier suggestion to allow any kind of prefix or postfix #164 (comment) ?

I know classes with conventions like can, should, with, on, after, etc.

@aschrijver
Copy link
Contributor

Yet another option, and easily added is a public overload that takes the prefix.

@ayhanap
Copy link
Contributor Author

ayhanap commented Aug 20, 2016

I think searching for method names containing fielddefinition names could lead to some unexpected results.
All I can think is to search for a method name as exact field definition name. So that way you can define field definitions on your getter method name. And the example scenario I mentioned is as follows.

newFieldDefinition()
            .name("shouldUpdateAccount") // instead of using updateAccount
            .description("Should Update Account Details")
            .type(GraphQLString)
            .build()

And a POJO like this

public class Account{
    boolean updateAccount;
    public shouldUpdateAccount(){
        return this.uppdateAccount;
    }
}

What do you think @aschrijver.

@aschrijver
Copy link
Contributor

Yes, I agree, you are right with the search. It would be too much of a burden on the developer to get it right, and don't forget when adding a similarly-named member in future.

Passing a prefix parameter could be solution.

On a different note, related but not the same, I have a need to be able to define field aliases. I am discussing that on the metadata-related issues I created earlier.

@dminkovsky
Copy link
Contributor

dminkovsky commented Aug 20, 2016

I kind of like the idea of keeping the surface of this class small, like it currently is. Property fetchers, after all, are a core public API. If people need some specific functionality they can write their own property fetcher and with lambda notation this is trivial for a simple property reference. To me, this PR is about responding to a specific repeated request from multiple users.

So, let's revisit adding more functionality to this property fetcher when there is repeated, specific demand? I like how it looks right now as of this PR, especially with the removed unused fields.

@aschrijver
Copy link
Contributor

aschrijver commented Aug 20, 2016

Yeah, that's no problem. Let's close this one. Given how busy I am it will be some time before I revisit this, maybe then I'll come up with a more elegant PR for other prefixes. We'll see..

@ayhanap
Copy link
Contributor Author

ayhanap commented Aug 20, 2016

Agree, those other requirements need more discussion and evaluation. Keeping in mind that this class is responsible of most of the data fetching, it should be fast and simple.

@markphilpot markphilpot merged commit ad26987 into graphql-java:master Aug 20, 2016
@dminkovsky
Copy link
Contributor

Thanks Mark

суббота, 20 августа 2016 г. пользователь Mark Philpot написал:

Merged #164 #164.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#164 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AANWZYLq6dN71JDtpZLJ6U1ls95S2VlXks5qh4vQgaJpZM4I2vHN
.

@aschrijver
Copy link
Contributor

@dminkovsky Hailing from Russia or Ukrain? Then you are making even more late-nighters than me 😉

@markphilpot
Copy link
Contributor

пожалуйста

@dminkovsky
Copy link
Contributor

Oh no thankfully on the east coast of the US. Have my phone in Russian
though, so it does this. I try to get to sleep at a reasonable hour :)

суббота, 20 августа 2016 г. пользователь Mark Philpot написал:

пожалуйста


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#164 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AANWZRFzIo2I7fRWpEB5tFDen3vukEmgks5qh6AUgaJpZM4I2vHN
.

@aschrijver
Copy link
Contributor

Lucky you, ha ha. I should be horizontal now at 04:00PM on the clock, not in S shape over laptop 😁

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

Successfully merging this pull request may close these issues.

None yet

4 participants