Skip to content

Commit fc3ad76

Browse files
Allow interfaces to be mapped as types or inputs
Signed-off-by:Phillip Kruger <phillip.kruger@gmail.com>
1 parent a1334cd commit fc3ad76

5 files changed

Lines changed: 91 additions & 3 deletions

File tree

common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/ReferenceCreator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ public Reference createReference(Direction direction, ClassInfo classInfo, boole
177177
// we figure out this is actually an enum or interface
178178
ReferenceType referenceType = getCorrectReferenceType(direction);
179179

180+
Annotations annotationsForClass = Annotations.getAnnotationsForClass(classInfo);
181+
180182
// Now check if this is an interface or enum
181-
if (Classes.isInterface(classInfo)) {
183+
if (isInterface(classInfo, annotationsForClass)) {
182184
// Also check that we create all implementations
183185
Collection<ClassInfo> knownDirectImplementors = ScanningContext.getIndex()
184186
.getAllKnownImplementors(classInfo.name());
@@ -218,7 +220,7 @@ public Reference createReference(Direction direction, ClassInfo classInfo, boole
218220

219221
// Now we should have the correct reference type.
220222
String className = classInfo.name().toString();
221-
Annotations annotationsForClass = Annotations.getAnnotationsForClass(classInfo);
223+
222224
String name = TypeNameHelper.getAnyTypeName(
223225
addParametrizedTypeNameExtension
224226
? TypeNameHelper.createParametrizedTypeNameExtension(parametrizedTypeArgumentsReferences)
@@ -241,6 +243,18 @@ public Reference createReference(Direction direction, ClassInfo classInfo, boole
241243
return reference;
242244
}
243245

246+
private boolean isInterface(ClassInfo classInfo, Annotations annotationsForClass) {
247+
boolean isJavaInterface = Classes.isInterface(classInfo);
248+
if (isJavaInterface) {
249+
if (annotationsForClass.containsOneOfTheseAnnotations(Annotations.TYPE, Annotations.INPUT)) {
250+
// This should be mapped to a type/input and not an interface
251+
return false;
252+
}
253+
return true;
254+
}
255+
return false;
256+
}
257+
244258
private Reference getReference(Direction direction, Type fieldType, Type methodType, Annotations annotations) {
245259
return getReference(direction, fieldType, methodType, annotations, null);
246260
}

server/tck/src/test/java/io/smallrye/graphql/SmallRyeGraphQLArchiveProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.smallrye.graphql.test.apps.fieldexistence.api.FieldExistenceApi;
2323
import io.smallrye.graphql.test.apps.generics.api.ControllerWithGenerics;
2424
import io.smallrye.graphql.test.apps.grouping.api.BookGraphQLApi;
25+
import io.smallrye.graphql.test.apps.interfaces.api.FoodResource;
2526
import io.smallrye.graphql.test.apps.jsonp.api.JsonPApi;
2627
import io.smallrye.graphql.test.apps.mapping.api.MappingResource;
2728
import io.smallrye.graphql.test.apps.mutiny.api.MutinyApi;
@@ -99,7 +100,7 @@ public void process(Archive<?> applicationArchive, TestClass testClass) {
99100
war.addPackage(CreatorApi.class.getPackage());
100101
war.addPackage(EnumListApi.class.getPackage());
101102
war.addPackage(ExceptionListApi.class.getPackage());
102-
103+
war.addPackage(FoodResource.class.getPackage());
103104
}
104105
}
105106
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.smallrye.graphql.test.apps.interfaces.api;
2+
3+
import org.eclipse.microprofile.graphql.Type;
4+
5+
/**
6+
* Define something that is fit to be consumed as food
7+
*
8+
* @author Phillip Kruger (phillip.kruger@redhat.com)
9+
*/
10+
@Type
11+
public interface Eatable {
12+
13+
public String getColor();
14+
15+
public void setColor(String color);
16+
17+
public String getName();
18+
19+
public void setName(String name);
20+
21+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.smallrye.graphql.test.apps.interfaces.api;
2+
3+
import org.eclipse.microprofile.graphql.GraphQLApi;
4+
import org.eclipse.microprofile.graphql.Query;
5+
6+
@GraphQLApi
7+
public class FoodResource {
8+
9+
@Query
10+
public Eatable getApple() {
11+
return new Apple("Granny Smith", "green");
12+
}
13+
14+
public static class Apple implements Eatable {
15+
16+
private String name;
17+
private String color;
18+
19+
public Apple() {
20+
}
21+
22+
public Apple(String name, String color) {
23+
this.name = name;
24+
this.color = color;
25+
}
26+
27+
@Override
28+
public String getColor() {
29+
return color;
30+
}
31+
32+
@Override
33+
public void setColor(String color) {
34+
this.color = color;
35+
}
36+
37+
@Override
38+
public String getName() {
39+
return name;
40+
}
41+
42+
@Override
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
}
48+
49+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# testInterfaceAsType
2+
1| type Query | apple: Eatable | Eatable should be mapped to a type in Query
3+
2| type Eatable | color: String | Eatable should be mapped to a type

0 commit comments

Comments
 (0)