Skip to content

Commit 3783436

Browse files
jminijmartisk
authored andcommitted
Skip empty types in union
1 parent a2cc01d commit 3783436

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

client/implementation/src/main/java/io/smallrye/graphql/client/impl/typesafe/QueryBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ private String recursionCheckedFields(TypeInfo type) {
9595
}
9696

9797
private String fieldsFragment(TypeInfo typeInfo) {
98+
if (typeInfo.fields().findAny().isEmpty()) {
99+
return "";
100+
}
98101
return "... on " + typeInfo.getGraphQlTypeName() + fields(typeInfo);
99102
}
100103

client/tck/src/main/java/tck/graphql/typesafe/UnionBehavior.java

+46
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,34 @@ String getMessage() {
4242
}
4343
}
4444

45+
@Union
46+
@JsonbTypeInfo(key = "__typename", value = {
47+
@JsonbSubtype(alias = "Data", type = Data.class),
48+
@JsonbSubtype(alias = "Hidden", type = Hidden.class),
49+
})
50+
public interface DetailsResponse {
51+
}
52+
53+
@Type("SuperHeroDetailsData")
54+
public static class Data implements DetailsResponse {
55+
String data;
56+
57+
String getData() {
58+
return data;
59+
}
60+
}
61+
62+
@Type("SuperHeroDetailsHidden")
63+
public static class Hidden implements DetailsResponse {
64+
}
65+
4566
@GraphQLClientApi
4667
interface UnionApi {
4768
SuperHeroResponse find(String name);
4869

4970
List<SuperHeroResponse> findAll(String name);
71+
72+
List<DetailsResponse> details(String name);
5073
}
5174

5275
@Test
@@ -118,4 +141,27 @@ void shouldUnionFindAll() {
118141
then(response.get(1)).isInstanceOf(NotFound.class);
119142
then(((NotFound) response.get(1)).getMessage()).isEqualTo("The Nobody");
120143
}
144+
145+
@Test
146+
void shouldSkipEmptyTypes() {
147+
fixture.returnsData("'details':[" +
148+
"{'__typename':'SuperHeroDetailsData','data':'Speed'}," +
149+
"{'__typename':'SuperHeroDetailsData','data':'Agility'}," +
150+
"{'__typename':'SuperHeroDetailsHidden'}" +
151+
"]");
152+
var api = fixture.build(UnionApi.class);
153+
154+
var response = api.details("Spider-Man");
155+
156+
then(fixture.query()).isEqualTo("query details($name: String) { details(name: $name){" +
157+
"__typename " +
158+
"... on SuperHeroDetailsData {data} } }");
159+
then(fixture.variables()).isEqualTo("{'name':'Spider-Man'}");
160+
then(response).hasSize(3);
161+
then(response.get(0)).isInstanceOf(Data.class);
162+
then(((Data) response.get(0)).getData()).isEqualTo("Speed");
163+
then(response.get(1)).isInstanceOf(Data.class);
164+
then(((Data) response.get(1)).getData()).isEqualTo("Agility");
165+
then(response.get(2)).isInstanceOf(Hidden.class);
166+
}
121167
}

0 commit comments

Comments
 (0)