forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutionResultImpl.java
More file actions
190 lines (157 loc) · 5.63 KB
/
Copy pathExecutionResultImpl.java
File metadata and controls
190 lines (157 loc) · 5.63 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package graphql;
import com.google.common.collect.ImmutableList;
import graphql.collect.ImmutableKit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static graphql.collect.ImmutableKit.map;
@Internal
public class ExecutionResultImpl implements ExecutionResult {
private final List<GraphQLError> errors;
private final Object data;
private final transient Map<Object, Object> extensions;
private final transient boolean dataPresent;
public ExecutionResultImpl(GraphQLError error) {
this(false, null, Collections.singletonList(error), null);
}
public ExecutionResultImpl(List<? extends GraphQLError> errors) {
this(false, null, errors, null);
}
public ExecutionResultImpl(Object data, List<? extends GraphQLError> errors) {
this(true, data, errors, null);
}
public ExecutionResultImpl(Object data, List<? extends GraphQLError> errors, Map<Object, Object> extensions) {
this(true, data, errors, extensions);
}
public ExecutionResultImpl(ExecutionResultImpl other) {
this(other.dataPresent, other.data, other.errors, other.extensions);
}
public <T extends Builder<T>> ExecutionResultImpl(Builder<T> builder) {
this(builder.dataPresent, builder.data, builder.errors, builder.extensions);
}
private ExecutionResultImpl(boolean dataPresent, Object data, List<? extends GraphQLError> errors, Map<Object, Object> extensions) {
this.dataPresent = dataPresent;
this.data = data;
if (errors != null) {
this.errors = ImmutableList.copyOf(errors);
} else {
this.errors = ImmutableKit.emptyList();
}
this.extensions = extensions;
}
public boolean isDataPresent() {
return dataPresent;
}
@Override
public List<GraphQLError> getErrors() {
return errors;
}
@Override
@SuppressWarnings("TypeParameterUnusedInFormals")
public <T> T getData() {
//noinspection unchecked
return (T) data;
}
@Override
public Map<Object, Object> getExtensions() {
return extensions;
}
@Override
public Map<String, Object> toSpecification() {
Map<String, Object> result = new LinkedHashMap<>();
if (errors != null && !errors.isEmpty()) {
result.put("errors", errorsToSpec(errors));
}
if (dataPresent) {
result.put("data", data);
}
if (extensions != null) {
result.put("extensions", extensions);
}
return result;
}
private Object errorsToSpec(List<GraphQLError> errors) {
return map(errors, GraphQLError::toSpecification);
}
@SuppressWarnings("unchecked")
static ExecutionResult fromSpecification(Map<String, Object> specificationMap) {
ExecutionResult.Builder<?> builder = ExecutionResult.newExecutionResult();
Object data = specificationMap.get("data");
if (data != null) {
builder.data(data);
}
List<Map<String, Object>> errors = (List<Map<String, Object>>) specificationMap.get("errors");
if (errors != null) {
builder.errors(GraphqlErrorHelper.fromSpecification(errors));
}
Map<Object, Object> extensions = (Map<Object, Object>) specificationMap.get("extensions");
if (extensions != null) {
builder.extensions(extensions);
}
return builder.build();
}
@Override
public String toString() {
return "ExecutionResultImpl{" +
"errors=" + errors +
", data=" + data +
", dataPresent=" + dataPresent +
", extensions=" + extensions +
'}';
}
public static <T extends Builder<T>> Builder<T> newExecutionResult() {
return new Builder<>();
}
public static class Builder<T extends Builder<T>> implements ExecutionResult.Builder<T> {
private boolean dataPresent;
private Object data;
private List<GraphQLError> errors = new ArrayList<>();
private Map<Object, Object> extensions;
@Override
public T from(ExecutionResult executionResult) {
dataPresent = executionResult.isDataPresent();
data = executionResult.getData();
errors = new ArrayList<>(executionResult.getErrors());
extensions = executionResult.getExtensions();
return (T) this;
}
@Override
public T data(Object data) {
dataPresent = true;
this.data = data;
return (T) this;
}
@Override
public T errors(List<GraphQLError> errors) {
this.errors = errors;
return (T) this;
}
@Override
public T addErrors(List<GraphQLError> errors) {
this.errors.addAll(errors);
return (T) this;
}
@Override
public T addError(GraphQLError error) {
this.errors.add(error);
return (T) this;
}
@Override
public T extensions(Map<Object, Object> extensions) {
this.extensions = extensions;
return (T) this;
}
@Override
public T addExtension(String key, Object value) {
this.extensions = (this.extensions == null ? new LinkedHashMap<>() : this.extensions);
this.extensions.put(key, value);
return (T) this;
}
@Override
public ExecutionResult build() {
return new ExecutionResultImpl(dataPresent, data, errors, extensions);
}
}
}