-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRxExecutionStrategy.java
More file actions
123 lines (103 loc) · 5 KB
/
RxExecutionStrategy.java
File metadata and controls
123 lines (103 loc) · 5 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
package graphql.execution;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.tuple.Pair;
import graphql.ExecutionResult;
import graphql.language.Field;
import graphql.schema.GraphQLList;
import rx.Observable;
public class RxExecutionStrategy extends ExecutionStrategy {
@Override
public ExecutionResult execute(ExecutionContext executionContext, ExecutionParameters parameters)
throws NonNullableFieldWasNullException
{
List<Observable<Pair<String, ?>>> observables = new ArrayList<>();
Map<String, List<Field>> fields = parameters.fields();
for (String fieldName : fields.keySet()) {
final List<Field> fieldList = fields.get(fieldName);
ExecutionResult executionResult = resolveField(executionContext, parameters, fieldList);
if (executionResult instanceof RxExecutionResult) {
RxExecutionResult rxResult = (RxExecutionResult)executionResult;
Observable<?> unwrapped = rxResult.getDataObservable().flatMap(potentialResult -> {
if (potentialResult instanceof RxExecutionResult) {
return ((RxExecutionResult) potentialResult).getDataObservable();
}
if (potentialResult instanceof ExecutionResult) {
return Observable.just(((ExecutionResult) potentialResult).getData());
}
return Observable.just(potentialResult);
});
observables.add(Observable.zip(Observable.just(fieldName), unwrapped, Pair::of));
} else {
observables.add(Observable.just(Pair.of(fieldName, executionResult != null ? executionResult.getData() : null)));
}
}
Observable<Map<String, Object>> result =
Observable.merge(observables)
.toMap(Pair::getLeft, Pair::getRight);
return new RxExecutionResult(result, Observable.just(executionContext.getErrors()));
}
@Override
protected ExecutionResult completeValue(ExecutionContext executionContext, ExecutionParameters parameters,
List<Field> fields)
{
Object result = parameters.source();
if (result instanceof Observable) {
return new RxExecutionResult(((Observable<?>) result).map(r -> {
ExecutionParameters newParameters = ExecutionParameters.newParameters()
.typeInfo(parameters.typeInfo())
.fields(parameters.fields())
.source(r).build();
return super.completeValue(executionContext, newParameters, fields);
}), null);
}
return super.completeValue(executionContext, parameters, fields);
}
@Override
protected ExecutionResult completeValueForList(ExecutionContext executionContext, ExecutionParameters parameters,
List<Field> fields, Iterable<Object> result)
{
TypeInfo typeInfo = parameters.typeInfo();
GraphQLList fieldType = typeInfo.castType(GraphQLList.class);
AtomicInteger idx = new AtomicInteger(0);
Observable<?> resultObservable =
Observable.from(
StreamSupport.stream(result.spliterator(), false)
.map(i -> new ListTuple(idx.getAndIncrement(), i))
.toArray(ListTuple[]::new)
)
.flatMap(tuple -> {
TypeInfo newType = typeInfo.asType(fieldType.getWrappedType());
ExecutionParameters newParameters = ExecutionParameters.newParameters()
.typeInfo(newType)
.fields(parameters.fields())
.source(tuple.result).build();
ExecutionResult executionResult = completeValue(executionContext, newParameters, fields);
if (executionResult instanceof RxExecutionResult) {
return Observable.zip(Observable.just(tuple.index), ((RxExecutionResult)executionResult).getDataObservable(), ListTuple::new);
}
return Observable.just(new ListTuple(tuple.index, executionResult.getData()));
})
.toList()
.map(listTuples -> {
return listTuples.stream()
.sorted(Comparator.comparingInt(x -> x.index))
.map(x -> x.result)
.collect(Collectors.toList());
});
return new RxExecutionResult(resultObservable, null);
}
private class ListTuple {
public int index;
public Object result;
public ListTuple(int index, Object result) {
this.index = index;
this.result = result;
}
}
}