Skip to content

Commit 35cd56e

Browse files
committed
Improve error support
1 parent 415d12f commit 35cd56e

File tree

8 files changed

+79
-16
lines changed

8 files changed

+79
-16
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=1.0.0-SNAPSHOT
1+
version=0.0.1b202003301508
22

33
PROJECT_GROUP = com.graphql-java-kickstart
44
PROJECT_NAME = graphql-spring-webclient
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package graphql.kickstart.spring.webclient.boot;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import lombok.Data;
6+
7+
@Data
8+
public class GraphQLError {
9+
10+
private String message;
11+
private List<Location> locations;
12+
private List<String> path;
13+
private Map<String, Object> extensions ;
14+
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package graphql.kickstart.spring.webclient.boot;
2+
3+
import java.util.List;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.Value;
7+
8+
@Value
9+
@EqualsAndHashCode(callSuper = false)
10+
@RequiredArgsConstructor
11+
public class GraphQLErrorsException extends RuntimeException {
12+
13+
List<GraphQLError> errors;
14+
15+
@Override
16+
public String getMessage() {
17+
return errors.get(0).getMessage();
18+
}
19+
20+
}

graphql-webclient/src/main/java/graphql/kickstart/spring/webclient/boot/GraphQLResponse.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@
33
import java.util.List;
44
import java.util.Map;
55
import java.util.Map.Entry;
6+
import java.util.NoSuchElementException;
67
import lombok.Data;
78

89
@Data
910
class GraphQLResponse {
1011

1112
private Map<String, Object> data;
12-
private List<String> errors;
13+
private List<GraphQLError> errors;
1314

1415
Object getFirstObject() {
15-
return data.entrySet().stream().findFirst().map(Entry::getValue).orElseThrow();
16+
validateNoErrors();
17+
18+
if (data != null) {
19+
return data.entrySet().stream().findFirst().map(Entry::getValue).orElseThrow(NoSuchElementException::new);
20+
}
21+
throw new NoSuchElementException();
22+
}
23+
24+
private void validateNoErrors() {
25+
if (errors != null && !errors.isEmpty()) {
26+
throw new GraphQLErrorsException(errors);
27+
}
1628
}
1729

1830
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package graphql.kickstart.spring.webclient.boot;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Location {
7+
8+
private int line;
9+
private int column;
10+
11+
}

graphql-webclient/src/test/java/graphql/kickstart/spring/webclient/boot/GraphQLWebClientTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
56

67
import com.fasterxml.jackson.databind.ObjectMapper;
78
import graphql.kickstart.spring.webclient.testapp.Simple;
8-
import java.util.HashMap;
99
import java.util.Map;
1010
import org.junit.jupiter.api.BeforeEach;
1111
import org.junit.jupiter.api.DisplayName;
@@ -47,23 +47,25 @@ void queryWithoutVariablesSucceeds() {
4747
@Test
4848
@DisplayName("Query echo with String variable and returning String")
4949
void echoStringSucceeds() {
50-
Map<String, Object> variables = new HashMap<>();
51-
variables.put("value", "echo echo echo");
52-
Mono<String> response = graphqlClient.post("query-echo.graphql", variables, String.class);
50+
Mono<String> response = graphqlClient.post("query-echo.graphql", Map.of("value", "echo echo echo"), String.class);
5351
assertNotNull("response should not be null", response);
5452
assertEquals("response should equal 'echo echo echo'", "echo echo echo", response.block());
5553
}
5654

5755
@Test
5856
@DisplayName("Query simple return type")
5957
void simpleTypeSucceeds() {
60-
Map<String, Object> variables = new HashMap<>();
61-
variables.put("id", "my-id");
62-
Mono<Simple> response = graphqlClient.post("query-simple.graphql", variables, Simple.class);
58+
Mono<Simple> response = graphqlClient.post("query-simple.graphql", Map.of("id", "my-id"), Simple.class);
6359
assertNotNull("response should not be null", response);
6460
Simple object = response.block();
6561
assertNotNull(object);
6662
assertEquals("response id should equal 'my-id'", "my-id", object.getId());
6763
}
6864

65+
@Test
66+
void errorResponseSucceeds() {
67+
Mono<String> response = graphqlClient.post("error.graphql", String.class);
68+
assertThrows(GraphQLErrorsException.class, response::block);
69+
}
70+
6971
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query {
2+
error
3+
}

travis-build.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ EOL
1515
}
1616

1717
if [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${TRAVIS_BRANCH}" = "master" ]; then
18-
if [ "${RELEASE}" = "true" ]; then
18+
# if [ "${RELEASE}" = "true" ]; then
1919
echo "Deploying release to Bintray"
2020
saveGitCredentials
2121
./gradlew clean assemble && ./gradlew check --info && ./gradlew bintrayUpload -x check --info
22-
else
23-
echo "Deploying snapshot"
24-
saveGitCredentials
25-
./gradlew artifactoryPublish -Dsnapshot=true -Dbuild.number="${TRAVIS_BUILD_NUMBER}"
26-
fi
22+
# else
23+
# echo "Deploying snapshot"
24+
# saveGitCredentials
25+
# ./gradlew artifactoryPublish -Dsnapshot=true -Dbuild.number="${TRAVIS_BUILD_NUMBER}"
26+
# fi
2727
else
2828
echo "Verify"
2929
./gradlew clean assemble && ./gradlew check --info

0 commit comments

Comments
 (0)