Skip to content

Commit 40c82fb

Browse files
committed
SelectionSetContainer is also a Node
add tests for QueryTraversal
1 parent 32e5652 commit 40c82fb

6 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎src/main/java/graphql/language/Field.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* This might change in the future.
1717
*/
1818
@PublicApi
19-
public class Field extends AbstractNode<Field> implements Selection<Field>, SelectionSetContainer {
19+
public class Field extends AbstractNode<Field> implements Selection<Field>, SelectionSetContainer<Field> {
2020

2121
private String name;
2222
private String alias;

‎src/main/java/graphql/language/FragmentDefinition.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Provided to the DataFetcher, therefore public API
1616
*/
1717
@PublicApi
18-
public class FragmentDefinition extends AbstractNode<FragmentDefinition> implements Definition<FragmentDefinition>, SelectionSetContainer {
18+
public class FragmentDefinition extends AbstractNode<FragmentDefinition> implements Definition<FragmentDefinition>, SelectionSetContainer<FragmentDefinition> {
1919

2020
private String name;
2121
private TypeName typeCondition;

‎src/main/java/graphql/language/InlineFragment.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import static graphql.language.NodeUtil.directivesByName;
1212

13-
public class InlineFragment extends AbstractNode<InlineFragment> implements Selection<InlineFragment>, SelectionSetContainer {
13+
public class InlineFragment extends AbstractNode<InlineFragment> implements Selection<InlineFragment>, SelectionSetContainer<InlineFragment> {
1414
private TypeName typeCondition;
1515
private List<Directive> directives;
1616
private SelectionSet selectionSet;

‎src/main/java/graphql/language/OperationDefinition.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import static graphql.language.NodeUtil.directivesByName;
1212

13-
public class OperationDefinition extends AbstractNode<OperationDefinition> implements Definition<OperationDefinition>, SelectionSetContainer {
13+
public class OperationDefinition extends AbstractNode<OperationDefinition> implements Definition<OperationDefinition>, SelectionSetContainer<OperationDefinition> {
1414

1515
public enum Operation {
1616
QUERY, MUTATION, SUBSCRIPTION
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.language;
22

3-
public interface SelectionSetContainer {
3+
4+
public interface SelectionSetContainer<T extends Node> extends Node<T> {
45
SelectionSet getSelectionSet();
56
}

‎src/test/groovy/graphql/analysis/QueryTraversalTest.groovy‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package graphql.analysis
33
import graphql.TestUtil
44
import graphql.language.Document
55
import graphql.language.Field
6+
import graphql.language.FragmentDefinition
7+
import graphql.language.InlineFragment
68
import graphql.parser.Parser
79
import graphql.schema.GraphQLSchema
810
import spock.lang.Specification
@@ -46,12 +48,15 @@ class QueryTraversalTest extends Specification {
4648
queryTraversal.visitPreOrder(visitor)
4749

4850
then:
49-
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" })
51+
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" &&
52+
it.selectionSetContainer == null})
5053
then:
5154
1 * visitor.visitField({ QueryVisitorEnvironment it ->
5255
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
5356
it.parentType.name == "Foo" &&
54-
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo"
57+
it.parentEnvironment.field.name == "foo" && it.parentEnvironment.fieldDefinition.type.name == "Foo" &&
58+
it.selectionSetContainer == it.parentEnvironment.field
59+
5560
})
5661
then:
5762
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
@@ -293,11 +298,13 @@ class QueryTraversalTest extends Specification {
293298
}
294299
""")
295300
QueryTraversal queryTraversal = createQueryTraversal(query, schema)
301+
def inlineFragment = query.children[0].children[0].children[1]
302+
assert inlineFragment instanceof InlineFragment
296303
when:
297304
queryTraversal."$visitFn"(visitor)
298305
299306
then:
300-
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" })
307+
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" && it.selectionSetContainer == inlineFragment })
301308
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
302309
1 * visitor.visitField({ QueryVisitorEnvironment it ->
303310
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
@@ -381,11 +388,13 @@ class QueryTraversalTest extends Specification {
381388

382389
""")
383390
QueryTraversal queryTraversal = createQueryTraversal(query, schema)
391+
def fragmentDefinition = query.children[1]
392+
assert fragmentDefinition instanceof FragmentDefinition
384393
when:
385394
queryTraversal."$visitFn"(visitor)
386395
387396
then:
388-
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" })
397+
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "foo" && it.fieldDefinition.type.name == "Foo" && it.parentType.name == "Query" && it.selectionSetContainer == fragmentDefinition})
389398
1 * visitor.visitField({ QueryVisitorEnvironment it -> it.field.name == "bar" && it.fieldDefinition.type.name == "String" && it.parentType.name == "Query" })
390399
1 * visitor.visitField({ QueryVisitorEnvironment it ->
391400
it.field.name == "subFoo" && it.fieldDefinition.type.name == "String" &&
@@ -902,7 +911,7 @@ class QueryTraversalTest extends Specification {
902911
def query = createQuery("""
903912
{foo { subFoo {id}} }
904913
""")
905-
def root = query.getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0]
914+
def root = query.children[0].children[0].children[0].children[0].children[0]
906915
assert root instanceof Field
907916
((Field)root).name == "subFoo"
908917
def rootParentType = schema.getType("Foo")

0 commit comments

Comments
 (0)