3333import graphql .schema .GraphQLType ;
3434import graphql .schema .GraphQLUnionType ;
3535import graphql .schema .GraphQLUnmodifiedType ;
36+ import graphql .schema .InputValueWithState ;
3637
3738import java .util .ArrayList ;
3839import java .util .List ;
@@ -48,6 +49,7 @@ public class TraversalContext implements DocumentVisitor {
4849 private final List <GraphQLOutputType > outputTypeStack = new ArrayList <>();
4950 private final List <GraphQLCompositeType > parentTypeStack = new ArrayList <>();
5051 private final List <GraphQLInputType > inputTypeStack = new ArrayList <>();
52+ private final List <InputValueWithState > defaultValueStack = new ArrayList <>();
5153 private final List <GraphQLFieldDefinition > fieldDefStack = new ArrayList <>();
5254 private final List <String > nameStack = new ArrayList <>();
5355 private GraphQLDirective directive ;
@@ -156,6 +158,7 @@ private void enterImpl(Argument argument) {
156158 }
157159
158160 addInputType (argumentType != null ? argumentType .getType () : null );
161+ addDefaultValue (argumentType != null ? argumentType .getArgumentDefaultValue () : null );
159162 this .argument = argumentType ;
160163 }
161164
@@ -166,23 +169,30 @@ private void enterImpl(ArrayValue arrayValue) {
166169 inputType = (GraphQLInputType ) unwrapOne (nullableType );
167170 }
168171 addInputType (inputType );
172+ // List positions never have a default value. See graphql-js impl for inspiration
173+ addDefaultValue (null );
169174 }
170175
171176 private void enterImpl (ObjectField objectField ) {
172177 GraphQLUnmodifiedType objectType = unwrapAll (getInputType ());
173178 GraphQLInputType inputType = null ;
179+ GraphQLInputObjectField inputField = null ;
174180 if (objectType instanceof GraphQLInputObjectType ) {
175181 GraphQLInputObjectType inputObjectType = (GraphQLInputObjectType ) objectType ;
176- GraphQLInputObjectField inputField = schema .getFieldVisibility ().getFieldDefinition (inputObjectType , objectField .getName ());
177- if (inputField != null )
182+ inputField = schema .getFieldVisibility ().getFieldDefinition (inputObjectType , objectField .getName ());
183+ if (inputField != null ) {
178184 inputType = inputField .getType ();
185+ }
179186 }
180187 addInputType (inputType );
188+ addDefaultValue (inputField != null ? inputField .getInputFieldDefaultValue () : null );
181189 }
182190
183191 private GraphQLArgument find (List <GraphQLArgument > arguments , String name ) {
184192 for (GraphQLArgument argument : arguments ) {
185- if (argument .getName ().equals (name )) return argument ;
193+ if (argument .getName ().equals (name )) {
194+ return argument ;
195+ }
186196 }
187197 return null ;
188198 }
@@ -191,29 +201,32 @@ private GraphQLArgument find(List<GraphQLArgument> arguments, String name) {
191201 @ Override
192202 public void leave (Node node , List <Node > ancestors ) {
193203 if (node instanceof OperationDefinition ) {
194- outputTypeStack . remove (outputTypeStack . size () - 1 );
204+ pop (outputTypeStack );
195205 } else if (node instanceof SelectionSet ) {
196- parentTypeStack . remove (parentTypeStack . size () - 1 );
206+ pop (parentTypeStack );
197207 } else if (node instanceof Field ) {
198208 leaveName (((Field ) node ).getName ());
199- fieldDefStack . remove (fieldDefStack . size () - 1 );
200- outputTypeStack . remove (outputTypeStack . size () - 1 );
209+ pop (fieldDefStack );
210+ pop (outputTypeStack );
201211 } else if (node instanceof Directive ) {
202212 directive = null ;
203213 } else if (node instanceof InlineFragment ) {
204- outputTypeStack . remove (outputTypeStack . size () - 1 );
214+ pop (outputTypeStack );
205215 } else if (node instanceof FragmentDefinition ) {
206216 leaveName (((FragmentDefinition ) node ).getName ());
207- outputTypeStack . remove (outputTypeStack . size () - 1 );
217+ pop (outputTypeStack );
208218 } else if (node instanceof VariableDefinition ) {
209219 inputTypeStack .remove (inputTypeStack .size () - 1 );
210220 } else if (node instanceof Argument ) {
211221 argument = null ;
212- inputTypeStack .remove (inputTypeStack .size () - 1 );
222+ pop (inputTypeStack );
223+ pop (defaultValueStack );
213224 } else if (node instanceof ArrayValue ) {
214- inputTypeStack .remove (inputTypeStack .size () - 1 );
225+ pop (inputTypeStack );
226+ pop (defaultValueStack );
215227 } else if (node instanceof ObjectField ) {
216- inputTypeStack .remove (inputTypeStack .size () - 1 );
228+ pop (inputTypeStack );
229+ pop (defaultValueStack );
217230 }
218231 }
219232
@@ -250,10 +263,16 @@ private void addOutputType(GraphQLOutputType type) {
250263 }
251264
252265 private <T > T lastElement (List <T > list ) {
253- if (list .size () == 0 ) return null ;
266+ if (list .isEmpty ()) {
267+ return null ;
268+ }
254269 return list .get (list .size () - 1 );
255270 }
256271
272+ private <T > T pop (List <T > list ) {
273+ return list .remove (list .size () - 1 );
274+ }
275+
257276 /**
258277 * @return can be null if the parent is not a CompositeType
259278 */
@@ -268,11 +287,18 @@ private void addParentType(GraphQLCompositeType compositeType) {
268287 public GraphQLInputType getInputType () {
269288 return lastElement (inputTypeStack );
270289 }
290+ public InputValueWithState getDefaultValue () {
291+ return lastElement (defaultValueStack );
292+ }
271293
272294 private void addInputType (GraphQLInputType graphQLInputType ) {
273295 inputTypeStack .add (graphQLInputType );
274296 }
275297
298+ private void addDefaultValue (InputValueWithState defaultValue ) {
299+ defaultValueStack .add (defaultValue );
300+ }
301+
276302 public GraphQLFieldDefinition getFieldDef () {
277303 return lastElement (fieldDefStack );
278304 }
0 commit comments