11package graphql .validation ;
22
33
4- import java .util .ArrayList ;
5- import java .util .List ;
6-
74import graphql .Assert ;
85import graphql .Internal ;
96import graphql .execution .TypeFromAST ;
3936import graphql .schema .GraphQLUnmodifiedType ;
4037import graphql .schema .SchemaUtil ;
4138
39+ import java .util .ArrayList ;
40+ import java .util .List ;
41+
4242import static graphql .introspection .Introspection .SchemaMetaFieldDef ;
4343import static graphql .introspection .Introspection .TypeMetaFieldDef ;
4444import static graphql .introspection .Introspection .TypeNameMetaFieldDef ;
@@ -50,6 +50,7 @@ public class TraversalContext implements DocumentVisitor {
5050 final List <GraphQLCompositeType > parentTypeStack = new ArrayList <>();
5151 final List <GraphQLInputType > inputTypeStack = new ArrayList <>();
5252 final List <GraphQLFieldDefinition > fieldDefStack = new ArrayList <>();
53+ final List <String > nameStack = new ArrayList <>();
5354 GraphQLDirective directive ;
5455 GraphQLArgument argument ;
5556
@@ -96,6 +97,7 @@ private void enterImpl(SelectionSet selectionSet) {
9697 }
9798
9899 private void enterImpl (Field field ) {
100+ enterName (field .getName ());
99101 GraphQLCompositeType parentType = getParentType ();
100102 GraphQLFieldDefinition fieldDefinition = null ;
101103 if (parentType != null ) {
@@ -133,6 +135,7 @@ private void enterImpl(InlineFragment inlineFragment) {
133135 }
134136
135137 private void enterImpl (FragmentDefinition fragmentDefinition ) {
138+ enterName (fragmentDefinition .getName ());
136139 GraphQLType type = schema .getType (fragmentDefinition .getTypeCondition ().getName ());
137140 addOutputType ((GraphQLOutputType ) type );
138141 }
@@ -190,13 +193,15 @@ public void leave(Node node, List<Node> ancestors) {
190193 } else if (node instanceof SelectionSet ) {
191194 parentTypeStack .remove (parentTypeStack .size () - 1 );
192195 } else if (node instanceof Field ) {
196+ leaveName (((Field ) node ).getName ());
193197 fieldDefStack .remove (fieldDefStack .size () - 1 );
194198 outputTypeStack .remove (outputTypeStack .size () - 1 );
195199 } else if (node instanceof Directive ) {
196200 directive = null ;
197201 } else if (node instanceof InlineFragment ) {
198202 outputTypeStack .remove (outputTypeStack .size () - 1 );
199203 } else if (node instanceof FragmentDefinition ) {
204+ leaveName (((FragmentDefinition ) node ).getName ());
200205 outputTypeStack .remove (outputTypeStack .size () - 1 );
201206 } else if (node instanceof VariableDefinition ) {
202207 inputTypeStack .remove (inputTypeStack .size () - 1 );
@@ -210,6 +215,21 @@ public void leave(Node node, List<Node> ancestors) {
210215 }
211216 }
212217
218+ private void enterName (String name ) {
219+ if (!isEmpty (name )) {
220+ nameStack .add (name );
221+ }
222+ }
223+
224+ private void leaveName (String name ) {
225+ if (!isEmpty (name )) {
226+ nameStack .remove (nameStack .size () - 1 );
227+ }
228+ }
229+
230+ private boolean isEmpty (String name ) {
231+ return name == null || name .isEmpty ();
232+ }
213233
214234 private GraphQLNullableType getNullableType (GraphQLType type ) {
215235 return (GraphQLNullableType ) (type instanceof GraphQLNonNull ? ((GraphQLNonNull ) type ).getWrappedType () : type );
@@ -256,6 +276,13 @@ public GraphQLFieldDefinition getFieldDef() {
256276 return lastElement (fieldDefStack );
257277 }
258278
279+ public List <Object > getPath () {
280+ if (nameStack .isEmpty ()) {
281+ return null ;
282+ }
283+ return new ArrayList <>(nameStack );
284+ }
285+
259286 private void addFieldDef (GraphQLFieldDefinition fieldDefinition ) {
260287 fieldDefStack .add (fieldDefinition );
261288 }
0 commit comments