Skip to content

Commit a458016

Browse files
committed
#2: Allow query and mutation classes to extend classes and implement multiple interfaces
1 parent bc517e3 commit a458016

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

src/main/java/graphql/annotations/processor/retrievers/GraphQLObjectInfoRetriever.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import java.lang.reflect.Method;
2424
import java.util.Arrays;
2525
import java.util.Comparator;
26+
import java.util.LinkedHashMap;
2627
import java.util.List;
28+
import java.util.Map;
2729
import java.util.stream.Collectors;
2830

2931
import static graphql.annotations.processor.util.NamingKit.toGraphqlName;
@@ -36,12 +38,29 @@ public String getTypeName(Class<?> objectClass) {
3638
return toGraphqlName(name == null ? objectClass.getSimpleName() : name.value());
3739
}
3840

39-
public List<Method> getOrderedMethods(Class c) {
40-
return Arrays.stream(c.getMethods())
41+
public List<Method> getOrderedMethods(Class<?> c) {
42+
var methods = new LinkedHashMap<String, Method>();
43+
collectMethods(c, methods);
44+
return methods.values().stream()
4145
.sorted(Comparator.comparing(Method::getName))
4246
.collect(Collectors.toList());
4347
}
4448

49+
private void collectMethods(Class<?> c, Map<String, Method> methods) {
50+
if (c == null) {
51+
return;
52+
}
53+
54+
Arrays.stream(c.getDeclaredMethods())
55+
.forEach(method -> methods.putIfAbsent(method.getName(), method));
56+
57+
for (Class<?> iface : c.getInterfaces()) {
58+
collectMethods(iface, methods);
59+
}
60+
61+
collectMethods(c.getSuperclass(), methods);
62+
}
63+
4564
public Boolean isGraphQLField(AnnotatedElement element) {
4665
GraphQLField annotation = element.getAnnotation(GraphQLField.class);
4766
if (annotation == null) {
@@ -50,5 +69,4 @@ public Boolean isGraphQLField(AnnotatedElement element) {
5069
return annotation.value();
5170
}
5271

53-
5472
}

src/test/java/graphql/annotations/AnnotationsSchemaCreatorTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,63 @@ public void build_Mutation_SchemaIsCreatedWithMutation() {
9696
assertThat(mutationType.getFieldDefinitions().size(), is(1));
9797
}
9898

99+
public interface BaseQuery {
100+
@GraphQLField
101+
int baseQuery();
102+
}
103+
104+
public interface IntermediateQuery extends BaseQuery {
105+
@GraphQLField
106+
int intermediateQuery();
107+
}
108+
109+
public interface InheritedQuery extends IntermediateQuery {
110+
@GraphQLField
111+
int inheritedQuery();
112+
}
113+
114+
public interface BaseMutation {
115+
@GraphQLField
116+
int baseMutation();
117+
}
118+
119+
public static class ParentMutation implements BaseMutation {
120+
@Override
121+
public int baseMutation() {
122+
return 6;
123+
}
124+
125+
@GraphQLField
126+
public int parentMutation() {
127+
return 7;
128+
}
129+
}
130+
131+
public static class InheritedMutation extends ParentMutation {
132+
@GraphQLField
133+
public int inheritedMutation() {
134+
return 8;
135+
}
136+
}
137+
138+
@Test
139+
public void build_QueryAndMutationWithInheritedTypes_SchemaContainsInheritedFields() {
140+
// arrange + act
141+
GraphQLSchema schema = builder.query(InheritedQuery.class).mutation(InheritedMutation.class).build();
142+
GraphQLObjectType queryType = schema.getQueryType();
143+
GraphQLObjectType mutationType = schema.getMutationType();
144+
145+
// assert
146+
assertThat(queryType.getFieldDefinition("baseQuery"), notNullValue());
147+
assertThat(queryType.getFieldDefinition("intermediateQuery"), notNullValue());
148+
assertThat(queryType.getFieldDefinition("inheritedQuery"), notNullValue());
149+
assertThat(queryType.getFieldDefinitions().size(), is(3));
150+
assertThat(mutationType.getFieldDefinition("baseMutation"), notNullValue());
151+
assertThat(mutationType.getFieldDefinition("parentMutation"), notNullValue());
152+
assertThat(mutationType.getFieldDefinition("inheritedMutation"), notNullValue());
153+
assertThat(mutationType.getFieldDefinitions().size(), is(3));
154+
}
155+
99156
public static class SubscriptionTest {
100157
@GraphQLField
101158
public int subscribe() {

0 commit comments

Comments
 (0)