3232import graphql .schema .GraphQLType ;
3333import graphql .schema .GraphQLUnionType ;
3434import graphql .schema .GraphQLUnmodifiedType ;
35+ import graphql .schema .InputValueWithState ;
3536
3637import java .util .ArrayList ;
3738import java .util .List ;
@@ -47,6 +48,7 @@ public class TraversalContext implements DocumentVisitor {
4748 private final List <GraphQLOutputType > outputTypeStack = new ArrayList <>();
4849 private final List <GraphQLCompositeType > parentTypeStack = new ArrayList <>();
4950 private final List <GraphQLInputType > inputTypeStack = new ArrayList <>();
51+ private final List <InputValueWithState > defaultValueStack = new ArrayList <>();
5052 private final List <GraphQLFieldDefinition > fieldDefStack = new ArrayList <>();
5153 private final List <String > nameStack = new ArrayList <>();
5254 private GraphQLDirective directive ;
@@ -155,6 +157,7 @@ private void enterImpl(Argument argument) {
155157 }
156158
157159 addInputType (argumentType != null ? argumentType .getType () : null );
160+ addDefaultValue (argumentType != null ? argumentType .getArgumentDefaultValue () : null );
158161 this .argument = argumentType ;
159162 }
160163
@@ -165,23 +168,30 @@ private void enterImpl(ArrayValue arrayValue) {
165168 inputType = (GraphQLInputType ) unwrapOne (nullableType );
166169 }
167170 addInputType (inputType );
171+ // List positions never have a default value. See graphql-js impl for inspiration
172+ addDefaultValue (null );
168173 }
169174
170175 private void enterImpl (ObjectField objectField ) {
171176 GraphQLUnmodifiedType objectType = unwrapAll (getInputType ());
172177 GraphQLInputType inputType = null ;
178+ GraphQLInputObjectField inputField = null ;
173179 if (objectType instanceof GraphQLInputObjectType ) {
174180 GraphQLInputObjectType inputObjectType = (GraphQLInputObjectType ) objectType ;
175- GraphQLInputObjectField inputField = schema .getFieldVisibility ().getFieldDefinition (inputObjectType , objectField .getName ());
176- if (inputField != null )
181+ inputField = schema .getFieldVisibility ().getFieldDefinition (inputObjectType , objectField .getName ());
182+ if (inputField != null ) {
177183 inputType = inputField .getType ();
184+ }
178185 }
179186 addInputType (inputType );
187+ addDefaultValue (inputField != null ? inputField .getInputFieldDefaultValue () : null );
180188 }
181189
182190 private GraphQLArgument find (List <GraphQLArgument > arguments , String name ) {
183191 for (GraphQLArgument argument : arguments ) {
184- if (argument .getName ().equals (name )) return argument ;
192+ if (argument .getName ().equals (name )) {
193+ return argument ;
194+ }
185195 }
186196 return null ;
187197 }
@@ -190,29 +200,32 @@ private GraphQLArgument find(List<GraphQLArgument> arguments, String name) {
190200 @ Override
191201 public void leave (Node node , List <Node > ancestors ) {
192202 if (node instanceof OperationDefinition ) {
193- outputTypeStack . remove (outputTypeStack . size () - 1 );
203+ pop (outputTypeStack );
194204 } else if (node instanceof SelectionSet ) {
195- parentTypeStack . remove (parentTypeStack . size () - 1 );
205+ pop (parentTypeStack );
196206 } else if (node instanceof Field ) {
197207 leaveName (((Field ) node ).getName ());
198- fieldDefStack . remove (fieldDefStack . size () - 1 );
199- outputTypeStack . remove (outputTypeStack . size () - 1 );
208+ pop (fieldDefStack );
209+ pop (outputTypeStack );
200210 } else if (node instanceof Directive ) {
201211 directive = null ;
202212 } else if (node instanceof InlineFragment ) {
203- outputTypeStack . remove (outputTypeStack . size () - 1 );
213+ pop (outputTypeStack );
204214 } else if (node instanceof FragmentDefinition ) {
205215 leaveName (((FragmentDefinition ) node ).getName ());
206- outputTypeStack . remove (outputTypeStack . size () - 1 );
216+ pop (outputTypeStack );
207217 } else if (node instanceof VariableDefinition ) {
208218 inputTypeStack .remove (inputTypeStack .size () - 1 );
209219 } else if (node instanceof Argument ) {
210220 argument = null ;
211- inputTypeStack .remove (inputTypeStack .size () - 1 );
221+ pop (inputTypeStack );
222+ pop (defaultValueStack );
212223 } else if (node instanceof ArrayValue ) {
213- inputTypeStack .remove (inputTypeStack .size () - 1 );
224+ pop (inputTypeStack );
225+ pop (defaultValueStack );
214226 } else if (node instanceof ObjectField ) {
215- inputTypeStack .remove (inputTypeStack .size () - 1 );
227+ pop (inputTypeStack );
228+ pop (defaultValueStack );
216229 }
217230 }
218231
@@ -249,10 +262,16 @@ private void addOutputType(GraphQLOutputType type) {
249262 }
250263
251264 private <T > T lastElement (List <T > list ) {
252- if (list .size () == 0 ) return null ;
265+ if (list .isEmpty ()) {
266+ return null ;
267+ }
253268 return list .get (list .size () - 1 );
254269 }
255270
271+ private <T > T pop (List <T > list ) {
272+ return list .remove (list .size () - 1 );
273+ }
274+
256275 /**
257276 * @return can be null if the parent is not a CompositeType
258277 */
@@ -267,11 +286,18 @@ private void addParentType(GraphQLCompositeType compositeType) {
267286 public GraphQLInputType getInputType () {
268287 return lastElement (inputTypeStack );
269288 }
289+ public InputValueWithState getDefaultValue () {
290+ return lastElement (defaultValueStack );
291+ }
270292
271293 private void addInputType (GraphQLInputType graphQLInputType ) {
272294 inputTypeStack .add (graphQLInputType );
273295 }
274296
297+ private void addDefaultValue (InputValueWithState defaultValue ) {
298+ defaultValueStack .add (defaultValue );
299+ }
300+
275301 public GraphQLFieldDefinition getFieldDef () {
276302 return lastElement (fieldDefStack );
277303 }
0 commit comments