| 1 |
from copy import deepcopy |
| 2 |
|
| 3 |
from pytest import raises |
| 4 |
|
| 5 |
from graphql.language import ScalarTypeDefinitionNode, NameNode, print_ast, parse |
| 6 |
|
| 7 |
from ..fixtures import kitchen_sink_sdl # noqa: F401 |
| 8 |
from ..utils import dedent |
| 9 |
|
| 10 |
|
| 11 |
def describe_printer_sdl_document(): |
| 12 |
def prints_minimal_ast(): |
| 13 |
node = ScalarTypeDefinitionNode(name=NameNode(value="foo")) |
| 14 |
assert print_ast(node) == "scalar foo" |
| 15 |
|
| 16 |
def produces_helpful_error_messages(): |
| 17 |
bad_ast = {"random": "Data"} |
| 18 |
with raises(TypeError) as exc_info: |
| 19 |
# noinspection PyTypeChecker |
| 20 |
print_ast(bad_ast) # type: ignore |
| 21 |
msg = str(exc_info.value) |
| 22 |
assert msg == "Not an AST Node: {'random': 'Data'}." |
| 23 |
|
| 24 |
# noinspection PyShadowingNames |
| 25 |
def prints_kitchen_sink_without_altering_ast(kitchen_sink_sdl): # noqa: F811 |
| 26 |
ast = parse(kitchen_sink_sdl, no_location=True) |
| 27 |
|
| 28 |
ast_before_print_call = deepcopy(ast) |
| 29 |
printed = print_ast(ast) |
| 30 |
printed_ast = parse(printed, no_location=True) |
| 31 |
|
| 32 |
assert printed_ast == ast |
| 33 |
assert deepcopy(ast) == ast_before_print_call |
| 34 |
|
| 35 |
assert printed == dedent(''' |
| 36 |
"""This is a description of the schema as a whole.""" |
| 37 |
schema { |
| 38 |
query: QueryType |
| 39 |
mutation: MutationType |
| 40 |
} |
| 41 |
|
| 42 |
""" |
| 43 |
This is a description |
| 44 |
of the `Foo` type. |
| 45 |
""" |
| 46 |
type Foo implements Bar & Baz & Two { |
| 47 |
"Description of the `one` field." |
| 48 |
one: Type |
| 49 |
"""This is a description of the `two` field.""" |
| 50 |
two( |
| 51 |
"""This is a description of the `argument` argument.""" |
| 52 |
argument: InputType! |
| 53 |
): Type |
| 54 |
"""This is a description of the `three` field.""" |
| 55 |
three(argument: InputType, other: String): Int |
| 56 |
four(argument: String = "string"): String |
| 57 |
five(argument: [String] = ["string", "string"]): String |
| 58 |
six(argument: InputType = {key: "value"}): Type |
| 59 |
seven(argument: Int = null): Type |
| 60 |
eight(argument: OneOfInputType): Type |
| 61 |
} |
| 62 |
|
| 63 |
type AnnotatedObject @onObject(arg: "value") { |
| 64 |
annotatedField(arg: Type = "default" @onArgumentDefinition): Type @onField |
| 65 |
} |
| 66 |
|
| 67 |
type UndefinedType |
| 68 |
|
| 69 |
extend type Foo { |
| 70 |
seven(argument: [String]): Type |
| 71 |
} |
| 72 |
|
| 73 |
extend type Foo @onType |
| 74 |
|
| 75 |
interface Bar { |
| 76 |
one: Type |
| 77 |
four(argument: String = "string"): String |
| 78 |
} |
| 79 |
|
| 80 |
interface AnnotatedInterface @onInterface { |
| 81 |
annotatedField(arg: Type @onArgumentDefinition): Type @onField |
| 82 |
} |
| 83 |
|
| 84 |
interface UndefinedInterface |
| 85 |
|
| 86 |
extend interface Bar implements Two { |
| 87 |
two(argument: InputType!): Type |
| 88 |
} |
| 89 |
|
| 90 |
extend interface Bar @onInterface |
| 91 |
|
| 92 |
interface Baz implements Bar & Two { |
| 93 |
one: Type |
| 94 |
two(argument: InputType!): Type |
| 95 |
four(argument: String = "string"): String |
| 96 |
} |
| 97 |
|
| 98 |
union Feed = Story | Article | Advert |
| 99 |
|
| 100 |
union AnnotatedUnion @onUnion = A | B |
| 101 |
|
| 102 |
union AnnotatedUnionTwo @onUnion = A | B |
| 103 |
|
| 104 |
union UndefinedUnion |
| 105 |
|
| 106 |
extend union Feed = Photo | Video |
| 107 |
|
| 108 |
extend union Feed @onUnion |
| 109 |
|
| 110 |
scalar CustomScalar |
| 111 |
|
| 112 |
scalar AnnotatedScalar @onScalar |
| 113 |
|
| 114 |
extend scalar CustomScalar @onScalar |
| 115 |
|
| 116 |
enum Site { |
| 117 |
"""This is a description of the `DESKTOP` value""" |
| 118 |
DESKTOP |
| 119 |
"""This is a description of the `MOBILE` value""" |
| 120 |
MOBILE |
| 121 |
"This is a description of the `WEB` value" |
| 122 |
WEB |
| 123 |
} |
| 124 |
|
| 125 |
enum AnnotatedEnum @onEnum { |
| 126 |
ANNOTATED_VALUE @onEnumValue |
| 127 |
OTHER_VALUE |
| 128 |
} |
| 129 |
|
| 130 |
enum UndefinedEnum |
| 131 |
|
| 132 |
extend enum Site { |
| 133 |
VR |
| 134 |
} |
| 135 |
|
| 136 |
extend enum Site @onEnum |
| 137 |
|
| 138 |
input InputType { |
| 139 |
key: String! |
| 140 |
answer: Int = 42 |
| 141 |
} |
| 142 |
|
| 143 |
input OneOfInputType @oneOf { |
| 144 |
string: String |
| 145 |
int: Int |
| 146 |
} |
| 147 |
|
| 148 |
input AnnotatedInput @onInputObject { |
| 149 |
annotatedField: Type @onInputFieldDefinition |
| 150 |
} |
| 151 |
|
| 152 |
input UndefinedInput |
| 153 |
|
| 154 |
extend input InputType { |
| 155 |
other: Float = 1.23e4 @onInputFieldDefinition |
| 156 |
} |
| 157 |
|
| 158 |
extend input InputType @onInputObject |
| 159 |
|
| 160 |
"""This is a description of the `@skip` directive""" |
| 161 |
directive @skip( |
| 162 |
"""This is a description of the `if` argument""" |
| 163 |
if: Boolean! @onArgumentDefinition |
| 164 |
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT |
| 165 |
|
| 166 |
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT |
| 167 |
|
| 168 |
directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT |
| 169 |
|
| 170 |
directive @myRepeatableDir(name: String!) repeatable on OBJECT | INTERFACE |
| 171 |
|
| 172 |
extend schema @onSchema |
| 173 |
|
| 174 |
extend schema @onSchema { |
| 175 |
subscription: SubscriptionType |
| 176 |
} |
| 177 |
''') # noqa: E501 |