Skip to content

Commit 5db82e6

Browse files
mrdon-atlassianbbakerman
authored andcommitted
Address PR feedback, add docs
1 parent b22c993 commit 5db82e6

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

docs/concerns.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It does not concern itself about other high level application concerns such as t
1010
- Data authorisation
1111
- Data pagination
1212
- HTTP transfer
13-
- JSON endoding
13+
- JSON encoding
1414
- Code wiring via dependency injection
1515

1616
You need to push these concerns into your business logic layers.

docs/execution.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ behaviour.
137137
};
138138
ExecutionStrategy executionStrategy = new AsyncExecutionStrategy(handler);
139139
140+
Returning data and errors
141+
^^^^^^^^^^^^^^^^^^^^^^^^^
142+
143+
It is also possible to return both data and multiple errors in a ``DataFetcher`` implementation by returning
144+
`graphql.execution.DataFetcherResult` either directly or wrapped in a ``CompletableFuture`` instance for asynchronous
145+
execution. This is a useful when your ``DataFetcher`` may need to retrieve data from multiple sources or from another
146+
GraphQL resource.
147+
148+
In this example, the ``DataFetcher`` retrieves a user from another GraphQL resource and returns its data and errors.
149+
150+
.. code-block:: java
151+
152+
DataFetcher userDataFetcher = new DataFetcher() {
153+
@Override
154+
public Object get(DataFetchingEnvironment environment) {
155+
Map response = fetchUserFromRemoteGraphQLResource(environment.getArgument("userId"));
156+
List<GraphQLError> errors = ((List)response.get("errors")).stream()
157+
.map(MyMapGraphQLError::new)
158+
.collect(Collectors.toList());
159+
return new DataFetcherResult(response.get("data"), errors);
160+
}
161+
};
162+
140163
Serializing results to JSON
141164
---------------------------
142165

src/main/java/graphql/execution/AbsoluteGraphQLError.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class AbsoluteGraphQLError implements GraphQLError {
2020
private final List<SourceLocation> locations;
2121
private final List<Object> absolutePath;
2222
private final GraphQLError relativeError;
23+
private final String message;
24+
private final ErrorType errorType;
2325

2426
AbsoluteGraphQLError(ExecutionStrategyParameters executionStrategyParameters, GraphQLError relativeError) {
2527
requireNonNull(executionStrategyParameters);
@@ -35,7 +37,7 @@ class AbsoluteGraphQLError implements GraphQLError {
3537

3638
Optional<SourceLocation> baseLocation;
3739
if (!executionStrategyParameters.field().isEmpty()) {
38-
baseLocation = Optional.of(executionStrategyParameters.field().get(0).getSourceLocation());
40+
baseLocation = Optional.ofNullable(executionStrategyParameters.field().get(0).getSourceLocation());
3941
} else {
4042
baseLocation = Optional.empty();
4143
}
@@ -49,11 +51,13 @@ class AbsoluteGraphQLError implements GraphQLError {
4951
.orElse(null))
5052
.collect(Collectors.toList()))
5153
.orElse(null);
54+
this.message = relativeError.getMessage();
55+
this.errorType = relativeError.getErrorType();
5256
}
5357

5458
@Override
5559
public String getMessage() {
56-
return relativeError.getMessage();
60+
return message;
5761
}
5862

5963
@Override
@@ -63,7 +67,7 @@ public List<SourceLocation> getLocations() {
6367

6468
@Override
6569
public ErrorType getErrorType() {
66-
return relativeError.getErrorType();
70+
return errorType;
6771
}
6872

6973
@Override

src/main/java/graphql/execution/DataFetcherResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public DataFetcherResult(T data, List<GraphQLError> errors) {
2727
}
2828

2929
/**
30-
* @return The data fetched. May be null
30+
* @return The data fetched. May be null.
3131
*/
3232
public T getData() {
3333
return data;
3434
}
3535

3636
/**
37-
* @return errors encountered when fetching data. Must not be null.
37+
* @return errors encountered when fetching data. This will be non null but possibly empty.
3838
*/
3939
public List<GraphQLError> getErrors() {
4040
return errors;

0 commit comments

Comments
 (0)