| 1 |
from copy import deepcopy |
| 2 |
|
| 3 |
from pytest import raises |
| 4 |
|
| 5 |
from graphql.error import GraphQLSyntaxError |
| 6 |
from graphql.language import ( |
| 7 |
FieldNode, |
| 8 |
NameNode, |
| 9 |
parse, |
| 10 |
parse_schema_coordinate, |
| 11 |
print_ast, |
| 12 |
) |
| 13 |
|
| 14 |
from ..fixtures import kitchen_sink_query # noqa: F401 |
| 15 |
from ..utils import dedent |
| 16 |
|
| 17 |
|
| 18 |
def describe_printer_query_document(): |
| 19 |
def prints_minimal_ast(): |
| 20 |
ast = FieldNode(name=NameNode(value="foo")) |
| 21 |
assert print_ast(ast) == "foo" |
| 22 |
|
| 23 |
def produces_helpful_error_messages(): |
| 24 |
bad_ast = {"random": "Data"} |
| 25 |
with raises(TypeError) as exc_info: |
| 26 |
# noinspection PyTypeChecker |
| 27 |
print_ast(bad_ast) # type: ignore |
| 28 |
assert str(exc_info.value) == "Not an AST Node: {'random': 'Data'}." |
| 29 |
corrupt_ast = FieldNode(name="random data") |
| 30 |
with raises(TypeError) as exc_info: |
| 31 |
print_ast(corrupt_ast) |
| 32 |
assert str(exc_info.value) == "Invalid AST Node: 'random data'." |
| 33 |
|
| 34 |
def correctly_prints_query_operation_without_name(): |
| 35 |
query_ast_shorthanded = parse("query { id, name }") |
| 36 |
assert print_ast(query_ast_shorthanded) == "{\n id\n name\n}" |
| 37 |
|
| 38 |
def correctly_prints_mutation_operation_without_name(): |
| 39 |
mutation_ast = parse("mutation { id, name }") |
| 40 |
assert print_ast(mutation_ast) == "mutation {\n id\n name\n}" |
| 41 |
|
| 42 |
def correctly_prints_query_operation_with_artifacts(): |
| 43 |
query_ast_with_artifacts = parse( |
| 44 |
"query ($foo: TestType) @testDirective { id, name }" |
| 45 |
) |
| 46 |
assert print_ast(query_ast_with_artifacts) == dedent(""" |
| 47 |
query ($foo: TestType) @testDirective { |
| 48 |
id |
| 49 |
name |
| 50 |
} |
| 51 |
""") |
| 52 |
|
| 53 |
def correctly_prints_mutation_operation_with_artifacts(): |
| 54 |
mutation_ast_with_artifacts = parse( |
| 55 |
"mutation ($foo: TestType) @testDirective { id, name }" |
| 56 |
) |
| 57 |
assert print_ast(mutation_ast_with_artifacts) == dedent(""" |
| 58 |
mutation ($foo: TestType) @testDirective { |
| 59 |
id |
| 60 |
name |
| 61 |
} |
| 62 |
""") |
| 63 |
|
| 64 |
def prints_query_with_variable_directives(): |
| 65 |
query_ast_with_variable_directive = parse( |
| 66 |
"query ($foo: TestType = {a: 123}" " @testDirective(if: true) @test) { id }" |
| 67 |
) |
| 68 |
assert print_ast(query_ast_with_variable_directive) == dedent(""" |
| 69 |
query ($foo: TestType = {a: 123} @testDirective(if: true) @test) { |
| 70 |
id |
| 71 |
} |
| 72 |
""") |
| 73 |
|
| 74 |
def keeps_arguments_on_one_line_if_line_has_80_chars_or_less(): |
| 75 |
printed = print_ast(parse("{trip(wheelchair:false arriveBy:false){dateTime}}")) |
| 76 |
|
| 77 |
assert printed == dedent(""" |
| 78 |
{ |
| 79 |
trip(wheelchair: false, arriveBy: false) { |
| 80 |
dateTime |
| 81 |
} |
| 82 |
} |
| 83 |
""") |
| 84 |
|
| 85 |
def puts_arguments_on_multiple_lines_if_line_has_more_than_80_chars(): |
| 86 |
printed = print_ast( |
| 87 |
parse( |
| 88 |
"{trip(wheelchair:false arriveBy:false includePlannedCancellations:true" |
| 89 |
" transitDistanceReluctance:2000){dateTime}}" |
| 90 |
) |
| 91 |
) |
| 92 |
|
| 93 |
assert printed == dedent(""" |
| 94 |
{ |
| 95 |
trip( |
| 96 |
wheelchair: false |
| 97 |
arriveBy: false |
| 98 |
includePlannedCancellations: true |
| 99 |
transitDistanceReluctance: 2000 |
| 100 |
) { |
| 101 |
dateTime |
| 102 |
} |
| 103 |
} |
| 104 |
""") |
| 105 |
|
| 106 |
def legacy_prints_fragment_with_variable_directives(): |
| 107 |
query_ast_with_variable_directive = parse( |
| 108 |
"fragment Foo($foo: TestType @test) on TestType @testDirective { id }", |
| 109 |
allow_legacy_fragment_variables=True, |
| 110 |
) |
| 111 |
assert print_ast(query_ast_with_variable_directive) == dedent(""" |
| 112 |
fragment Foo($foo: TestType @test) on TestType @testDirective { |
| 113 |
id |
| 114 |
} |
| 115 |
""") |
| 116 |
|
| 117 |
def experimental_prints_directives_on_directives(): |
| 118 |
query_ast_with_variable_directive = parse( |
| 119 |
""" |
| 120 |
directive @foo @bar on FIELD_DEFINITION |
| 121 |
extend directive @foo @baz |
| 122 |
""", |
| 123 |
experimental_directives_on_directive_definitions=True, |
| 124 |
) |
| 125 |
assert print_ast(query_ast_with_variable_directive) == dedent(""" |
| 126 |
directive @foo @bar on FIELD_DEFINITION |
| 127 |
|
| 128 |
extend directive @foo @baz |
| 129 |
""") |
| 130 |
|
| 131 |
def legacy_correctly_prints_fragment_defined_variables(): |
| 132 |
source = """ |
| 133 |
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType { |
| 134 |
id |
| 135 |
} |
| 136 |
""" |
| 137 |
fragment_with_variable = parse(source, allow_legacy_fragment_variables=True) |
| 138 |
assert print_ast(fragment_with_variable) == dedent(source) |
| 139 |
|
| 140 |
def prints_fragment(): |
| 141 |
printed = print_ast(parse('"Fragment description" fragment Foo on Bar { baz }')) |
| 142 |
assert printed == dedent(""" |
| 143 |
"Fragment description" |
| 144 |
fragment Foo on Bar { |
| 145 |
baz |
| 146 |
} |
| 147 |
""") |
| 148 |
|
| 149 |
def prints_schema_coordinates(): |
| 150 |
assert print_ast(parse_schema_coordinate("Name")) == "Name" |
| 151 |
assert print_ast(parse_schema_coordinate("Name.field")) == "Name.field" |
| 152 |
assert ( |
| 153 |
print_ast(parse_schema_coordinate("Name.field(arg:)")) == "Name.field(arg:)" |
| 154 |
) |
| 155 |
assert print_ast(parse_schema_coordinate("@name")) == "@name" |
| 156 |
assert print_ast(parse_schema_coordinate("@name(arg:)")) == "@name(arg:)" |
| 157 |
assert print_ast(parse_schema_coordinate("__Type")) == "__Type" |
| 158 |
assert ( |
| 159 |
print_ast(parse_schema_coordinate("Type.__metafield")) == "Type.__metafield" |
| 160 |
) |
| 161 |
assert ( |
| 162 |
print_ast(parse_schema_coordinate("Type.__metafield(arg:)")) |
| 163 |
== "Type.__metafield(arg:)" |
| 164 |
) |
| 165 |
|
| 166 |
def throws_syntax_error_for_ignored_tokens_in_schema_coordinates(): |
| 167 |
with raises(GraphQLSyntaxError) as exc_info: |
| 168 |
print_ast(parse_schema_coordinate("# foo\nName")) |
| 169 |
assert "Syntax Error: Invalid character: '#'" in str(exc_info.value) |
| 170 |
with raises(GraphQLSyntaxError) as exc_info: |
| 171 |
print_ast(parse_schema_coordinate("\nName")) |
| 172 |
assert "Syntax Error: Invalid character: U+000A." in str(exc_info.value) |
| 173 |
with raises(GraphQLSyntaxError) as exc_info: |
| 174 |
print_ast(parse_schema_coordinate("Name .field")) |
| 175 |
assert "Syntax Error: Invalid character: ' '" in str(exc_info.value) |
| 176 |
|
| 177 |
def prints_kitchen_sink_without_altering_ast(kitchen_sink_query): # noqa: F811 |
| 178 |
ast = parse(kitchen_sink_query, no_location=True) |
| 179 |
|
| 180 |
ast_before_print_call = deepcopy(ast) |
| 181 |
printed = print_ast(ast) |
| 182 |
printed_ast = parse(printed, no_location=True) |
| 183 |
assert printed_ast == ast |
| 184 |
assert deepcopy(ast) == ast_before_print_call |
| 185 |
|
| 186 |
assert printed == dedent(r''' |
| 187 |
"Query description" |
| 188 |
query queryName( |
| 189 |
"Very complex variable" |
| 190 |
$foo: ComplexType |
| 191 |
$site: Site = MOBILE |
| 192 |
) @onQuery { |
| 193 |
whoever123is: node(id: [123, 456]) { |
| 194 |
id |
| 195 |
... on User @onInlineFragment { |
| 196 |
field2 { |
| 197 |
id |
| 198 |
alias: field1(first: 10, after: $foo) @include(if: $foo) { |
| 199 |
id |
| 200 |
...frag @onFragmentSpread |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
... @skip(unless: $foo) { |
| 205 |
id |
| 206 |
} |
| 207 |
... { |
| 208 |
id |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
mutation likeStory @onMutation { |
| 214 |
like(story: 123) @onField { |
| 215 |
story { |
| 216 |
id @onField |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput @onVariableDefinition) @onSubscription { |
| 222 |
storyLikeSubscribe(input: $input) { |
| 223 |
story { |
| 224 |
likers { |
| 225 |
count |
| 226 |
} |
| 227 |
likeSentence { |
| 228 |
text |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
"""Fragment description""" |
| 235 |
fragment frag on Friend @onFragmentDefinition { |
| 236 |
foo( |
| 237 |
size: $size |
| 238 |
bar: $b |
| 239 |
obj: {key: "value", block: """ |
| 240 |
block string uses \""" |
| 241 |
"""} |
| 242 |
) |
| 243 |
} |
| 244 |
|
| 245 |
{ |
| 246 |
unnamed(truthy: true, falsy: false, nullish: null) |
| 247 |
query |
| 248 |
} |
| 249 |
|
| 250 |
{ |
| 251 |
__typename |
| 252 |
} |
| 253 |
''') # noqa: E501 |