forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutionResult.java
More file actions
152 lines (131 loc) · 4.61 KB
/
ExecutionResult.java
File metadata and controls
152 lines (131 loc) · 4.61 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package graphql;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
/**
* This simple value class represents the result of performing a graphql query.
*/
@PublicApi
@SuppressWarnings("TypeParameterUnusedInFormals")
public interface ExecutionResult {
/**
* @return the errors that occurred during execution or empty list if there is none
*/
List<GraphQLError> getErrors();
/**
* @param <T> allows type coercion
*
* @return the data in the result or null if there is none
*/
<T> T getData();
/**
* The graphql specification specifies:
*
* "If an error was encountered before execution begins, the data entry should not be present in the result.
* If an error was encountered during the execution that prevented a valid response, the data entry in the response should be null."
*
* This allows to distinguish between the cases where {@link #getData()} returns null.
*
* See : <a href="https://graphql.github.io/graphql-spec/June2018/#sec-Data">https://graphql.github.io/graphql-spec/June2018/#sec-Data</a>
*
* @return <code>true</code> if the entry "data" should be present in the result
* <code>false</code> otherwise
*/
boolean isDataPresent();
/**
* @return a map of extensions or null if there are none
*/
Map<Object, Object> getExtensions();
/**
* The graphql specification says that result of a call should be a map that follows certain rules on what items
* should be present. Certain JSON serializers may or may interpret {@link ExecutionResult} to spec, so this method
* is provided to produce a map that strictly follows the specification.
*
* See : <a href="https://spec.graphql.org/October2021/#sec-Response-Format">https://spec.graphql.org/October2021/#sec-Response-Format</a>
*
* @return a map of the result that strictly follows the spec
*/
Map<String, Object> toSpecification();
/**
* This helps you transform the current {@link ExecutionResult} object into another one by starting a builder with all
* the current values and allows you to transform it how you want.
*
* @param builderConsumer the consumer code that will be given a builder to transform
*
* @return a new {@link ExecutionResult} object based on calling build on that builder
*/
default ExecutionResult transform(Consumer<Builder<?>> builderConsumer) {
Builder<?> builder = newExecutionResult().from(this);
builderConsumer.accept(builder);
return builder.build();
}
/**
* @return a builder that allows you to build a new execution result
*/
static Builder<?> newExecutionResult() {
return ExecutionResultImpl.newExecutionResult();
}
interface Builder<B extends Builder<B>> {
/**
* Sets values into the builder based on a previous {@link ExecutionResult}
*
* @param executionResult the previous {@link ExecutionResult}
*
* @return the builder
*/
B from(ExecutionResult executionResult);
/**
* Sets new data into the builder
*
* @param data the data to use
*
* @return the builder
*/
B data(Object data);
/**
* Sets error list as the errors for this builder
*
* @param errors the errors to use
*
* @return the builder
*/
B errors(List<GraphQLError> errors);
/**
* Adds the error list to any existing the errors for this builder
*
* @param errors the errors to add
*
* @return the builder
*/
B addErrors(List<GraphQLError> errors);
/**
* Adds the error to any existing the errors for this builder
*
* @param error the error to add
*
* @return the builder
*/
B addError(GraphQLError error);
/**
* Sets the extension map for this builder
*
* @param extensions the extensions to use
*
* @return the builder
*/
B extensions(Map<Object, Object> extensions);
/**
* Adds a new entry into the extensions map for this builder
*
* @param key the key of the extension entry
* @param value the value of the extension entry
*
* @return the builder
*/
B addExtension(String key, Object value);
/**
* @return a newly built {@link ExecutionResult}
*/
ExecutionResult build();
}
}