Skip to content

Commit 2c5cac1

Browse files
committed
Rename to @nullonerror
1 parent 160abcd commit 2c5cac1

File tree

6 files changed

+33
-104
lines changed

6 files changed

+33
-104
lines changed

src/main/java/graphql/Directives.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Directives {
4242
private static final String SPECIFIED_BY = "specifiedBy";
4343
private static final String ONE_OF = "oneOf";
4444
private static final String DEFER = "defer";
45-
private static final String ERROR_HANDLING = "errorHandling";
45+
private static final String NULL_ON_ERROR = "nullOnError";
4646

4747
public static final DirectiveDefinition DEPRECATED_DIRECTIVE_DEFINITION;
4848
public static final DirectiveDefinition INCLUDE_DIRECTIVE_DEFINITION;
@@ -53,7 +53,7 @@ public class Directives {
5353
@ExperimentalApi
5454
public static final DirectiveDefinition DEFER_DIRECTIVE_DEFINITION;
5555
@ExperimentalApi
56-
public static final DirectiveDefinition ERROR_HANDLING_DIRECTIVE_DEFINITION;
56+
public static final DirectiveDefinition NULL_ON_ERROR_DIRECTIVE_DEFINITION;
5757

5858
public static final String BOOLEAN = "Boolean";
5959
public static final String STRING = "String";
@@ -141,19 +141,12 @@ public class Directives {
141141
.type(newTypeName().name(STRING).build())
142142
.build())
143143
.build();
144-
ERROR_HANDLING_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
145-
.name(ERROR_HANDLING)
144+
NULL_ON_ERROR_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
145+
.name(NULL_ON_ERROR)
146146
.directiveLocation(newDirectiveLocation().name(QUERY.name()).build())
147147
.directiveLocation(newDirectiveLocation().name(MUTATION.name()).build())
148148
.directiveLocation(newDirectiveLocation().name(SUBSCRIPTION.name()).build())
149-
.description(createDescription("This directive controls how to handle errors"))
150-
.inputValueDefinition(
151-
newInputValueDefinition()
152-
.name("onError")
153-
.type(newNonNullType(newTypeName().name(Enums.ON_ERROR).build()).build())
154-
.defaultValue(EnumValue.newEnumValue(Enums.ON_ERROR_PROPAGATE).build())
155-
.description(createDescription("How to handle errors."))
156-
.build())
149+
.description(createDescription("This directive allows returning null in non-null locations that have an associated error"))
157150
.build();
158151
}
159152

@@ -249,20 +242,11 @@ public class Directives {
249242
.build();
250243

251244
@ExperimentalApi
252-
public static final GraphQLDirective ErrorHandlingDirective = GraphQLDirective.newDirective()
253-
.name(ERROR_HANDLING)
254-
.description("This directive controls how to handle errors.")
255-
.argument(newArgument()
256-
.name("onError")
257-
.type(nonNull(GraphQLEnumType.newEnum()
258-
.name(Enums.ON_ERROR)
259-
.value(Enums.ON_ERROR_PROPAGATE)
260-
.value(Enums.ON_ERROR_NULL)
261-
.build()))
262-
.defaultValueProgrammatic(Enums.ON_ERROR_PROPAGATE)
263-
.description("How to handle errors."))
245+
public static final GraphQLDirective NullOnErrorDirective = GraphQLDirective.newDirective()
246+
.name(NULL_ON_ERROR)
247+
.description("This directive allows returning null in non-null locations that have an associated error.")
264248
.validLocations(QUERY, MUTATION, SUBSCRIPTION)
265-
.definition(ERROR_HANDLING_DIRECTIVE_DEFINITION)
249+
.definition(NULL_ON_ERROR_DIRECTIVE_DEFINITION)
266250
.build();
267251

268252
private static Description createDescription(String s) {

src/main/java/graphql/Enums.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/java/graphql/ExperimentalApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
String ENABLE_INCREMENTAL_SUPPORT = "ENABLE_INCREMENTAL_SUPPORT";
2727
/**
28-
* The key that should be associated with a boolean value which indicates whether @errorHandling behaviour is enabled for this execution.
28+
* The key that should be associated with a boolean value which indicates whether @nullOnError behaviour is enabled for this execution.
2929
*/
30-
String ENABLE_CUSTOM_ERROR_HANDLING = "ENABLE_CUSTOM_ERROR_HANDLING";
30+
String ENABLE_NULL_ON_ERROR = "ENABLE_NULL_ON_ERROR";
3131
}

src/main/java/graphql/execution/Execution.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package graphql.execution;
22

33

4-
import graphql.Assert;
5-
import graphql.Enums;
64
import graphql.ExecutionInput;
75
import graphql.ExecutionResult;
86
import graphql.ExecutionResultImpl;
@@ -35,13 +33,11 @@
3533

3634
import java.util.Collections;
3735
import java.util.List;
38-
import java.util.Locale;
3936
import java.util.Map;
4037
import java.util.Optional;
4138
import java.util.concurrent.CompletableFuture;
4239

43-
import static graphql.Directives.ERROR_HANDLING_DIRECTIVE_DEFINITION;
44-
import static graphql.Directives.ErrorHandlingDirective;
40+
import static graphql.Directives.NULL_ON_ERROR_DIRECTIVE_DEFINITION;
4541
import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
4642
import static graphql.execution.ExecutionStepInfo.newExecutionStepInfo;
4743
import static graphql.execution.ExecutionStrategyParameters.newParameters;
@@ -93,9 +89,9 @@ public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSche
9389
}
9490

9591
boolean customErrorPropagationEnabled = Optional.ofNullable(executionInput.getGraphQLContext())
96-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_CUSTOM_ERROR_HANDLING))
92+
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_NULL_ON_ERROR))
9793
.orElse(false);
98-
boolean propagateErrors = !customErrorPropagationEnabled || propagateErrors(coercedVariables, operationDefinition.getDirectives(), true);
94+
boolean propagateErrors = !customErrorPropagationEnabled || propagateErrors(operationDefinition.getDirectives(), true);
9995

