Skip to content

Commit 183752c

Browse files
Bump version graphql-java from 17.2 to 20.2
1 parent 6d9d7a7 commit 183752c

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/DirectivesBuilder.java

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package graphql.annotations.processor.retrievers.fieldBuilders;
1616

17+
import graphql.Scalars;
1718
import graphql.annotations.annotationTypes.directives.activation.GraphQLDirectives;
1819
import graphql.annotations.processor.ProcessingElementsContainer;
1920
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
@@ -26,11 +27,14 @@
2627
import java.lang.annotation.Annotation;
2728
import java.lang.reflect.AnnotatedElement;
2829
import java.lang.reflect.Method;
30+
import java.math.BigDecimal;
2931
import java.util.ArrayList;
3032
import java.util.Arrays;
3133
import java.util.List;
3234
import java.util.stream.Collectors;
3335

36+
import static graphql.Assert.assertShouldNeverHappen;
37+
import static graphql.scalar.CoercingUtil.isNumberIsh;
3438
import static graphql.schema.GraphQLDirective.newDirective;
3539

3640

@@ -134,7 +138,22 @@ private void transformArgument(Annotation annotation, GraphQLDirective.Builder d
134138
Object argumentValue = methods[finalI].invoke(annotation);
135139
Object value;
136140
if (graphQLArgument.getType() instanceof GraphQLScalarType) {
137-
value = ((GraphQLScalarType) graphQLArgument.getType()).getCoercing().parseValue(argumentValue);
141+
// value = ((GraphQLScalarType) graphQLArgument.getType()).getCoercing().parseValue(argumentValue);
142+
143+
try {
144+
GraphQLScalarType argumentType = (GraphQLScalarType) graphQLArgument.getType();
145+
if ( argumentType.equals( Scalars.GraphQLBoolean ) )
146+
{
147+
value = castToBoolean( argumentValue );
148+
}
149+
else
150+
{
151+
value = argumentType.getCoercing().parseValue( argumentValue );
152+
}
153+
builder.value( value );
154+
} catch (Exception e) {
155+
throw new GraphQLAnnotationsException(COULD_NOT_PARSE_ARGUMENT_VALUE_TO_ARGUMENT_TYPE, e);
156+
}
138157
}
139158
else{
140159
value = argumentValue;
@@ -159,8 +178,17 @@ private void transformArgument(String[] argumentValues, GraphQLDirective.Builder
159178
if (graphQLArgument.getType() instanceof GraphQLScalarType) {
160179

161180
try {
162-
Object value = ((GraphQLScalarType) graphQLArgument.getType()).getCoercing().parseValue(argumentValue);
163-
builder.value(value);
181+
Object value;
182+
GraphQLScalarType argumentType = (GraphQLScalarType) graphQLArgument.getType();
183+
if ( argumentType.equals( Scalars.GraphQLBoolean ) )
184+
{
185+
value = castToBoolean( argumentValue );
186+
}
187+
else
188+
{
189+
value = argumentType.getCoercing().parseValue( argumentValue );
190+
}
191+
builder.value( value );
164192
} catch (Exception e) {
165193
throw new GraphQLAnnotationsException(COULD_NOT_PARSE_ARGUMENT_VALUE_TO_ARGUMENT_TYPE, e);
166194
}
@@ -169,4 +197,43 @@ private void transformArgument(String[] argumentValues, GraphQLDirective.Builder
169197
}
170198
}));
171199
}
200+
201+
private Boolean castToBoolean( Object input )
202+
{
203+
if ( input instanceof Boolean )
204+
{
205+
return (Boolean) input;
206+
}
207+
else if ( input instanceof String )
208+
{
209+
String lStr = ( (String) input ).toLowerCase();
210+
if ( lStr.equals( "true" ) )
211+
{
212+
return true;
213+
}
214+
if ( lStr.equals( "false" ) )
215+
{
216+
return false;
217+
}
218+
return null;
219+
}
220+
else if ( isNumberIsh( input ) )
221+
{
222+
BigDecimal value;
223+
try
224+
{
225+
value = new BigDecimal( input.toString() );
226+
}
227+
catch ( NumberFormatException e )
228+
{
229+
// this should never happen because String is handled above
230+
return assertShouldNeverHappen();
231+
}
232+
return value.compareTo( BigDecimal.ZERO ) != 0;
233+
}
234+
else
235+
{
236+
return null;
237+
}
238+
}
172239
}

