Skip to content

Commit af856d2

Browse files
lassetyryrashk
authored andcommitted
Including Set as possible GraphQLType (#57)
1 parent 836a558 commit af856d2

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/main/java/graphql/annotations/DefaultTypeFunction.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
139139
}
140140
}
141141

142+
private class SetFunction implements TypeFunction {
143+
144+
@Override
145+
public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
146+
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
147+
throw new IllegalArgumentException("List type parameter should be specified");
148+
}
149+
AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType;
150+
AnnotatedType arg = parameterizedType.getAnnotatedActualTypeArguments()[0];
151+
Class<?> klass;
152+
if (arg.getType() instanceof ParameterizedType) {
153+
klass = (Class<?>)((ParameterizedType)(arg.getType())).getRawType();
154+
} else {
155+
klass = (Class<?>) arg.getType();
156+
}
157+
return new GraphQLList(DefaultTypeFunction.this.apply(klass, arg));
158+
}
159+
160+
@Override public Collection<Class<?>> getAcceptedTypes() {
161+
return Arrays.asList(Set.class);
162+
}
163+
}
164+
142165
private class StreamFunction implements TypeFunction {
143166

144167
@Override
@@ -266,6 +289,7 @@ public DefaultTypeFunction() {
266289

267290
register(new ListFunction());
268291
register(new StreamFunction());
292+
register(new SetFunction());
269293

270294
register(new EnumFunction());
271295

src/test/java/graphql/annotations/DefaultTypeFunctionTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
import graphql.schema.GraphQLType;
1919
import org.testng.annotations.Test;
2020

21-
import java.util.Arrays;
22-
import java.util.LinkedList;
23-
import java.util.List;
24-
import java.util.Optional;
21+
import java.util.*;
2522
import java.util.stream.Collectors;
2623
import java.util.stream.Stream;
2724

@@ -92,6 +89,9 @@ public void long_() {
9289
@SuppressWarnings("unused")
9390
public Stream<List<@GraphQLNonNull String>> streamMethod() { return null;}
9491

92+
@SuppressWarnings("unused")
93+
public Set<Set<@GraphQLNonNull String>> setMethod() { return null;}
94+
9595
@Test
9696
public void list() throws NoSuchMethodException {
9797
DefaultTypeFunction instance = new DefaultTypeFunction();
@@ -103,6 +103,17 @@ public void list() throws NoSuchMethodException {
103103
assertEquals(wrappedType.getWrappedType(), GraphQLString);
104104
}
105105

106+
@Test
107+
public void set() throws NoSuchMethodException {
108+
DefaultTypeFunction instance = new DefaultTypeFunction();
109+
graphql.schema.GraphQLType type = instance.apply(getClass().getMethod("setMethod").getReturnType(), getClass().getMethod("setMethod").getAnnotatedReturnType());
110+
assertTrue(type instanceof GraphQLList);
111+
GraphQLList subtype = (GraphQLList) ((GraphQLList) type).getWrappedType();
112+
assertTrue(subtype.getWrappedType() instanceof graphql.schema.GraphQLNonNull);
113+
graphql.schema.GraphQLNonNull wrappedType = (graphql.schema.GraphQLNonNull) subtype.getWrappedType();
114+
assertEquals(wrappedType.getWrappedType(), GraphQLString);
115+
}
116+
106117
@Test(expectedExceptions = IllegalArgumentException.class)
107118
public void unparametrizedList() {
108119
DefaultTypeFunction instance = new DefaultTypeFunction();

0 commit comments

Comments
 (0)