Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip empty types in union #2264

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ private String recursionCheckedFields(TypeInfo type) {
}

private String fieldsFragment(TypeInfo typeInfo) {
if (typeInfo.fields().findAny().isEmpty()) {
return "";
}
return "... on " + typeInfo.getGraphQlTypeName() + fields(typeInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ private String makeFirstLetterUppercase(String value) {
}

private String fieldsFragment(TypeModel type) {
if (type.fields().findAny().isEmpty()) {
return "";
}
return "... on " + type.getGraphQlTypeName() + fields(type);
}

Expand Down
46 changes: 46 additions & 0 deletions client/tck/src/main/java/tck/graphql/typesafe/UnionBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,34 @@ String getMessage() {
}
}

@Union
@JsonbTypeInfo(key = "__typename", value = {
@JsonbSubtype(alias = "Data", type = Data.class),
@JsonbSubtype(alias = "Hidden", type = Hidden.class),
})
public interface DetailsResponse {
}

@Type("SuperHeroDetailsData")
public static class Data implements DetailsResponse {
String data;

String getData() {
return data;
}
}

@Type("SuperHeroDetailsHidden")
public static class Hidden implements DetailsResponse {
}

@GraphQLClientApi
interface UnionApi {
SuperHeroResponse find(String name);

List<SuperHeroResponse> findAll(String name);

List<DetailsResponse> details(String name);
}

@Test
Expand Down Expand Up @@ -118,4 +141,27 @@ void shouldUnionFindAll() {
then(response.get(1)).isInstanceOf(NotFound.class);
then(((NotFound) response.get(1)).getMessage()).isEqualTo("The Nobody");
}

@Test
void shouldSkipEmptyTypes() {
fixture.returnsData("'details':[" +
"{'__typename':'SuperHeroDetailsData','data':'Speed'}," +
"{'__typename':'SuperHeroDetailsData','data':'Agility'}," +
"{'__typename':'SuperHeroDetailsHidden'}" +
"]");
var api = fixture.build(UnionApi.class);

var response = api.details("Spider-Man");

then(fixture.query()).isEqualTo("query details($name: String) { details(name: $name){" +
"__typename " +
"... on SuperHeroDetailsData {data} } }");
then(fixture.variables()).isEqualTo("{'name':'Spider-Man'}");
then(response).hasSize(3);
then(response.get(0)).isInstanceOf(Data.class);
then(((Data) response.get(0)).getData()).isEqualTo("Speed");
then(response.get(1)).isInstanceOf(Data.class);
then(((Data) response.get(1)).getData()).isEqualTo("Agility");
then(response.get(2)).isInstanceOf(Hidden.class);
}
}
Loading