diff --git a/src/main/java/io/spring/projectapi/contentful/ContentfulQueries.java b/src/main/java/io/spring/projectapi/contentful/ContentfulQueries.java index 6be1633..ae88710 100644 --- a/src/main/java/io/spring/projectapi/contentful/ContentfulQueries.java +++ b/src/main/java/io/spring/projectapi/contentful/ContentfulQueries.java @@ -17,6 +17,7 @@ package io.spring.projectapi.contentful; import java.time.Duration; +import java.util.Collections; import java.util.List; import java.util.function.Function; @@ -76,7 +77,13 @@ private ClientGraphQlResponse executeForSingleProject(String documentName, Strin } private List fieldToEntityList(ClientGraphQlResponse response, String path, Class type) { - return field(response, path, (field) -> field.toEntityList(type)); + return field(response, path, (field) -> getList(type, response, path)); + } + + private static List getList(Class type, ClientGraphQlResponse response, String path) { + ClientResponseField field = response.field(path); + return (!field.hasValue() && response.getErrors().isEmpty()) ? Collections.emptyList() + : field.toEntityList(type); } private T fieldToEntity(ClientGraphQlResponse response, String path, Class type) { diff --git a/src/test/java/io/spring/projectapi/contentful/ContentfulQueriesTests.java b/src/test/java/io/spring/projectapi/contentful/ContentfulQueriesTests.java index 0b5a32d..a84c79b 100644 --- a/src/test/java/io/spring/projectapi/contentful/ContentfulQueriesTests.java +++ b/src/test/java/io/spring/projectapi/contentful/ContentfulQueriesTests.java @@ -133,6 +133,13 @@ void getProjectSupportsWhenNoProjectMatchThrowsException() throws IOException { .satisfies((ex) -> assertThat(ex.getProjectSlug()).isEqualTo("spring-xd")); } + @Test + void getProjectSupportsWhenNullReturnsEmptyList() throws IOException { + setupResponse("query-null-support.json"); + List supports = this.contentfulQueries.getProjectSupports("spring-xd"); + assertThat(supports).isEmpty(); + } + private void setupResponse(String name) throws IOException { setupResponse(new ClassPathResource(name, getClass())); } diff --git a/src/test/resources/io/spring/projectapi/contentful/query-error.json b/src/test/resources/io/spring/projectapi/contentful/query-error.json index 30c7e17..536eb25 100644 --- a/src/test/resources/io/spring/projectapi/contentful/query-error.json +++ b/src/test/resources/io/spring/projectapi/contentful/query-error.json @@ -1,4 +1,15 @@ { "data": { - } + }, + "errors": [ + { + "message": "Cannot query field \"a\" on type \"Project\".", + "locations": [ + { + "line": 4, + "column": 4 + } + ] + } + ] } diff --git a/src/test/resources/io/spring/projectapi/contentful/query-null-support.json b/src/test/resources/io/spring/projectapi/contentful/query-null-support.json new file mode 100644 index 0000000..22668e9 --- /dev/null +++ b/src/test/resources/io/spring/projectapi/contentful/query-null-support.json @@ -0,0 +1,11 @@ +{ + "data": { + "projectCollection": { + "items": [ + { + "support": null + } + ] + } + } +} \ No newline at end of file