6
6
import graphql .execution .ExecutionContext ;
7
7
import graphql .execution .ExecutionStrategyParameters ;
8
8
import graphql .execution .FieldValueInfo ;
9
- import graphql .execution .MergedField ;
10
9
import graphql .schema .DataFetcher ;
11
10
import graphql .util .LockKit ;
12
11
import org .dataloader .DataLoaderRegistry ;
@@ -27,55 +26,81 @@ private static class CallStack {
27
26
private final LockKit .ReentrantLock lock = new LockKit .ReentrantLock ();
28
27
private final LevelMap expectedFetchCountPerLevel = new LevelMap ();
29
28
private final LevelMap fetchCountPerLevel = new LevelMap ();
30
- private final LevelMap expectedStrategyCallsPerLevel = new LevelMap ();
31
- private final LevelMap happenedStrategyCallsPerLevel = new LevelMap ();
29
+
30
+ private final LevelMap expectedExecuteObjectCallsPerLevel = new LevelMap ();
31
+ private final LevelMap happenedExecuteObjectCallsPerLevel = new LevelMap ();
32
+
32
33
private final LevelMap happenedOnFieldValueCallsPerLevel = new LevelMap ();
33
34
34
35
private final Set <Integer > dispatchedLevels = new LinkedHashSet <>();
35
36
36
37
public CallStack () {
37
- expectedStrategyCallsPerLevel .set (1 , 1 );
38
+ expectedExecuteObjectCallsPerLevel .set (1 , 1 );
38
39
}
39
40
40
41
void increaseExpectedFetchCount (int level , int count ) {
41
42
expectedFetchCountPerLevel .increment (level , count );
42
43
}
43
44
45
+ void clearExpectedFetchCount () {
46
+ expectedFetchCountPerLevel .clear ();
47
+ }
48
+
44
49
void increaseFetchCount (int level ) {
45
50
fetchCountPerLevel .increment (level , 1 );
46
51
}
47
52
48
- void increaseExpectedStrategyCalls (int level , int count ) {
49
- expectedStrategyCallsPerLevel .increment (level , count );
53
+ void clearFetchCount () {
54
+ fetchCountPerLevel .clear ();
55
+ }
56
+
57
+ void increaseExpectedExecuteObjectCalls (int level , int count ) {
58
+ expectedExecuteObjectCallsPerLevel .increment (level , count );
50
59
}
51
60
52
- void increaseHappenedStrategyCalls (int level ) {
53
- happenedStrategyCallsPerLevel .increment (level , 1 );
61
+ void clearExpectedObjectCalls () {
62
+ expectedExecuteObjectCallsPerLevel .clear ();
63
+ }
64
+
65
+ void increaseHappenedExecuteObjectCalls (int level ) {
66
+ happenedExecuteObjectCallsPerLevel .increment (level , 1 );
67
+ }
68
+
69
+ void clearHappenedExecuteObjectCalls () {
70
+ happenedExecuteObjectCallsPerLevel .clear ();
54
71
}
55
72
56
73
void increaseHappenedOnFieldValueCalls (int level ) {
57
74
happenedOnFieldValueCallsPerLevel .increment (level , 1 );
58
75
}
59
76
60
- boolean allStrategyCallsHappened (int level ) {
61
- return happenedStrategyCallsPerLevel .get (level ) == expectedStrategyCallsPerLevel .get (level );
77
+ void clearHappenedOnFieldValueCalls () {
78
+ happenedOnFieldValueCallsPerLevel .clear ();
79
+ }
80
+
81
+ boolean allExecuteObjectCallsHappened (int level ) {
82
+ return happenedExecuteObjectCallsPerLevel .get (level ) == expectedExecuteObjectCallsPerLevel .get (level );
62
83
}
63
84
64
85
boolean allOnFieldCallsHappened (int level ) {
65
- return happenedOnFieldValueCallsPerLevel .get (level ) == expectedStrategyCallsPerLevel .get (level );
86
+ return happenedOnFieldValueCallsPerLevel .get (level ) == expectedExecuteObjectCallsPerLevel .get (level );
66
87
}
67
88
68
89
boolean allFetchesHappened (int level ) {
69
90
return fetchCountPerLevel .get (level ) == expectedFetchCountPerLevel .get (level );
70
91
}
71
92
93
+ void clearDispatchLevels () {
94
+ dispatchedLevels .clear ();
95
+ }
96
+
72
97
@ Override
73
98
public String toString () {
74
99
return "CallStack{" +
75
100
"expectedFetchCountPerLevel=" + expectedFetchCountPerLevel +
76
101
", fetchCountPerLevel=" + fetchCountPerLevel +
77
- ", expectedStrategyCallsPerLevel =" + expectedStrategyCallsPerLevel +
78
- ", happenedStrategyCallsPerLevel =" + happenedStrategyCallsPerLevel +
102
+ ", expectedExecuteObjectCallsPerLevel =" + expectedExecuteObjectCallsPerLevel +
103
+ ", happenedExecuteObjectCallsPerLevel =" + happenedExecuteObjectCallsPerLevel +
79
104
", happenedOnFieldValueCallsPerLevel=" + happenedOnFieldValueCallsPerLevel +
80
105
", dispatchedLevels" + dispatchedLevels +
81
106
'}' ;
@@ -105,33 +130,37 @@ public void executeDeferredOnFieldValueInfo(FieldValueInfo fieldValueInfo, Execu
105
130
@ Override
106
131
public void executionStrategy (ExecutionContext executionContext , ExecutionStrategyParameters parameters ) {
107
132
int curLevel = parameters .getExecutionStepInfo ().getPath ().getLevel () + 1 ;
108
- increaseCallCounts (curLevel , parameters );
133
+ increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (curLevel , parameters );
109
134
}
110
135
111
136
@ Override
112
- public void executionStrategyOnFieldValuesInfo (List <FieldValueInfo > fieldValueInfoList , ExecutionStrategyParameters parameters ) {
113
- int curLevel = parameters .getPath ().getLevel () + 1 ;
114
- onFieldValuesInfoDispatchIfNeeded (fieldValueInfoList , curLevel , parameters );
137
+ public void executionSerialStrategy (ExecutionContext executionContext , ExecutionStrategyParameters parameters ) {
138
+ resetCallStack ();
139
+ increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (1 , 1 );
140
+ }
141
+
142
+ @ Override
143
+ public void executionStrategyOnFieldValuesInfo (List <FieldValueInfo > fieldValueInfoList ) {
144
+ onFieldValuesInfoDispatchIfNeeded (fieldValueInfoList , 1 );
115
145
}
116
146
117
- public void executionStrategyOnFieldValuesException (Throwable t , ExecutionStrategyParameters executionStrategyParameters ) {
118
- int curLevel = executionStrategyParameters .getPath ().getLevel () + 1 ;
147
+ public void executionStrategyOnFieldValuesException (Throwable t ) {
119
148
callStack .lock .runLocked (() ->
120
- callStack .increaseHappenedOnFieldValueCalls (curLevel )
149
+ callStack .increaseHappenedOnFieldValueCalls (1 )
121
150
);
122
151
}
123
152
124
153
125
154
@ Override
126
155
public void executeObject (ExecutionContext executionContext , ExecutionStrategyParameters parameters ) {
127
156
int curLevel = parameters .getExecutionStepInfo ().getPath ().getLevel () + 1 ;
128
- increaseCallCounts (curLevel , parameters );
157
+ increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (curLevel , parameters );
129
158
}
130
159
131
160
@ Override
132
161
public void executeObjectOnFieldValuesInfo (List <FieldValueInfo > fieldValueInfoList , ExecutionStrategyParameters parameters ) {
133
162
int curLevel = parameters .getPath ().getLevel () + 1 ;
134
- onFieldValuesInfoDispatchIfNeeded (fieldValueInfoList , curLevel , parameters );
163
+ onFieldValuesInfoDispatchIfNeeded (fieldValueInfoList , curLevel );
135
164
}
136
165
137
166
@@ -143,16 +172,30 @@ public void executeObjectOnFieldValuesException(Throwable t, ExecutionStrategyPa
143
172
);
144
173
}
145
174
175
+ private void increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (int curLevel , ExecutionStrategyParameters executionStrategyParameters ) {
176
+ increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (curLevel , executionStrategyParameters .getFields ().size ());
177
+ }
146
178
147
- private void increaseCallCounts (int curLevel , ExecutionStrategyParameters executionStrategyParameters ) {
148
- int fieldCount = executionStrategyParameters .getFields ().size ();
179
+ private void increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (int curLevel , int fieldCount ) {
149
180
callStack .lock .runLocked (() -> {
181
+ callStack .increaseHappenedExecuteObjectCalls (curLevel );
150
182
callStack .increaseExpectedFetchCount (curLevel , fieldCount );
151
- callStack .increaseHappenedStrategyCalls (curLevel );
152
183
});
153
184
}
154
185
155
- private void onFieldValuesInfoDispatchIfNeeded (List <FieldValueInfo > fieldValueInfoList , int curLevel , ExecutionStrategyParameters parameters ) {
186
+ private void resetCallStack () {
187
+ callStack .lock .runLocked (() -> {
188
+ callStack .clearDispatchLevels ();
189
+ callStack .clearExpectedObjectCalls ();
190
+ callStack .clearExpectedFetchCount ();
191
+ callStack .clearFetchCount ();
192
+ callStack .clearHappenedExecuteObjectCalls ();
193
+ callStack .clearHappenedOnFieldValueCalls ();
194
+ callStack .expectedExecuteObjectCallsPerLevel .set (1 , 1 );
195
+ });
196
+ }
197
+
198
+ private void onFieldValuesInfoDispatchIfNeeded (List <FieldValueInfo > fieldValueInfoList , int curLevel ) {
156
199
boolean dispatchNeeded = callStack .lock .callLocked (() ->
157
200
handleOnFieldValuesInfo (fieldValueInfoList , curLevel )
158
201
);
@@ -166,18 +209,21 @@ private void onFieldValuesInfoDispatchIfNeeded(List<FieldValueInfo> fieldValueIn
166
209
//
167
210
private boolean handleOnFieldValuesInfo (List <FieldValueInfo > fieldValueInfos , int curLevel ) {
168
211
callStack .increaseHappenedOnFieldValueCalls (curLevel );
169
- int expectedStrategyCalls = getCountForList (fieldValueInfos );
170
- callStack .increaseExpectedStrategyCalls (curLevel + 1 , expectedStrategyCalls );
212
+ int expectedOnObjectCalls = getObjectCountForList (fieldValueInfos );
213
+ callStack .increaseExpectedExecuteObjectCalls (curLevel + 1 , expectedOnObjectCalls );
171
214
return dispatchIfNeeded (curLevel + 1 );
172
215
}
173
216
174
- private int getCountForList (List <FieldValueInfo > fieldValueInfos ) {
217
+ /**
218
+ * the amount of (non nullable) objects that will require an execute object call
219
+ */
220
+ private int getObjectCountForList (List <FieldValueInfo > fieldValueInfos ) {
175
221
int result = 0 ;
176
222
for (FieldValueInfo fieldValueInfo : fieldValueInfos ) {
177
223
if (fieldValueInfo .getCompleteValueType () == FieldValueInfo .CompleteValueType .OBJECT ) {
178
224
result += 1 ;
179
225
} else if (fieldValueInfo .getCompleteValueType () == FieldValueInfo .CompleteValueType .LIST ) {
180
- result += getCountForList (fieldValueInfo .getFieldValueInfos ());
226
+ result += getObjectCountForList (fieldValueInfo .getFieldValueInfos ());
181
227
}
182
228
}
183
229
return result ;
@@ -221,7 +267,7 @@ private boolean levelReady(int level) {
221
267
return callStack .allFetchesHappened (1 );
222
268
}
223
269
if (levelReady (level - 1 ) && callStack .allOnFieldCallsHappened (level - 1 )
224
- && callStack .allStrategyCallsHappened (level ) && callStack .allFetchesHappened (level )) {
270
+ && callStack .allExecuteObjectCallsHappened (level ) && callStack .allFetchesHappened (level )) {
225
271
226
272
return true ;
227
273
}
0 commit comments