55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http://www.apache.org/licenses/LICENSE-2.0
8+ * http://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1414 */
1515package graphql .annotations ;
1616
17- import graphql .ExceptionWhileDataFetching ;
1817import graphql .ExecutionResult ;
1918import graphql .execution .ExecutionContext ;
19+ import graphql .execution .ExecutionParameters ;
2020import graphql .execution .SimpleExecutionStrategy ;
21- import graphql .language .*;
22- import graphql .schema .*;
21+ import graphql .execution .TypeInfo ;
22+ import graphql .language .Argument ;
23+ import graphql .language .Field ;
24+ import graphql .language .ObjectValue ;
25+ import graphql .language .StringValue ;
26+ import graphql .language .VariableReference ;
27+ import graphql .schema .GraphQLEnumType ;
28+ import graphql .schema .GraphQLFieldDefinition ;
29+ import graphql .schema .GraphQLObjectType ;
2330import graphql .schema .GraphQLType ;
24- import org .slf4j .Logger ;
25- import org .slf4j .LoggerFactory ;
2631
2732import java .util .HashMap ;
2833import java .util .List ;
2934import java .util .Optional ;
3035
36+ import static graphql .execution .ExecutionParameters .newParameters ;
37+ import static graphql .execution .TypeInfoWorkaround .newTypeInfo ;
38+
3139public class EnhancedExecutionStrategy extends SimpleExecutionStrategy {
3240
33- private static final Logger log = LoggerFactory .getLogger (EnhancedExecutionStrategy .class );
3441 private static final String CLIENT_MUTATION_ID = "clientMutationId" ;
3542
3643 @ Override
37- protected ExecutionResult resolveField (ExecutionContext executionContext , GraphQLObjectType parentType , Object source , List <Field > fields ) {
44+ protected ExecutionResult resolveField (ExecutionContext executionContext , ExecutionParameters parameters , List <Field > fields ) {
45+ GraphQLObjectType parentType = (GraphQLObjectType ) parameters .typeInfo ().type ();
3846 GraphQLFieldDefinition fieldDef = getFieldDef (executionContext .getGraphQLSchema (), parentType , fields .get (0 ));
3947 if (fieldDef == null ) return null ;
4048
@@ -43,7 +51,7 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, GraphQ
4351 Argument argument = field .getArguments ().get (0 );
4452
4553 Object clientMutationId ;
46- if (argument .getValue () instanceof VariableReference ) {
54+ if (argument .getValue () instanceof VariableReference ) {
4755 VariableReference ref = (VariableReference ) argument .getValue ();
4856 HashMap mutationInputVariables = (HashMap ) executionContext .getVariables ().get (ref .getName ());
4957 clientMutationId = mutationInputVariables .get (CLIENT_MUTATION_ID );
@@ -55,20 +63,45 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, GraphQ
5563 clientMutationId = clientMutationIdVal .getValue ();
5664 }
5765
58- return completeValue (executionContext , fieldDef .getType (), fields , clientMutationId );
66+ TypeInfo fieldTypeInfo = newTypeInfo (fieldDef .getType (), parameters .typeInfo ());
67+ ExecutionParameters newParameters = newParameters ()
68+ .arguments (parameters .arguments ())
69+ .fields (parameters .fields ())
70+ .typeInfo (fieldTypeInfo )
71+ .source (clientMutationId )
72+ .build ();
73+
74+
75+ return completeValue (executionContext , newParameters , fields );
5976 } else {
60- return super .resolveField (executionContext , parentType , source , fields );
77+ return super .resolveField (executionContext , parameters , fields );
6178 }
6279 }
6380
6481 @ Override
65- protected ExecutionResult completeValue (ExecutionContext executionContext , GraphQLType fieldType , List <Field > fields , Object result ) {
82+ protected ExecutionResult completeValue (ExecutionContext executionContext , ExecutionParameters parameters , List <Field > fields ) {
83+ GraphQLType fieldType = parameters .typeInfo ().type ();
84+ Object result = parameters .source ();
6685 if (result instanceof Enum && fieldType instanceof GraphQLEnumType ) {
67- return super .completeValue (executionContext , fieldType , fields , ((GraphQLEnumType ) fieldType ).getCoercing ().parseValue (((Enum ) result ).name ()));
86+ Object value = ((GraphQLEnumType ) fieldType ).getCoercing ().parseValue (((Enum ) result ).name ());
87+ return super .completeValue (executionContext , withSource (parameters , value ), fields );
6888 }
6989 if (result instanceof Optional ) {
70- return completeValue (executionContext , fieldType , fields , ((Optional ) result ).orElse (null ));
90+ Object value = ((Optional <?>) result ).orElse (null );
91+ return completeValue (executionContext , withSource (parameters , value ), fields );
7192 }
72- return super .completeValue (executionContext , fieldType , fields , result );
93+ return super .completeValue (executionContext , parameters , fields );
94+ }
95+
96+ /*
97+ Creates a new parameters with the specified object as its source
98+ */
99+ private ExecutionParameters withSource (ExecutionParameters parameters , Object source ) {
100+ return newParameters ()
101+ .arguments (parameters .arguments ())
102+ .fields (parameters .fields ())
103+ .typeInfo (parameters .typeInfo ())
104+ .source (source )
105+ .build ();
73106 }
74107}
0 commit comments