4
4
import re
5
5
6
6
import six
7
+ from graphql import DirectiveNode
8
+ from graphql import DocumentNode
9
+ from graphql import FieldNode
10
+ from graphql import FloatValueNode
11
+ from graphql import FragmentDefinitionNode
12
+ from graphql import FragmentSpreadNode
13
+ from graphql import InlineFragmentNode
14
+ from graphql import IntValueNode
15
+ from graphql import ListValueNode
16
+ from graphql import ObjectValueNode
17
+ from graphql import OperationDefinitionNode
7
18
8
19
from graphql import print_ast
9
- from graphql .language .ast import Document , IntValue , FloatValue , StringValue , ListValue , ObjectValue , Field , \
10
- Directive , FragmentDefinition , InlineFragment , FragmentSpread , SelectionSet , OperationDefinition
20
+ from graphql import SelectionSetNode
21
+ from graphql import separate_operations
22
+ from graphql import StringValueNode
11
23
from graphql .language .visitor import Visitor , visit
12
24
13
- from graphene_tornado .apollo_tooling .seperate_operations import separate_operations
14
25
15
-
16
- def hide_literals (ast : Document ) -> Document :
26
+ def hide_literals (ast : DocumentNode ) -> DocumentNode :
17
27
"""
18
28
Replace numeric, string, list, and object literals with "empty"
19
29
values. Leaves enums alone (since there's no consistent "zero" enum). This
@@ -26,7 +36,7 @@ def hide_literals(ast: Document) -> Document:
26
36
return ast
27
37
28
38
29
- def hide_string_and_numeric_literals (ast : Document ) -> Document :
39
+ def hide_string_and_numeric_literals (ast : DocumentNode ) -> DocumentNode :
30
40
"""
31
41
In the same spirit as the similarly named `hideLiterals` function, only
32
42
hide string and numeric literals.
@@ -35,7 +45,7 @@ def hide_string_and_numeric_literals(ast: Document) -> Document:
35
45
return ast
36
46
37
47
38
- def drop_unused_definitions (ast : Document , operation_name : str ) -> Document :
48
+ def drop_unused_definitions (ast : DocumentNode , operation_name : str ) -> DocumentNode :
39
49
"""
40
50
A GraphQL query may contain multiple named operations, with the operation to
41
51
use specified separately by the client. This transformation drops unused
@@ -49,7 +59,7 @@ def drop_unused_definitions(ast: Document, operation_name: str) -> Document:
49
59
return separated
50
60
51
61
52
- def sort_ast (ast : Document ) -> Document :
62
+ def sort_ast (ast : DocumentNode ) -> DocumentNode :
53
63
"""
54
64
sortAST sorts most multi-child nodes alphabetically. Using this as part of
55
65
your signature calculation function may make it easier to tell the difference
@@ -61,7 +71,7 @@ def sort_ast(ast: Document) -> Document:
61
71
return ast
62
72
63
73
64
- def remove_aliases (ast : Document ) -> Document :
74
+ def remove_aliases (ast : DocumentNode ) -> DocumentNode :
65
75
"""
66
76
removeAliases gets rid of GraphQL aliases, a feature by which you can tell a
67
77
server to return a field's data under a different name from the field
@@ -72,7 +82,7 @@ def remove_aliases(ast: Document) -> Document:
72
82
return ast
73
83
74
84
75
- def print_with_reduced_whitespace (ast : Document ) -> str :
85
+ def print_with_reduced_whitespace (ast : DocumentNode ) -> str :
76
86
"""
77
87
Like the graphql-js print function, but deleting whitespace wherever
78
88
feasible. Specifically, all whitespace (outside of string literals) is
@@ -115,15 +125,15 @@ def __init__(self, only_string_and_numeric=False):
115
125
self ._only_string_and_numeric = only_string_and_numeric
116
126
117
127
def enter (self , node , key , parent , path , ancestors ):
118
- if isinstance (node , IntValue ):
128
+ if isinstance (node , IntValueNode ):
119
129
node .value = 0
120
- elif isinstance (node , FloatValue ):
130
+ elif isinstance (node , FloatValueNode ):
121
131
node .value = 0
122
- elif isinstance (node , StringValue ):
132
+ elif isinstance (node , StringValueNode ):
123
133
node .value = ""
124
- elif not self ._only_string_and_numeric and isinstance (node , ListValue ):
134
+ elif not self ._only_string_and_numeric and isinstance (node , ListValueNode ):
125
135
node .values = []
126
- elif not self ._only_string_and_numeric and isinstance (node , ObjectValue ):
136
+ elif not self ._only_string_and_numeric and isinstance (node , ObjectValueNode ):
127
137
node .fields = []
128
138
return node
129
139
@@ -135,7 +145,7 @@ def enter(self, node, key, parent, path, ancestors):
135
145
class _RemoveAliasesVisitor (Visitor ):
136
146
137
147
def enter (self , node , key , parent , path , ancestors ):
138
- if isinstance (node , Field ):
148
+ if isinstance (node , FieldNode ):
139
149
node .alias = None
140
150
return node
141
151
@@ -146,7 +156,7 @@ def enter(self, node, key, parent, path, ancestors):
146
156
class _HexConversionVisitor (Visitor ):
147
157
148
158
def enter (self , node , key , parent , path , ancestors ):
149
- if isinstance (node , StringValue ) and node .value is not None :
159
+ if isinstance (node , StringValueNode ) and node .value is not None :
150
160
if six .PY3 :
151
161
encoded = node .value .encode ('utf-8' ).hex ()
152
162
else :
@@ -161,26 +171,26 @@ def enter(self, node, key, parent, path, ancestors):
161
171
class _SortingVisitor (Visitor ):
162
172
163
173
def enter (self , node , key , parent , path , ancestors ):
164
- if isinstance (node , Document ):
174
+ if isinstance (node , DocumentNode ):
165
175
node .definitions = _sorted (node .definitions , lambda x : (x .__class__ .__name__ , self ._by_name (x )))
166
- elif isinstance (node , OperationDefinition ):
176
+ elif isinstance (node , OperationDefinitionNode ):
167
177
node .variable_definitions = _sorted (node .variable_definitions , self ._by_variable_name )
168
- elif isinstance (node , SelectionSet ):
178
+ elif isinstance (node , SelectionSetNode ):
169
179
node .selections = _sorted (node .selections , lambda x : (x .__class__ .__name__ , self ._by_name (x )))
170
- elif isinstance (node , Field ):
180
+ elif isinstance (node , FieldNode ):
171
181
node .arguments = _sorted (node .arguments , self ._by_name )
172
- elif isinstance (node , FragmentSpread ):
182
+ elif isinstance (node , FragmentSpreadNode ):
173
183
node .directives = _sorted (node .directives , self ._by_name )
174
- elif isinstance (node , InlineFragment ):
184
+ elif isinstance (node , InlineFragmentNode ):
175
185
node .directives = _sorted (node .directives , self ._by_type_definition )
176
- elif isinstance (node , FragmentDefinition ):
186
+ elif isinstance (node , FragmentDefinitionNode ):
177
187
node .directives = _sorted (node .directives , self ._by_name )
178
- elif isinstance (node , Directive ):
188
+ elif isinstance (node , DirectiveNode ):
179
189
node .arguments = _sorted (node .arguments , self ._by_name )
180
190
return node
181
191
182
192
def _by_name (self , node ):
183
- if isinstance (node , InlineFragment ):
193
+ if isinstance (node , InlineFragmentNode ):
184
194
return self ._by_type_definition (node )
185
195
elif node .name is not None :
186
196
return node .name .value
0 commit comments