Skip to content

Commit 44e434f

Browse files
committed
Merge branch 'mixcloud-upstream-release' into releases/0.5.0
# Conflicts: # graphql/language/tests/test_visitor.py
2 parents cb4d508 + 47c0530 commit 44e434f

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

graphql/language/tests/test_visitor.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from graphql.language.ast import Field, Name, SelectionSet
1+
from graphql.language.ast import Field, Name, SelectionSet, Document, OperationDefinition
22
from graphql.language.parser import parse
33
from graphql.language.printer import print_ast
44
from graphql.language.visitor import (BREAK, REMOVE, ParallelVisitor,
@@ -10,6 +10,67 @@
1010
from .fixtures import KITCHEN_SINK
1111

1212

13+
def test_allows_editing_a_node_both_on_enter_and_on_leave():
14+
ast = parse('{ a, b, c { a, b, c } }', no_location=True)
15+
16+
class TestVisitor(Visitor):
17+
def enter(self, node, *args):
18+
if isinstance(node, OperationDefinition):
19+
selection_set = node.selection_set
20+
self.selections = None
21+
if selection_set:
22+
self.selections = selection_set.selections
23+
new_selection_set = SelectionSet(
24+
selections=[])
25+
return OperationDefinition(
26+
name=node.name,
27+
variable_definitions=node.variable_definitions,
28+
directives=node.directives,
29+
loc=node.loc,
30+
operation=node.operation,
31+
selection_set=new_selection_set)
32+
33+
def leave(self, node, *args):
34+
if isinstance(node, OperationDefinition):
35+
new_selection_set = None
36+
if self.selections:
37+
new_selection_set = SelectionSet(
38+
selections=self.selections)
39+
return OperationDefinition(
40+
name=node.name,
41+
variable_definitions=node.variable_definitions,
42+
directives=node.directives,
43+
loc=node.loc,
44+
operation=node.operation,
45+
selection_set=new_selection_set)
46+
47+
edited_ast = visit(ast, TestVisitor())
48+
assert ast == parse('{ a, b, c { a, b, c } }', no_location=True)
49+
assert edited_ast == ast
50+
51+
52+
def test_allows_editing_the_root_node_on_enter_and_on_leave():
53+
ast = parse('{ a, b, c { a, b, c } }', no_location=True)
54+
55+
definitions = ast.definitions
56+
57+
class TestVisitor(Visitor):
58+
def enter(self, node, *args):
59+
if isinstance(node, Document):
60+
return Document(
61+
loc=node.loc,
62+
definitions=[])
63+
64+
def leave(self, node, *args):
65+
if isinstance(node, Document):
66+
return Document(
67+
loc=node.loc,
68+
definitions=definitions)
69+
70+
edited_ast = visit(ast, TestVisitor())
71+
assert edited_ast == ast
72+
73+
1374
def test_allows_for_editing_on_enter():
1475
ast = parse('{ a, b, c { a, b, c } }', no_location=True)
1576

graphql/language/visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def visit(root, visitor, key_map=None):
154154
break
155155

156156
if edits:
157-
new_root = edits[0][1]
157+
new_root = edits[-1][1]
158158

159159
return new_root
160160

0 commit comments

Comments
 (0)