2626import java .util .concurrent .Executor ;
2727
2828import graphql .GraphQLContext ;
29+ import io .micrometer .context .ContextSnapshot ;
2930import reactor .core .publisher .Mono ;
3031
3132import org .springframework .core .CoroutinesUtils ;
@@ -110,32 +111,22 @@ protected Object doInvoke(GraphQLContext graphQLContext, Object... argValues) {
110111Object result ;
111112if (this .invokeAsync ) {
112113Callable <Object > callable = () -> method .invoke (getBean (), argValues );
113- result = adaptCallable (graphQLContext , callable );
114+ result = adaptCallable (graphQLContext , callable , method , argValues );
114115}
115116else {
116117result = method .invoke (getBean (), argValues );
117118if (this .hasCallableReturnValue && result != null ) {
118- result = adaptCallable (graphQLContext , (Callable <?>) result );
119+ result = adaptCallable (graphQLContext , (Callable <?>) result , method , argValues );
119120}
120121}
121122
122123return result ;
123124}
124125catch (IllegalArgumentException ex ) {
125- assertTargetBean (method , getBean (), argValues );
126- String text = (ex .getMessage () != null ) ? ex .getMessage () : "Illegal argument" ;
127- return Mono .error (new IllegalStateException (formatInvokeError (text , argValues ), ex ));
126+ return Mono .error (processIllegalArgumentException (argValues , ex , method ));
128127}
129128catch (InvocationTargetException ex ) {
130- // Unwrap for DataFetcherExceptionResolvers ...
131- Throwable targetException = ex .getTargetException ();
132- if (targetException instanceof Error || targetException instanceof Exception ) {
133- return Mono .error (targetException );
134- }
135- else {
136- return Mono .error (new IllegalStateException (
137- formatInvokeError ("Invocation failure" , argValues ), targetException ));
138- }
129+ return Mono .error (processInvocationTargetException (argValues , ex ));
139130}
140131catch (Throwable ex ) {
141132return Mono .error (ex );
@@ -155,16 +146,46 @@ private static Object invokeSuspendingFunction(Object bean, Method method, Objec
155146return result ;
156147}
157148
158- private CompletableFuture <?> adaptCallable (GraphQLContext graphQLContext , Callable <?> result ) {
159- return CompletableFuture .supplyAsync (() -> {
149+ @ SuppressWarnings ("DataFlowIssue" )
150+ private CompletableFuture <?> adaptCallable (
151+ GraphQLContext graphQLContext , Callable <?> result , Method method , Object [] argValues ) {
152+
153+ CompletableFuture <Object > future = new CompletableFuture <>();
154+ this .executor .execute (() -> {
160155try {
161- return ContextSnapshotFactoryHelper .captureFrom (graphQLContext ).wrap (result ).call ();
156+ ContextSnapshot snapshot = ContextSnapshotFactoryHelper .captureFrom (graphQLContext );
157+ Object value = snapshot .wrap ((Callable <?>) result ).call ();
158+ future .complete (value );
159+ }
160+ catch (IllegalArgumentException ex ) {
161+ future .completeExceptionally (processIllegalArgumentException (argValues , ex , method ));
162+ }
163+ catch (InvocationTargetException ex ) {
164+ future .completeExceptionally (processInvocationTargetException (argValues , ex ));
162165}
163166catch (Exception ex ) {
164- String msg = "Failure in Callable returned from " + getBridgedMethod ().toGenericString ();
165- throw new IllegalStateException (msg , ex );
167+ future .completeExceptionally (ex );
166168}
167- }, this .executor );
169+ });
170+ return future ;
171+ }
172+
173+ private IllegalStateException processIllegalArgumentException (
174+ Object [] argValues , IllegalArgumentException ex , Method method ) {
175+
176+ assertTargetBean (method , getBean (), argValues );
177+ String text = (ex .getMessage () != null ) ? ex .getMessage () : "Illegal argument" ;
178+ return new IllegalStateException (formatInvokeError (text , argValues ), ex );
179+ }
180+
181+ private Throwable processInvocationTargetException (Object [] argValues , InvocationTargetException ex ) {
182+ // Unwrap for DataFetcherExceptionResolvers ...
183+ Throwable targetException = ex .getTargetException ();
184+ if (targetException instanceof Error || targetException instanceof Exception ) {
185+ return targetException ;
186+ }
187+ String message = formatInvokeError ("Invocation failure" , argValues );
188+ return new IllegalStateException (message , targetException );
168189}
169190
170191/**
0 commit comments