66import graphql .execution .ExecutionContext ;
77import graphql .execution .ExecutionStrategyParameters ;
88import graphql .execution .FieldValueInfo ;
9- import graphql .execution .MergedField ;
109import graphql .schema .DataFetcher ;
1110import graphql .util .LockKit ;
1211import org .dataloader .DataLoaderRegistry ;
@@ -27,55 +26,81 @@ private static class CallStack {
2726 private final LockKit .ReentrantLock lock = new LockKit .ReentrantLock ();
2827 private final LevelMap expectedFetchCountPerLevel = new LevelMap ();
2928 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+
3233 private final LevelMap happenedOnFieldValueCallsPerLevel = new LevelMap ();
3334
3435 private final Set <Integer > dispatchedLevels = new LinkedHashSet <>();
3536
3637 public CallStack () {
37- expectedStrategyCallsPerLevel .set (1 , 1 );
38+ expectedExecuteObjectCallsPerLevel .set (1 , 1 );
3839 }
3940
4041 void increaseExpectedFetchCount (int level , int count ) {
4142 expectedFetchCountPerLevel .increment (level , count );
4243 }
4344
45+ void clearExpectedFetchCount () {
46+ expectedFetchCountPerLevel .clear ();
47+ }
48+
4449 void increaseFetchCount (int level ) {
4550 fetchCountPerLevel .increment (level , 1 );
4651 }
4752
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 );
5059 }
5160
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 ();
5471 }
5572
5673 void increaseHappenedOnFieldValueCalls (int level ) {
5774 happenedOnFieldValueCallsPerLevel .increment (level , 1 );
5875 }
5976
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 );
6283 }
6384
6485 boolean allOnFieldCallsHappened (int level ) {
65- return happenedOnFieldValueCallsPerLevel .get (level ) == expectedStrategyCallsPerLevel .get (level );
86+ return happenedOnFieldValueCallsPerLevel .get (level ) == expectedExecuteObjectCallsPerLevel .get (level );
6687 }
6788
6889 boolean allFetchesHappened (int level ) {
6990 return fetchCountPerLevel .get (level ) == expectedFetchCountPerLevel .get (level );
7091 }
7192
93+ void clearDispatchLevels () {
94+ dispatchedLevels .clear ();
95+ }
96+
7297 @ Override
7398 public String toString () {
7499 return "CallStack{" +
75100 "expectedFetchCountPerLevel=" + expectedFetchCountPerLevel +
76101 ", fetchCountPerLevel=" + fetchCountPerLevel +
77- ", expectedStrategyCallsPerLevel =" + expectedStrategyCallsPerLevel +
78- ", happenedStrategyCallsPerLevel =" + happenedStrategyCallsPerLevel +
102+ ", expectedExecuteObjectCallsPerLevel =" + expectedExecuteObjectCallsPerLevel +
103+ ", happenedExecuteObjectCallsPerLevel =" + happenedExecuteObjectCallsPerLevel +
79104 ", happenedOnFieldValueCallsPerLevel=" + happenedOnFieldValueCallsPerLevel +
80105 ", dispatchedLevels" + dispatchedLevels +
81106 '}' ;
@@ -105,33 +130,37 @@ public void executeDeferredOnFieldValueInfo(FieldValueInfo fieldValueInfo, Execu
105130 @ Override
106131 public void executionStrategy (ExecutionContext executionContext , ExecutionStrategyParameters parameters ) {
107132 int curLevel = parameters .getExecutionStepInfo ().getPath ().getLevel () + 1 ;
108- increaseCallCounts (curLevel , parameters );
133+ increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (curLevel , parameters );
109134 }
110135
111136 @ 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 );
115145 }
116146
117- public void executionStrategyOnFieldValuesException (Throwable t , ExecutionStrategyParameters executionStrategyParameters ) {
118- int curLevel = executionStrategyParameters .getPath ().getLevel () + 1 ;
147+ public void executionStrategyOnFieldValuesException (Throwable t ) {
119148 callStack .lock .runLocked (() ->
120- callStack .increaseHappenedOnFieldValueCalls (curLevel )
149+ callStack .increaseHappenedOnFieldValueCalls (1 )
121150 );
122151 }
123152
124153
125154 @ Override
126155 public void executeObject (ExecutionContext executionContext , ExecutionStrategyParameters parameters ) {
127156 int curLevel = parameters .getExecutionStepInfo ().getPath ().getLevel () + 1 ;
128- increaseCallCounts (curLevel , parameters );
157+ increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (curLevel , parameters );
129158 }
130159
131160 @ Override
132161 public void executeObjectOnFieldValuesInfo (List <FieldValueInfo > fieldValueInfoList , ExecutionStrategyParameters parameters ) {
133162 int curLevel = parameters .getPath ().getLevel () + 1 ;
134- onFieldValuesInfoDispatchIfNeeded (fieldValueInfoList , curLevel , parameters );
163+ onFieldValuesInfoDispatchIfNeeded (fieldValueInfoList , curLevel );
135164 }
136165
137166
@@ -143,16 +172,30 @@ public void executeObjectOnFieldValuesException(Throwable t, ExecutionStrategyPa
143172 );
144173 }
145174
175+ private void increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (int curLevel , ExecutionStrategyParameters executionStrategyParameters ) {
176+ increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (curLevel , executionStrategyParameters .getFields ().size ());
177+ }
146178
147- private void increaseCallCounts (int curLevel , ExecutionStrategyParameters executionStrategyParameters ) {
148- int fieldCount = executionStrategyParameters .getFields ().size ();
179+ private void increaseHappenedExecuteObjectAndIncreaseExpectedFetchCount (int curLevel , int fieldCount ) {
149180 callStack .lock .runLocked (() -> {
181+ callStack .increaseHappenedExecuteObjectCalls (curLevel );
150182 callStack .increaseExpectedFetchCount (curLevel , fieldCount );
151- callStack .increaseHappenedStrategyCalls (curLevel );
152183 });
153184 }
154185
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 ) {
156199 boolean dispatchNeeded = callStack .lock .callLocked (() ->
157200 handleOnFieldValuesInfo (fieldValueInfoList , curLevel )
158201 );
@@ -166,18 +209,21 @@ private void onFieldValuesInfoDispatchIfNeeded(List<FieldValueInfo> fieldValueIn
166209//
167210 private boolean handleOnFieldValuesInfo (List <FieldValueInfo > fieldValueInfos , int curLevel ) {
168211 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 );
171214 return dispatchIfNeeded (curLevel + 1 );
172215 }
173216
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 ) {
175221 int result = 0 ;
176222 for (FieldValueInfo fieldValueInfo : fieldValueInfos ) {
177223 if (fieldValueInfo .getCompleteValueType () == FieldValueInfo .CompleteValueType .OBJECT ) {
178224 result += 1 ;
179225 } else if (fieldValueInfo .getCompleteValueType () == FieldValueInfo .CompleteValueType .LIST ) {
180- result += getCountForList (fieldValueInfo .getFieldValueInfos ());
226+ result += getObjectCountForList (fieldValueInfo .getFieldValueInfos ());
181227 }
182228 }
183229 return result ;
@@ -221,7 +267,7 @@ private boolean levelReady(int level) {
221267 return callStack .allFetchesHappened (1 );
222268 }
223269 if (levelReady (level - 1 ) && callStack .allOnFieldCallsHappened (level - 1 )
224- && callStack .allStrategyCallsHappened (level ) && callStack .allFetchesHappened (level )) {
270+ && callStack .allExecuteObjectCallsHappened (level ) && callStack .allFetchesHappened (level )) {
225271
226272 return true ;
227273 }
0 commit comments