Skip to content

Commit 83295d8

Browse files
author
Sam Cooke
committed
Tests to demonstrate problem
Related GraphQL-js commit: graphql/graphql-js@4256230
1 parent 480bf4c commit 83295d8

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

graphql/core/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.core.language.ast import Field, Name, SelectionSet
1+
from graphql.core.language.ast import Field, Name, SelectionSet, Document, OperationDefinition
22
from graphql.core.language.parser import parse
33
from graphql.core.language.printer import print_ast
44
from graphql.core.language.visitor import (
@@ -10,6 +10,67 @@
1010
from ...validation.tests.utils import test_schema
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

0 commit comments

Comments
 (0)