Skip to content

Commit f480d56

Browse files
author
Mridang Agarwalla
committed
Added support for deserialising Optional collection types
1 parent 6b1c6ef commit f480d56

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Mon Nov 30 23:20:00 EET 2020
2+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
13
distributionBase=GRADLE_USER_HOME
24
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
4-
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
zipStoreBase=GRADLE_USER_HOME

src/main/java/graphql/annotations/dataFetchers/MethodDataFetcher.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ private Object[] invocationArgs(DataFetchingEnvironment environment, ProcessingE
131131
return result.toArray();
132132
}
133133

134+
@SuppressWarnings("ConstantConditions")
134135
private Object buildArg(Type p, GraphQLType graphQLType, Optional<Object> arg) {
135136
if (arg == null) {
136137
return null;
@@ -140,7 +141,7 @@ private Object buildArg(Type p, GraphQLType graphQLType, Optional<Object> arg) {
140141
}
141142

142143
if (p instanceof Class<?> && graphQLType instanceof GraphQLInputObjectType) {
143-
Constructor<?> constructors[] = ((Class) p).getConstructors();
144+
Constructor<?>[] constructors = ((Class) p).getConstructors();
144145
Constructor<?> constructor = getBuildArgConstructor(constructors);
145146
Parameter[] parameters = constructor.getParameters();
146147

@@ -156,15 +157,23 @@ private Object buildArg(Type p, GraphQLType graphQLType, Optional<Object> arg) {
156157
return constructNewInstance(constructor, objects.toArray(new Object[objects.size()]));
157158
}
158159
} else if (p instanceof ParameterizedType && graphQLType instanceof GraphQLList) {
159-
List<Object> list = new ArrayList<>();
160-
Type subType = ((ParameterizedType) p).getActualTypeArguments()[0];
161-
GraphQLType wrappedType = ((GraphQLList) graphQLType).getWrappedType();
160+
if (((ParameterizedType) p).getRawType() == Optional.class) {
161+
if (arg == null) {
162+
return null;
163+
} else {
164+
Type subType = ((ParameterizedType) p).getActualTypeArguments()[0];
165+
return Optional.ofNullable(buildArg(subType, graphQLType, arg));
166+
}
167+
} else {
168+
List<Object> list = new ArrayList<>();
169+
Type subType = ((ParameterizedType) p).getActualTypeArguments()[0];
170+
GraphQLType wrappedType = ((GraphQLList) graphQLType).getWrappedType();
162171

163-
for (Object item : ((List) arg.get())) {
164-
list.add(buildArg(subType, wrappedType, Optional.ofNullable(item)));
172+
for (Object item : ((List) arg.get())) {
173+
list.add(buildArg(subType, wrappedType, Optional.ofNullable(item)));
174+
}
175+
return list;
165176
}
166-
167-
return list;
168177
} else if (p instanceof ParameterizedType && ((ParameterizedType) p).getRawType() == Optional.class) {
169178
Type subType = ((ParameterizedType) p).getActualTypeArguments()[0];
170179
if (arg == null) {

src/test/java/graphql/annotations/GraphQLInputTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ public String something(@GraphQLName("code") AnotherCode code) {
169169
public String otherthing(@GraphQLName("code") AnotherCode code) {
170170
return (code.firstField != null ? code.firstField.orElse("") : "") + code.secondField;
171171
}
172+
173+
@SuppressWarnings({"unused", "OptionalAssignedToNull", "OptionalUsedAsFieldOrParameterType"})
174+
@GraphQLField
175+
public String listthings(@GraphQLName("codes") Optional<List<AnotherCode>> code) {
176+
return code == null ? "was null" : (code.map(anotherCodes -> "code was " + anotherCodes.size()).orElse("was empty"));
177+
}
172178
}
173179

174180
public static class QueryMultipleDefinitions {
@@ -232,10 +238,11 @@ public void queryWithUndefinableParameters() {
232238
GraphQLSchema schema = newAnnotationsSchema().query(QueryUndefinedParameter.class).build();
233239

234240
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
235-
ExecutionResult result = graphQL.execute("{ something(code: {firstField:\"a\",secondField:\"b\"}) otherthing(code: {secondField:\"c\"}) }", new QueryUndefinedParameter());
241+
ExecutionResult result = graphQL.execute("{ something(code: {firstField:\"a\",secondField:\"b\"}) otherthing(code: {secondField:\"c\"}) listthings }", new QueryUndefinedParameter());
236242
assertTrue(result.getErrors().isEmpty());
237243
assertEquals(((Map<String, String>) result.getData()).get("something"), "ab");
238244
assertEquals(((Map<String, String>) result.getData()).get("otherthing"), "c");
245+
assertEquals(((Map<String, String>) result.getData()).get("listthings"), "was null");
239246
}
240247

241248
@Test

0 commit comments

Comments
 (0)