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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/main/java/graphql/schema/PropertyDataFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,27 @@ public Object get(DataFetchingEnvironment environment) {
}

private Object getPropertyViaGetter(Object object, GraphQLOutputType outputType) {
String prefix = isBooleanProperty(outputType) ? "is" : "get";
try {
if (isBooleanProperty(outputType)) {
try {
return getPropertyViaGetterUsingPrefix(object, "is");
} catch (NoSuchMethodException e) {
return getPropertyViaGetterUsingPrefix(object, "get");
}
} else {
return getPropertyViaGetterUsingPrefix(object, "get");
}
} catch (NoSuchMethodException e1) {
return getPropertyViaFieldAccess(object);
}
}

private Object getPropertyViaGetterUsingPrefix(Object object, 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) {
return getFieldValue(object, outputType);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
Expand All @@ -50,7 +63,7 @@ private boolean isBooleanProperty(GraphQLOutputType outputType) {
return false;
}

private Object getFieldValue(Object object, GraphQLOutputType outputType) {
private Object getPropertyViaFieldAccess(Object object) {
try {
Field field = object.getClass().getField(propertyName);
return field.get(object);
Expand Down
39 changes: 39 additions & 0 deletions src/test/groovy/graphql/DataFetcherTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import graphql.schema.FieldDataFetcher
import graphql.schema.PropertyDataFetcher
import spock.lang.Specification

import static graphql.Scalars.GraphQLBoolean
import static graphql.Scalars.GraphQLString

class DataFetcherTest extends Specification {
Expand All @@ -13,6 +14,8 @@ class DataFetcherTest extends Specification {

private String privateField
public String publicField
private Boolean booleanField
private Boolean booleanFieldWithGet

public String getProperty() {
return privateField
Expand All @@ -21,13 +24,31 @@ class DataFetcherTest extends Specification {
public void setProperty(String value) {
privateField = value
}

public Boolean isBooleanField() {
return booleanField;
}

public void setBooleanField(Boolean value) {
booleanField = value
}

public Boolean getBooleanFieldWithGet() {
return booleanFieldWithGet
}

public Boolean setBooleanFieldWithGet(Boolean value) {
booleanFieldWithGet = value
}
}
def DataHolder dataHolder

def setup() {
dataHolder = new DataHolder();
dataHolder.publicField = "publicValue"
dataHolder.setProperty("propertyValue")
dataHolder.setBooleanField(true)
dataHolder.setBooleanFieldWithGet(false)
}

def "get field value"() {
Expand All @@ -48,6 +69,24 @@ class DataFetcherTest extends Specification {
result == "propertyValue"
}

def "get Boolean property value"() {
given:
def environment = new DataFetchingEnvironment(dataHolder, null, null, null, GraphQLBoolean, null, null)
when:
def result = new PropertyDataFetcher("booleanField").get(environment)
then:
result == true
}

def "get Boolean property value with get"() {
given:
def environment = new DataFetchingEnvironment(dataHolder, null, null, null, GraphQLBoolean, null, null)
when:
def result = new PropertyDataFetcher("booleanFieldWithGet").get(environment)
then:
result == false
}

def "get field value as property"() {
given:
def environment = new DataFetchingEnvironment(dataHolder, null, null, null, GraphQLString, null, null)
Expand Down