10096
ExecutionContext executionContext = newExecutionContextBuilder()
10197
.instrumentation(instrumentation)
@@ -275,13 +271,10 @@ private ExecutionResult mergeExtensionsBuilderIfPresent(ExecutionResult executio
275271
return executionResult;
276272
}
277273

278-
private boolean propagateErrors(CoercedVariables variables, List<Directive> directives, boolean defaultValue) {
279-
Directive foundDirective = NodeUtil.findNodeByName(directives, ERROR_HANDLING_DIRECTIVE_DEFINITION.getName());
274+
private boolean propagateErrors(List<Directive> directives, boolean defaultValue) {
275+
Directive foundDirective = NodeUtil.findNodeByName(directives, NULL_ON_ERROR_DIRECTIVE_DEFINITION.getName());
280276
if (foundDirective != null) {
281-
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(ErrorHandlingDirective.getArguments(), foundDirective.getArguments(), variables, GraphQLContext.getDefault(), Locale.getDefault());
282-
Object argumentValue = argumentValues.get("onError");
283-
Assert.assertTrue(argumentValue instanceof String, "The '%s' directive MUST have an OnError value for the 'onError' argument", ERROR_HANDLING_DIRECTIVE_DEFINITION.getName());
284-
return argumentValue.equals(Enums.ON_ERROR_PROPAGATE);
277+
return false;
285278
}
286279
return defaultValue;
287280
}

src/main/java/graphql/execution/ExecutionContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public ValueUnboxer getValueUnboxer() {
176176
/**
177177
* @return true if the current operation should propagate errors in non-null positions
178178
* Propagating errors is the default. Error aware clients may opt in returning null in non-null positions
179-
* by using the `@errorHandling` directive.
179+
* by using the `@nullOnError` directive.
180180
*/
181181
@ExperimentalApi
182182
public boolean propagateErrors() { return propagateErrors; }

src/test/groovy/graphql/execution/NoErrorPropagationTest.groovy

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,25 @@ import spock.lang.Specification
77

88
class NoErrorPropagationTest extends Specification {
99

10-
def "when onError is ALLOW_NULL null is returned"() {
10+
def "with nullOnError, null is returned"() {
1111

1212
def sdl = '''
1313
type Query {
1414
foo : Int!
1515
}
16-
enum OnError {
17-
ALLOW_NULL
18-
PROPAGATE
19-
}
20-
directive @errorHandling(onError: OnError) on QUERY | MUTATION | SUBSCRIPTION
16+
directive @nullOnError on QUERY | MUTATION | SUBSCRIPTION
2117
'''
2218

2319
def graphql = TestUtil.graphQL(sdl).build()
2420

2521
def query = '''
26-
query GetFoo @errorHandling(onError: ALLOW_NULL) { foo }
22+
query GetFoo @nullOnError { foo }
2723
'''
2824
when:
2925

3026
ExecutionInput ei = ExecutionInput.newExecutionInput(query).root(
3127
[foo: null]
32-
).graphQLContext([(ExperimentalApi.ENABLE_CUSTOM_ERROR_HANDLING): true])
28+
).graphQLContext([(ExperimentalApi.ENABLE_NULL_ON_ERROR): true])
3329
.build()
3430

3531
def er = graphql.execute(ei)
@@ -40,29 +36,25 @@ class NoErrorPropagationTest extends Specification {
4036
er.errors[0].path.toList() == ["foo"]
4137
}
4238

43-
def "when onError is PROPAGATE error is propagated"() {
39+
def "without nullOnError, error is propagated"() {
4440

4541
def sdl = '''
4642
type Query {
4743
foo : Int!
4844
}
49-
enum OnError {
50-
ALLOW_NULL
51-
PROPAGATE
52-
}
53-
directive @errorHandling(onError: OnError) on QUERY | MUTATION | SUBSCRIPTION
45+
directive @nullOnError on QUERY | MUTATION | SUBSCRIPTION
5446
'''
5547

5648
def graphql = TestUtil.graphQL(sdl).build()
5749

5850
def query = '''
59-
query GetFoo @errorHandling(onError: PROPAGATE) { foo }
51+
query GetFoo { foo }
6052
'''
6153
when:
6254

6355
ExecutionInput ei = ExecutionInput.newExecutionInput(query).root(
6456
[foo: null]
65-
).graphQLContext([(ExperimentalApi.ENABLE_CUSTOM_ERROR_HANDLING): true])
57+
).graphQLContext([(ExperimentalApi.ENABLE_NULL_ON_ERROR): true])
6658
.build()
6759

6860
def er = graphql.execute(ei)
@@ -74,23 +66,19 @@ class NoErrorPropagationTest extends Specification {
7466
}
7567

7668

77-
def "when custom error propagation is disabled error is propagated"() {
69+
def "when ENABLE_NULL_ON_ERROR is false, error is propagated"() {
7870

7971
def sdl = '''
8072
type Query {
8173
foo : Int!
8274
}
83-
enum OnError {
84-
ALLOW_NULL
85-
PROPAGATE
86-
}
87-
directive @errorHandling(onError: OnError) on QUERY | MUTATION | SUBSCRIPTION
75+
directive @nullOnError on QUERY | MUTATION | SUBSCRIPTION
8876
'''
8977

9078
def graphql = TestUtil.graphQL(sdl).build()
9179

9280
def query = '''
93-
query GetFoo @errorHandling(onError: ALLOW_NULL) { foo }
81+
query GetFoo @nullOnError { foo }
9482
'''
9583
when:
9684

@@ -105,7 +93,7 @@ class NoErrorPropagationTest extends Specification {
10593
er.errors[0].path.toList() == ["foo"]
10694
}
10795

108-
def "when @errorHandling is not added to the schema operation does not validate"() {
96+
def "when @nullOnError is not added to the schema operation does not validate"() {
10997

11098
def sdl = '''
11199
type Query {
@@ -116,20 +104,20 @@ class NoErrorPropagationTest extends Specification {
116104
def graphql = TestUtil.graphQL(sdl).build()
117105

118106
def query = '''
119-
query GetFoo @errorHandling(onError: PROPAGATE) { foo }
107+
query GetFoo @nullOnError { foo }
120108
'''
121109
when:
122110

123111
ExecutionInput ei = ExecutionInput.newExecutionInput(query).root(
124112
[foo: null]
125-
).graphQLContext([(ExperimentalApi.ENABLE_CUSTOM_ERROR_HANDLING): true])
113+
).graphQLContext([(ExperimentalApi.ENABLE_NULL_ON_ERROR): true])
126114
.build()
127115

128116
def er = graphql.execute(ei)
129117

130118
then:
131119
er.data == null
132-
er.errors[0].message.equals("Validation error (UnknownDirective) : Unknown directive 'errorHandling'")
120+
er.errors[0].message.equals("Validation error (UnknownDirective) : Unknown directive 'nullOnError'")
133121
}
134122

135123
}

0 commit comments

Comments
 (0)