src/test/java/graphql/annotations/GraphQLDataFetcherTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import graphql.annotations.processor.GraphQLAnnotations;
2323
import graphql.schema.DataFetcher;
2424
import graphql.schema.DataFetchingEnvironment;
25+
import graphql.schema.GraphQLFieldDefinition;
2526
import graphql.schema.GraphQLSchema;
2627
import graphql.schema.PropertyDataFetcher;
2728
import org.testng.annotations.BeforeMethod;
2829
import org.testng.annotations.Test;
2930

3031
import java.util.HashMap;
32+
import java.util.function.Supplier;
3133

3234
import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema;
3335
import static org.testng.Assert.*;
@@ -54,7 +56,7 @@ public void shouldUsePreferredConstructor() {
5456
final HashMap<String, Object> data = result.getData();
5557
assertNotNull(data);
5658
assertTrue(((HashMap<String, Boolean>) data.get("sample")).get("isGreat"));
57-
assertFalse(((HashMap<String, Boolean>) data.get("sample")).get("isBad")); // TODO investigate why returned false instead of true
59+
assertTrue(((HashMap<String, Boolean>) data.get("sample")).get("isBad"));
5860
}
5961

6062
@Test
@@ -84,7 +86,7 @@ public void shouldUseTargetAndArgumentsForDataFetcherDeclaredInMethod() {
8486
// Then
8587
final HashMap<String, Object> data = result.getData();
8688
assertNotNull(data);
87-
assertFalse(((HashMap<String, Boolean>) data.get("sample")).get("isBad")); // TODO investigate why returned true instead of false
89+
assertTrue(((HashMap<String, Boolean>) data.get("sample")).get("isBad"));
8890
}
8991

9092
@GraphQLName("Query")
@@ -161,8 +163,10 @@ public SampleMultiArgDataFetcher(String target, String flip) {
161163
}
162164

163165
@Override
164-
public Object get(DataFetchingEnvironment environment) {
165-
final Object result = super.get(environment);
166+
public Object get( final GraphQLFieldDefinition fieldDefinition, final Object source, final Supplier supplier )
167+
throws Exception
168+
{
169+
final Object result = super.get( fieldDefinition, source, supplier );
166170
if (flip) {
167171
return !(Boolean) result;
168172
} else {

src/test/java/graphql/annotations/GraphQLDirectivesViaClassDefinitionTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public static class UpperCaseNoDefault {
207207
}
208208

209209

210-
@Test(enabled = false) // TODO there is issue with coercing in DirectivesBuilder
210+
@Test
211211
public void queryName_directivesProvidedToRegistry_wiringIsActivated() throws Exception {
212212
this.graphQLAnnotations.directive(UpperCase.class);
213213

@@ -218,21 +218,21 @@ public void queryName_directivesProvidedToRegistry_wiringIsActivated() throws Ex
218218
assertEquals(((Map<String, String>) result.getData()).get("name").toString(), "YARIN");
219219
}
220220

221-
@Test(enabled = false) // TODO there is issue with coercing in DirectivesBuilder
221+
@Test
222222
public void queryNameWithFalse_directivesProvidedToRegistry_wiringIsActivated() throws Exception {
223223
GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean))
224224
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build();
225225
this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(upperCase.getName(), new DirectiveAndWiring(upperCase, UpperWiring.class));
226226
GraphQLObjectType object = this.graphQLAnnotations.object(Query.class);
227227
GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build();
228-
GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build();
228+
GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).additionalDirective( upperCase ).build();
229229

230230
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithFalse }");
231231
assertTrue(result.getErrors().isEmpty());
232232
assertEquals(((Map<String, String>) result.getData()).get("nameWithFalse").toString(), "yarin");
233233
}
234234

235-
@Test(enabled = false) // TODO there is issue with coercing in DirectivesBuilder
235+
@Test
236236
public void queryNameWithNoArgs_directivesProvidedToRegistry_wiringIsActivated() throws Exception {
237237
GraphQLSchema schema = newAnnotationsSchema().query(Query2.class).directive(UpperCaseNoDefault.class).build();
238238

@@ -248,7 +248,7 @@ public void queryNameWithNoArgs_noDefaultValue_exceptionIsThrown() throws Except
248248
GraphQL.newGraphQL(schema).build().execute("query { nameWithNoArgs }");
249249
}
250250

251-
@Test(enabled = false) // TODO there is issue with coercing in DirectivesBuilder
251+
@Test
252252
public void queryName_chainedDirectives_wiringIsActivatedInCorrectOrder() throws Exception {
253253
GraphQLSchema schema = newAnnotationsSchema().query(Query3.class).directives(SuffixDirective.class, UpperCase.class).build();
254254

0 commit comments

Comments
 (0)