-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathOnValidationErrorStrategy.java
52 lines (45 loc) · 2.01 KB
/
OnValidationErrorStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package graphql.validation.rules;
import graphql.GraphQLError;
import graphql.PublicSpi;
import graphql.execution.DataFetcherResult;
import graphql.schema.DataFetchingEnvironment;
import java.util.List;
/**
* A callback that indicates whether to continue the data fetching after validation errors are detected and what value should be
* returned if it decides to not continue.
* <p>
* {@link #RETURN_NULL} is a common strategy to use, that is return null as the value for an invalid field
*/
@PublicSpi
public interface OnValidationErrorStrategy {
/**
* This strategy will prevent the current data fetch and return null as a value along with the errors
*/
OnValidationErrorStrategy RETURN_NULL = new OnValidationErrorStrategy() {
@Override
public boolean shouldContinue(List<GraphQLError> errors, DataFetchingEnvironment environment) {
return false;
}
@Override
public Object onErrorValue(List<GraphQLError> errors, DataFetchingEnvironment environment) {
return DataFetcherResult.newResult().errors(errors).data(null).build();
}
};
/**
* This is called when there are validation errors present and it can decide whether to continue the current
* data fetch (and hence return null) or whether it should in fact continue on anyway.
*
* @param errors the list errors
* @param environment the environment in play
* @return true if the current data fetch should continue
*/
boolean shouldContinue(List<GraphQLError> errors, DataFetchingEnvironment environment);
/**
* This will be called to generate a value that should be returned if we decide NOT to continue via {@link #shouldContinue(java.util.List, graphql.schema.DataFetchingEnvironment)}.
*
* @param errors the list errors
* @param environment the environment in play
* @return an object (a sensible value would be null)
*/
Object onErrorValue(List<GraphQLError> errors, DataFetchingEnvironment environment);
}