| 1 |
from contextlib import contextmanager |
| 2 |
from typing import cast |
| 3 |
|
| 4 |
from graphql import graphql_sync |
| 5 |
from graphql.pyutils import ( |
| 6 |
Description, |
| 7 |
is_description, |
| 8 |
register_description, |
| 9 |
unregister_description, |
| 10 |
) |
| 11 |
from graphql.type import ( |
| 12 |
GraphQLArgument, |
| 13 |
GraphQLDirective, |
| 14 |
GraphQLEnumValue, |
| 15 |
GraphQLField, |
| 16 |
GraphQLInputField, |
| 17 |
GraphQLNamedType, |
| 18 |
GraphQLObjectType, |
| 19 |
GraphQLSchema, |
| 20 |
GraphQLString, |
| 21 |
) |
| 22 |
from graphql.utilities import get_introspection_query, print_schema |
| 23 |
from pytest import raises |
| 24 |
|
| 25 |
from ..utils import dedent |
| 26 |
|
| 27 |
|
| 28 |
class LazyString: |
| 29 |
def __init__(self, text: object) -> None: |
| 30 |
self.text = text |
| 31 |
|
| 32 |
def __str__(self) -> str: |
| 33 |
return str(self.text) |
| 34 |
|
| 35 |
|
| 36 |
lazy_string = cast(str, LazyString("Why am I so lazy?")) |
| 37 |
|
| 38 |
|
| 39 |
@contextmanager |
| 40 |
def registered(base: type): |
| 41 |
register_description(base) |
| 42 |
try: |
| 43 |
yield None |
| 44 |
finally: |
| 45 |
unregister_description(base) |
| 46 |
|
| 47 |
|
| 48 |
def describe_description(): |
| 49 |
def by_default_strings_are_accepted(): |
| 50 |
is_description("") |
| 51 |
is_description("text") |
| 52 |
|
| 53 |
def by_default_non_strings_are_not_accepted(): |
| 54 |
assert not is_description(None) |
| 55 |
assert not is_description(b"bytes") |
| 56 |
assert not is_description(0) |
| 57 |
assert not is_description(42) |
| 58 |
assert not is_description(("tuple",)) |
| 59 |
assert not is_description(["list"]) |
| 60 |
|
| 61 |
def after_registration_lazy_strings_are_accepted(): |
| 62 |
with registered(LazyString): |
| 63 |
assert is_description("not lazy") |
| 64 |
assert is_description(lazy_string) |
| 65 |
assert not is_description(42) |
| 66 |
|
| 67 |
def can_register_and_unregister(): |
| 68 |
try: |
| 69 |
assert Description.bases is str |
| 70 |
register_description(str) |
| 71 |
assert Description.bases is str |
| 72 |
register_description(int) |
| 73 |
assert Description.bases == (str, int) |
| 74 |
register_description(int) |
| 75 |
assert Description.bases == (str, int) |
| 76 |
register_description(float) |
| 77 |
assert Description.bases == (str, int, float) |
| 78 |
unregister_description(int) |
| 79 |
assert Description.bases == (str, float) |
| 80 |
unregister_description(float) |
| 81 |
assert Description.bases is str |
| 82 |
unregister_description(str) |
| 83 |
assert Description.bases is object |
| 84 |
register_description(str) |
| 85 |
assert Description.bases is str |
| 86 |
register_description(object) |
| 87 |
assert Description.bases is object |
| 88 |
Description.bases = (str,) |
| 89 |
unregister_description(str) |
| 90 |
assert Description.bases is object |
| 91 |
unregister_description(str) |
| 92 |
assert Description.bases is object |
| 93 |
finally: |
| 94 |
Description.bases = str |
| 95 |
|
| 96 |
def can_only_register_types(): |
| 97 |
with raises(TypeError, match="Only types can be registered\\."): |
| 98 |
# noinspection PyTypeChecker |
| 99 |
register_description("foo") # type: ignore |
| 100 |
|
| 101 |
def can_only_unregister_types(): |
| 102 |
with raises(TypeError, match="Only types can be unregistered\\."): |
| 103 |
# noinspection PyTypeChecker |
| 104 |
unregister_description("foo") # type: ignore |
| 105 |
|
| 106 |
def describe_graphql_types(): |
| 107 |
def graphql_named_type(): |
| 108 |
named_type = GraphQLNamedType(name="Foo", description="not lazy") |
| 109 |
assert named_type.name == "Foo" |
| 110 |
assert named_type.description == "not lazy" |
| 111 |
with raises(TypeError, match="Expected name to be a string\\."): |
| 112 |
GraphQLNamedType(name=lazy_string) |
| 113 |
with raises(TypeError, match="The description must be a string\\."): |
| 114 |
GraphQLNamedType(name="Foo", description=lazy_string) |
| 115 |
with registered(LazyString): |
| 116 |
named_type = GraphQLNamedType(name="Foo", description=lazy_string) |
| 117 |
assert named_type.description is lazy_string |
| 118 |
assert str(named_type.description).endswith("lazy?") |
| 119 |
with raises(TypeError, match="Expected name to be a string\\."): |
| 120 |
GraphQLNamedType(name=lazy_string) |
| 121 |
|
| 122 |
def graphql_field(): |
| 123 |
field = GraphQLField(GraphQLString, description="not lazy") |
| 124 |
assert field.description == "not lazy" |
| 125 |
field = GraphQLField(GraphQLString, deprecation_reason="not lazy") |
| 126 |
assert field.deprecation_reason == "not lazy" |
| 127 |
with raises(TypeError, match="The description must be a string\\."): |
| 128 |
GraphQLField(GraphQLString, description=lazy_string) |
| 129 |
with raises(TypeError, match="The deprecation reason must be a string\\."): |
| 130 |
GraphQLField(GraphQLString, deprecation_reason=lazy_string) |
| 131 |
with registered(LazyString): |
| 132 |
field = GraphQLField( |
| 133 |
GraphQLString, |
| 134 |
description=lazy_string, |
| 135 |
deprecation_reason=lazy_string, |
| 136 |
) |
| 137 |
assert field.description is lazy_string |
| 138 |
assert str(field.description).endswith("lazy?") |
| 139 |
assert field.deprecation_reason is lazy_string |
| 140 |
assert str(field.deprecation_reason).endswith("lazy?") |
| 141 |
|
| 142 |
def graphql_argument(): |
| 143 |
arg = GraphQLArgument(GraphQLString, description="not lazy") |
| 144 |
assert arg.description == "not lazy" |
| 145 |
with raises(TypeError, match="Argument description must be a string\\."): |
| 146 |
GraphQLArgument(GraphQLString, description=lazy_string) |
| 147 |
with registered(LazyString): |
| 148 |
arg = GraphQLArgument(GraphQLString, description=lazy_string) |
| 149 |
assert arg.description is lazy_string |
| 150 |
assert str(arg.description).endswith("lazy?") |
| 151 |
|
| 152 |
def graphql_enum_value(): |
| 153 |
value = GraphQLEnumValue(description="not lazy") |
| 154 |
assert value.description == "not lazy" |
| 155 |
value = GraphQLEnumValue(deprecation_reason="not lazy") |
| 156 |
assert value.deprecation_reason == "not lazy" |
| 157 |
with raises( |
| 158 |
TypeError, match="The description of the enum value must be a string\\." |
| 159 |
): |
| 160 |
GraphQLEnumValue(description=lazy_string) |
| 161 |
with raises( |
| 162 |
TypeError, |
| 163 |
match="The deprecation reason for the enum value must be a string\\.", |
| 164 |
): |
| 165 |
GraphQLEnumValue(deprecation_reason=lazy_string) |
| 166 |
with registered(LazyString): |
| 167 |
value = GraphQLEnumValue( |
| 168 |
description=lazy_string, deprecation_reason=lazy_string |
| 169 |
) |
| 170 |
assert value.description is lazy_string |
| 171 |
assert str(value.description).endswith("lazy?") |
| 172 |
assert value.deprecation_reason is lazy_string |
| 173 |
assert str(value.deprecation_reason).endswith("lazy?") |
| 174 |
|
| 175 |
def graphql_input_field(): |
| 176 |
field = GraphQLInputField(GraphQLString, description="not lazy") |
| 177 |
assert field.description == "not lazy" |
| 178 |
with raises(TypeError, match="Input field description must be a string\\."): |
| 179 |
GraphQLInputField(GraphQLString, description=lazy_string) |
| 180 |
with registered(LazyString): |
| 181 |
field = GraphQLInputField(GraphQLString, description=lazy_string) |
| 182 |
assert field.description is lazy_string |
| 183 |
assert str(field.description).endswith("lazy?") |
| 184 |
|
| 185 |
def graphql_directive(): |
| 186 |
directive = GraphQLDirective("Foo", [], description="not lazy") |
| 187 |
assert directive.name == "Foo" |
| 188 |
assert directive.description == "not lazy" |
| 189 |
with raises(TypeError, match="Expected name to be a string\\."): |
| 190 |
GraphQLDirective(lazy_string, []) |
| 191 |
with raises(TypeError, match="Foo description must be a string\\."): |
| 192 |
GraphQLDirective("Foo", [], description=lazy_string) |
| 193 |
with registered(LazyString): |
| 194 |
directive = GraphQLDirective("Foo", [], description=lazy_string) |
| 195 |
assert directive.description is lazy_string |
| 196 |
assert str(directive.description).endswith("lazy?") |
| 197 |
with raises(TypeError, match="Expected name to be a string\\."): |
| 198 |
GraphQLDirective(lazy_string, []) |
| 199 |
|
| 200 |
def handles_introspection(): |
| 201 |
class Lazy: |
| 202 |
def __init__(self, text: str): |
| 203 |
self.text = text |
| 204 |
self.evaluated = False |
| 205 |
|
| 206 |
def __str__(self) -> str: |
| 207 |
self.evaluated = True |
| 208 |
return self.text |
| 209 |
|
| 210 |
description = Lazy("a lazy description") |
| 211 |
deprecation_reason = Lazy("a lazy reason") |
| 212 |
|
| 213 |
with registered(Lazy): |
| 214 |
field = GraphQLField( |
| 215 |
GraphQLString, |
| 216 |
description=cast(str, description), |
| 217 |
deprecation_reason=cast(str, deprecation_reason), |
| 218 |
) |
| 219 |
|
| 220 |
schema = GraphQLSchema(GraphQLObjectType("Query", {"lazyField": field})) |
| 221 |
|
| 222 |
query = get_introspection_query(descriptions=True) |
| 223 |
assert not description.evaluated |
| 224 |
assert not deprecation_reason.evaluated |
| 225 |
result = graphql_sync(schema, query) |
| 226 |
assert description.evaluated |
| 227 |
assert deprecation_reason.evaluated |
| 228 |
assert result.data |
| 229 |
introspected_query = result.data["__schema"]["types"][0] |
| 230 |
assert introspected_query["name"] == "Query" |
| 231 |
introspected_field = introspected_query["fields"][0] |
| 232 |
assert introspected_field["name"] == "lazyField" |
| 233 |
assert introspected_field["description"] == "a lazy description" |
| 234 |
assert introspected_field["deprecationReason"] == "a lazy reason" |
| 235 |
|
| 236 |
def handles_printing(): |
| 237 |
class Lazy: |
| 238 |
def __init__(self, text: str): |
| 239 |
self.text = text |
| 240 |
self.evaluated = False |
| 241 |
|
| 242 |
def __str__(self) -> str: |
| 243 |
self.evaluated = True |
| 244 |
return self.text |
| 245 |
|
| 246 |
description = Lazy("a lazy description") |
| 247 |
deprecation_reason = Lazy("a lazy reason") |
| 248 |
|
| 249 |
with registered(Lazy): |
| 250 |
field = GraphQLField( |
| 251 |
GraphQLString, |
| 252 |
description=cast(str, description), |
| 253 |
deprecation_reason=cast(str, deprecation_reason), |
| 254 |
) |
| 255 |
|
| 256 |
schema = GraphQLSchema(GraphQLObjectType("Query", {"lazyField": field})) |
| 257 |
|
| 258 |
assert not description.evaluated |
| 259 |
assert not deprecation_reason.evaluated |
| 260 |
assert print_schema(schema) == dedent(''' |
| 261 |
type Query { |
| 262 |
"""a lazy description""" |
| 263 |
lazyField: String @deprecated(reason: "a lazy reason") |
| 264 |
} |
| 265 |
''') |
| 266 |
assert description.evaluated |
| 267 |
assert deprecation_reason.evaluated |