| 1 |
from typing import cast |
| 2 |
|
| 3 |
from graphql import graphql_sync |
| 4 |
from graphql.language import DirectiveLocation |
| 5 |
from graphql.type import ( |
| 6 |
GraphQLArgument, |
| 7 |
GraphQLBoolean, |
| 8 |
GraphQLDirective, |
| 9 |
GraphQLEnumType, |
| 10 |
GraphQLEnumValue, |
| 11 |
GraphQLField, |
| 12 |
GraphQLFloat, |
| 13 |
GraphQLID, |
| 14 |
GraphQLInt, |
| 15 |
GraphQLObjectType, |
| 16 |
GraphQLSchema, |
| 17 |
GraphQLString, |
| 18 |
assert_enum_type, |
| 19 |
) |
| 20 |
from graphql.utilities import ( |
| 21 |
build_client_schema, |
| 22 |
build_schema, |
| 23 |
introspection_from_schema, |
| 24 |
print_schema, |
| 25 |
) |
| 26 |
from graphql.utilities.get_introspection_query import ( |
| 27 |
IntrospectionEnumType, |
| 28 |
IntrospectionInputObjectType, |
| 29 |
IntrospectionInterfaceType, |
| 30 |
IntrospectionObjectType, |
| 31 |
IntrospectionType, |
| 32 |
IntrospectionUnionType, |
| 33 |
) |
| 34 |
from pytest import raises |
| 35 |
|
| 36 |
from ..utils import dedent |
| 37 |
|
| 38 |
|
| 39 |
def cycle_introspection(sdl_string: str): |
| 40 |
"""Test that the client side introspection gives the same result. |
| 41 |
|
| 42 |
This function does a full cycle of going from a string with the contents of the SDL, |
| 43 |
build in-memory GraphQLSchema from it, produce a client-side representation of the |
| 44 |
schema by using "build_client_schema" and then return that schema printed as SDL. |
| 45 |
""" |
| 46 |
server_schema = build_schema(sdl_string) |
| 47 |
initial_introspection = introspection_from_schema(server_schema) |
| 48 |
client_schema = build_client_schema(initial_introspection) |
| 49 |
# If the client then runs the introspection query against the client-side schema, |
| 50 |
# it should get a result identical to what was returned by the server |
| 51 |
second_introspection = introspection_from_schema(client_schema) |
| 52 |
|
| 53 |
# If the client then runs the introspection query against the client-side |
| 54 |
# schema, it should get a result identical to what was returned by the server. |
| 55 |
assert initial_introspection == second_introspection |
| 56 |
return print_schema(client_schema) |
| 57 |
|
| 58 |
|
| 59 |
def describe_type_system_build_schema_from_introspection(): |
| 60 |
def builds_a_simple_schema(): |
| 61 |
sdl = dedent(''' |
| 62 |
"""Simple schema""" |
| 63 |
schema { |
| 64 |
query: Simple |
| 65 |
} |
| 66 |
|
| 67 |
"""This is a simple type""" |
| 68 |
type Simple { |
| 69 |
"""This is a string field""" |
| 70 |
string: String |
| 71 |
} |
| 72 |
''') |
| 73 |
|
| 74 |
assert cycle_introspection(sdl) == sdl |
| 75 |
|
| 76 |
def builds_a_schema_without_the_query_type(): |
| 77 |
sdl = dedent(""" |
| 78 |
type Query { |
| 79 |
foo: String |
| 80 |
} |
| 81 |
""") |
| 82 |
|
| 83 |
schema = build_schema(sdl) |
| 84 |
introspection = introspection_from_schema(schema) |
| 85 |
del introspection["__schema"]["queryType"] # type: ignore |
| 86 |
|
| 87 |
client_schema = build_client_schema(introspection) |
| 88 |
assert client_schema.query_type is None |
| 89 |
assert print_schema(client_schema) == sdl |
| 90 |
|
| 91 |
def builds_a_simple_schema_with_all_operation_types(): |
| 92 |
sdl = dedent(''' |
| 93 |
schema { |
| 94 |
query: QueryType |
| 95 |
mutation: MutationType |
| 96 |
subscription: SubscriptionType |
| 97 |
} |
| 98 |
|
| 99 |
"""This is a simple mutation type""" |
| 100 |
type MutationType { |
| 101 |
"""Set the string field""" |
| 102 |
string: String |
| 103 |
} |
| 104 |
|
| 105 |
"""This is a simple query type""" |
| 106 |
type QueryType { |
| 107 |
"""This is a string field""" |
| 108 |
string: String |
| 109 |
} |
| 110 |
|
| 111 |
"""This is a simple subscription type""" |
| 112 |
type SubscriptionType { |
| 113 |
"""This is a string field""" |
| 114 |
string: String |
| 115 |
} |
| 116 |
''') |
| 117 |
|
| 118 |
assert cycle_introspection(sdl) == sdl |
| 119 |
|
| 120 |
def uses_built_in_scalars_when_possible(): |
| 121 |
sdl = dedent(""" |
| 122 |
scalar CustomScalar |
| 123 |
|
| 124 |
type Query { |
| 125 |
int: Int |
| 126 |
float: Float |
| 127 |
string: String |
| 128 |
boolean: Boolean |
| 129 |
id: ID |
| 130 |
custom: CustomScalar |
| 131 |
} |
| 132 |
""") |
| 133 |
|
| 134 |
assert cycle_introspection(sdl) == sdl |
| 135 |
|
| 136 |
schema = build_schema(sdl) |
| 137 |
introspection = introspection_from_schema(schema) |
| 138 |
client_schema = build_client_schema(introspection) |
| 139 |
|
| 140 |
# Built-ins are used |
| 141 |
assert client_schema.get_type("Int") is GraphQLInt |
| 142 |
assert client_schema.get_type("Float") is GraphQLFloat |
| 143 |
assert client_schema.get_type("String") is GraphQLString |
| 144 |
assert client_schema.get_type("Boolean") is GraphQLBoolean |
| 145 |
assert client_schema.get_type("ID") is GraphQLID |
| 146 |
|
| 147 |
# Custom are built |
| 148 |
custom_scalar = schema.get_type("CustomScalar") |
| 149 |
assert client_schema.get_type("CustomScalar") is not custom_scalar |
| 150 |
|
| 151 |
def includes_standard_types_only_if_they_are_used(): |
| 152 |
schema = build_schema(""" |
| 153 |
type Query { |
| 154 |
foo: String |
| 155 |
} |
| 156 |
""") |
| 157 |
introspection = introspection_from_schema(schema) |
| 158 |
client_schema = build_client_schema(introspection) |
| 159 |
|
| 160 |
assert client_schema.get_type("Int") is None |
| 161 |
assert client_schema.get_type("Float") is None |
| 162 |
assert client_schema.get_type("ID") is None |
| 163 |
|
| 164 |
def builds_a_schema_with_a_recursive_type_reference(): |
| 165 |
sdl = dedent(""" |
| 166 |
schema { |
| 167 |
query: Recur |
| 168 |
} |
| 169 |
|
| 170 |
type Recur { |
| 171 |
recur: Recur |
| 172 |
} |
| 173 |
""") |
| 174 |
|
| 175 |
assert cycle_introspection(sdl) == sdl |
| 176 |
|
| 177 |
def builds_a_schema_with_a_circular_type_reference(): |
| 178 |
sdl = dedent(""" |
| 179 |
type Dog { |
| 180 |
bestFriend: Human |
| 181 |
} |
| 182 |
|
| 183 |
type Human { |
| 184 |
bestFriend: Dog |
| 185 |
} |
| 186 |
|
| 187 |
type Query { |
| 188 |
dog: Dog |
| 189 |
human: Human |
| 190 |
} |
| 191 |
""") |
| 192 |
|
| 193 |
assert cycle_introspection(sdl) == sdl |
| 194 |
|
| 195 |
def builds_a_schema_with_an_interface(): |
| 196 |
sdl = dedent(''' |
| 197 |
type Dog implements Friendly { |
| 198 |
bestFriend: Friendly |
| 199 |
} |
| 200 |
|
| 201 |
interface Friendly { |
| 202 |
"""The best friend of this friendly thing""" |
| 203 |
bestFriend: Friendly |
| 204 |
} |
| 205 |
|
| 206 |
type Human implements Friendly { |
| 207 |
bestFriend: Friendly |
| 208 |
} |
| 209 |
|
| 210 |
type Query { |
| 211 |
friendly: Friendly |
| 212 |
} |
| 213 |
''') |
| 214 |
|
| 215 |
assert cycle_introspection(sdl) == sdl |
| 216 |
|
| 217 |
def builds_a_schema_with_an_interface_hierarchy(): |
| 218 |
sdl = dedent(''' |
| 219 |
type Dog implements Friendly & Named { |
| 220 |
bestFriend: Friendly |
| 221 |
name: String |
| 222 |
} |
| 223 |
|
| 224 |
interface Friendly implements Named { |
| 225 |
"""The best friend of this friendly thing""" |
| 226 |
bestFriend: Friendly |
| 227 |
name: String |
| 228 |
} |
| 229 |
|
| 230 |
type Human implements Friendly & Named { |
| 231 |
bestFriend: Friendly |
| 232 |
name: String |
| 233 |
} |
| 234 |
|
| 235 |
interface Named { |
| 236 |
name: String |
| 237 |
} |
| 238 |
|
| 239 |
type Query { |
| 240 |
friendly: Friendly |
| 241 |
} |
| 242 |
''') |
| 243 |
|
| 244 |
assert cycle_introspection(sdl) == sdl |
| 245 |
|
| 246 |
def builds_a_schema_with_an_implicit_interface(): |
| 247 |
sdl = dedent(''' |
| 248 |
type Dog implements Friendly { |
| 249 |
bestFriend: Friendly |
| 250 |
} |
| 251 |
|
| 252 |
interface Friendly { |
| 253 |
"""The best friend of this friendly thing""" |
| 254 |
bestFriend: Friendly |
| 255 |
} |
| 256 |
|
| 257 |
type Query { |
| 258 |
dog: Dog |
| 259 |
} |
| 260 |
''') |
| 261 |
|
| 262 |
assert cycle_introspection(sdl) == sdl |
| 263 |
|
| 264 |
def builds_a_schema_with_a_union(): |
| 265 |
sdl = dedent(""" |
| 266 |
type Dog { |
| 267 |
bestFriend: Friendly |
| 268 |
} |
| 269 |
|
| 270 |
union Friendly = Dog | Human |
| 271 |
|
| 272 |
type Human { |
| 273 |
bestFriend: Friendly |
| 274 |
} |
| 275 |
|
| 276 |
type Query { |
| 277 |
friendly: Friendly |
| 278 |
} |
| 279 |
""") |
| 280 |
|
| 281 |
assert cycle_introspection(sdl) == sdl |
| 282 |
|
| 283 |
def builds_a_schema_with_complex_field_values(): |
| 284 |
sdl = dedent(""" |
| 285 |
type Query { |
| 286 |
string: String |
| 287 |
listOfString: [String] |
| 288 |
nonNullString: String! |
| 289 |
nonNullListOfString: [String]! |
| 290 |
nonNullListOfNonNullString: [String!]! |
| 291 |
} |
| 292 |
""") |
| 293 |
|
| 294 |
assert cycle_introspection(sdl) == sdl |
| 295 |
|
| 296 |
def builds_a_schema_with_field_arguments(): |
| 297 |
sdl = dedent(''' |
| 298 |
type Query { |
| 299 |
"""A field with a single arg""" |
| 300 |
one( |
| 301 |
"""This is an int arg""" |
| 302 |
intArg: Int |
| 303 |
): String |
| 304 |
|
| 305 |
"""A field with a two args""" |
| 306 |
two( |
| 307 |
"""This is an list of int arg""" |
| 308 |
listArg: [Int] |
| 309 |
|
| 310 |
"""This is a required arg""" |
| 311 |
requiredArg: Boolean! |
| 312 |
): String |
| 313 |
} |
| 314 |
''') |
| 315 |
|
| 316 |
assert cycle_introspection(sdl) == sdl |
| 317 |
|
| 318 |
def builds_a_schema_with_default_value_on_custom_scalar_field(): |
| 319 |
sdl = dedent(""" |
| 320 |
scalar CustomScalar |
| 321 |
|
| 322 |
type Query { |
| 323 |
testField(testArg: CustomScalar = "default"): String |
| 324 |
} |
| 325 |
""") |
| 326 |
|
| 327 |
assert cycle_introspection(sdl) == sdl |
| 328 |
|
| 329 |
def builds_a_schema_with_an_enum(): |
| 330 |
food_enum = GraphQLEnumType( |
| 331 |
"Food", |
| 332 |
{ |
| 333 |
"VEGETABLES": GraphQLEnumValue( |
| 334 |
1, description="Foods that are vegetables." |
| 335 |
), |
| 336 |
"FRUITS": GraphQLEnumValue(2), |
| 337 |
"OILS": GraphQLEnumValue(3, deprecation_reason="Too fatty."), |
| 338 |
}, |
| 339 |
description="Varieties of food stuffs", |
| 340 |
) |
| 341 |
|
| 342 |
schema = GraphQLSchema( |
| 343 |
GraphQLObjectType( |
| 344 |
"EnumFields", |
| 345 |
{ |
| 346 |
"food": GraphQLField( |
| 347 |
food_enum, |
| 348 |
args={ |
| 349 |
"kind": GraphQLArgument( |
| 350 |
food_enum, description="what kind of food?" |
| 351 |
) |
| 352 |
}, |
| 353 |
description="Repeats the arg you give it", |
| 354 |
) |
| 355 |
}, |
| 356 |
) |
| 357 |
) |
| 358 |
|
| 359 |
introspection = introspection_from_schema(schema) |
| 360 |
client_schema = build_client_schema(introspection) |
| 361 |
|
| 362 |
second_introspection = introspection_from_schema(client_schema) |
| 363 |
assert second_introspection == introspection |
| 364 |
|
| 365 |
# It's also an Enum type on the client. |
| 366 |
client_food_enum = assert_enum_type(client_schema.get_type("Food")) |
| 367 |
|
| 368 |
# Client types do not get server-only values, so the values mirror the names, |
| 369 |
# rather than using the integers defined in the "server" schema. |
| 370 |
values = { |
| 371 |
name: value.to_kwargs() for name, value in client_food_enum.values.items() |
| 372 |
} |
| 373 |
assert values == { |
| 374 |
"VEGETABLES": { |
| 375 |
"value": "VEGETABLES", |
| 376 |
"description": "Foods that are vegetables.", |
| 377 |
"deprecation_reason": None, |
| 378 |
"extensions": {}, |
| 379 |
"ast_node": None, |
| 380 |
}, |
| 381 |
"FRUITS": { |
| 382 |
"value": "FRUITS", |
| 383 |
"description": None, |
| 384 |
"deprecation_reason": None, |
| 385 |
"extensions": {}, |
| 386 |
"ast_node": None, |
| 387 |
}, |
| 388 |
"OILS": { |
| 389 |
"value": "OILS", |
| 390 |
"description": None, |
| 391 |
"deprecation_reason": "Too fatty.", |
| 392 |
"extensions": {}, |
| 393 |
"ast_node": None, |
| 394 |
}, |
| 395 |
} |
| 396 |
|
| 397 |
def builds_a_schema_with_an_input_object(): |
| 398 |
sdl = dedent(''' |
| 399 |
"""An input address""" |
| 400 |
input Address { |
| 401 |
"""What street is this address?""" |
| 402 |
street: String! |
| 403 |
|
| 404 |
"""The city the address is within?""" |
| 405 |
city: String! |
| 406 |
|
| 407 |
"""The country (blank will assume USA).""" |
| 408 |
country: String = "USA" |
| 409 |
} |
| 410 |
|
| 411 |
type Query { |
| 412 |
"""Get a geocode from an address""" |
| 413 |
geocode( |
| 414 |
"""The address to lookup""" |
| 415 |
address: Address |
| 416 |
): String |
| 417 |
} |
| 418 |
''') |
| 419 |
|
| 420 |
assert cycle_introspection(sdl) == sdl |
| 421 |
|
| 422 |
def builds_a_schema_with_field_arguments_with_default_values(): |
| 423 |
sdl = dedent(""" |
| 424 |
input Geo { |
| 425 |
lat: Float |
| 426 |
lon: Float |
| 427 |
} |
| 428 |
|
| 429 |
type Query { |
| 430 |
defaultInt(intArg: Int = 30): String |
| 431 |
defaultList(listArg: [Int] = [1, 2, 3]): String |
| 432 |
defaultObject(objArg: Geo = {lat: 37.485, lon: -122.148}): String |
| 433 |
defaultNull(intArg: Int = null): String |
| 434 |
noDefault(intArg: Int): String |
| 435 |
} |
| 436 |
""") |
| 437 |
|
| 438 |
assert cycle_introspection(sdl) == sdl |
| 439 |
|
| 440 |
def builds_a_schema_with_custom_directives(): |
| 441 |
sdl = dedent(''' |
| 442 |
"""This is a custom directive""" |
| 443 |
directive @customDirective repeatable on FIELD |
| 444 |
|
| 445 |
type Query { |
| 446 |
string: String |
| 447 |
} |
| 448 |
''') |
| 449 |
|
| 450 |
assert cycle_introspection(sdl) == sdl |
| 451 |
|
| 452 |
def builds_a_schema_with_deprecated_directives(): |
| 453 |
schema = GraphQLSchema( |
| 454 |
query=GraphQLObjectType( |
| 455 |
name="Query", |
| 456 |
fields={"string": GraphQLField(GraphQLString)}, |
| 457 |
), |
| 458 |
directives=[ |
| 459 |
GraphQLDirective( |
| 460 |
name="someDirective", |
| 461 |
locations=[DirectiveLocation.QUERY], |
| 462 |
deprecation_reason="Use another directive", |
| 463 |
), |
| 464 |
], |
| 465 |
) |
| 466 |
introspection = introspection_from_schema(schema) |
| 467 |
|
| 468 |
client_schema = build_client_schema(introspection) |
| 469 |
|
| 470 |
directive = client_schema.get_directive("someDirective") |
| 471 |
assert directive is not None |
| 472 |
assert directive.name == "someDirective" |
| 473 |
assert directive.deprecation_reason == "Use another directive" |
| 474 |
|
| 475 |
def builds_a_schema_without_directives(): |
| 476 |
sdl = dedent(""" |
| 477 |
type Query { |
| 478 |
foo: String |
| 479 |
} |
| 480 |
""") |
| 481 |
|
| 482 |
schema = build_schema(sdl) |
| 483 |
introspection = introspection_from_schema(schema) |
| 484 |
del introspection["__schema"]["directives"] # type: ignore |
| 485 |
|
| 486 |
client_schema = build_client_schema(introspection) |
| 487 |
|
| 488 |
assert schema.directives |
| 489 |
assert client_schema.directives == () |
| 490 |
assert print_schema(client_schema) == sdl |
| 491 |
|
| 492 |
def builds_a_schema_aware_of_deprecation(): |
| 493 |
sdl = dedent(''' |
| 494 |
directive @someDirective( |
| 495 |
"""This is a shiny new argument""" |
| 496 |
shinyArg: SomeInputObject |
| 497 |
|
| 498 |
"""This was our design mistake :(""" |
| 499 |
oldArg: String @deprecated(reason: "Use shinyArg") |
| 500 |
) on QUERY |
| 501 |
|
| 502 |
enum Color { |
| 503 |
"""So rosy""" |
| 504 |
RED |
| 505 |
|
| 506 |
"""So grassy""" |
| 507 |
GREEN |
| 508 |
|
| 509 |
"""So calming""" |
| 510 |
BLUE |
| 511 |
|
| 512 |
"""So sickening""" |
| 513 |
MAUVE @deprecated(reason: "No longer in fashion") |
| 514 |
} |
| 515 |
|
| 516 |
input SomeInputObject { |
| 517 |
"""Nothing special about it, just deprecated for some unknown reason""" |
| 518 |
oldField: String @deprecated(reason: "Don't use it, use newField instead!") |
| 519 |
|
| 520 |
"""Same field but with a new name""" |
| 521 |
newField: String |
| 522 |
} |
| 523 |
|
| 524 |
type Query { |
| 525 |
"""This is a shiny string field""" |
| 526 |
shinyString: String |
| 527 |
|
| 528 |
"""This is a deprecated string field""" |
| 529 |
deprecatedString: String @deprecated(reason: "Use shinyString") |
| 530 |
|
| 531 |
"""Color of a week""" |
| 532 |
color: Color |
| 533 |
|
| 534 |
"""Some random field""" |
| 535 |
someField( |
| 536 |
"""This is a shiny new argument""" |
| 537 |
shinyArg: SomeInputObject |
| 538 |
|
| 539 |
"""This was our design mistake :(""" |
| 540 |
oldArg: String @deprecated(reason: "Use shinyArg") |
| 541 |
): String |
| 542 |
} |
| 543 |
''') # noqa: E501 |
| 544 |
|
| 545 |
assert cycle_introspection(sdl) == sdl |
| 546 |
|
| 547 |
def builds_a_schema_with_empty_deprecation_reasons(): |
| 548 |
sdl = dedent(""" |
| 549 |
directive @someDirective(someArg: SomeInputObject @deprecated(reason: "")) on QUERY |
| 550 |
|
| 551 |
type Query { |
| 552 |
someField(someArg: SomeInputObject @deprecated(reason: "")): SomeEnum @deprecated(reason: "") |
| 553 |
} |
| 554 |
|
| 555 |
input SomeInputObject { |
| 556 |
someInputField: String @deprecated(reason: "") |
| 557 |
} |
| 558 |
|
| 559 |
enum SomeEnum { |
| 560 |
SOME_VALUE @deprecated(reason: "") |
| 561 |
} |
| 562 |
""") # noqa: E501 |
| 563 |
|
| 564 |
assert cycle_introspection(sdl) == sdl |
| 565 |
|
| 566 |
def builds_a_schema_with_specified_by_url(): |
| 567 |
sdl = dedent(""" |
| 568 |
scalar Foo @specifiedBy(url: "https://example.com/foo_spec") |
| 569 |
|
| 570 |
type Query { |
| 571 |
foo: Foo |
| 572 |
} |
| 573 |
""") |
| 574 |
|
| 575 |
assert cycle_introspection(sdl) == sdl |
| 576 |
|
| 577 |
def builds_a_schema_with_one_of_directive(): |
| 578 |
sdl = dedent(""" |
| 579 |
type Query { |
| 580 |
someField(someArg: SomeInputObject): String |
| 581 |
} |
| 582 |
|
| 583 |
input SomeInputObject @oneOf { |
| 584 |
someInputField1: String |
| 585 |
someInputField2: String |
| 586 |
} |
| 587 |
""") |
| 588 |
|
| 589 |
assert cycle_introspection(sdl) == sdl |
| 590 |
|
| 591 |
def can_use_client_schema_for_limited_execution(): |
| 592 |
schema = build_schema(""" |
| 593 |
scalar CustomScalar |
| 594 |
|
| 595 |
type Query { |
| 596 |
foo(custom1: CustomScalar, custom2: CustomScalar): String |
| 597 |
} |
| 598 |
""") |
| 599 |
|
| 600 |
introspection = introspection_from_schema(schema) |
| 601 |
client_schema = build_client_schema(introspection) |
| 602 |
|
| 603 |
class Data: |
| 604 |
foo = "bar" |
| 605 |
unused = "value" |
| 606 |
|
| 607 |
result = graphql_sync( |
| 608 |
client_schema, |
| 609 |
"query Limited($v: CustomScalar) { foo(custom1: 123, custom2: $v) }", |
| 610 |
root_value=Data(), |
| 611 |
variable_values={"v": "baz"}, |
| 612 |
) |
| 613 |
|
| 614 |
assert result.data == {"foo": "bar"} |
| 615 |
|
| 616 |
def can_build_invalid_schema(): |
| 617 |
schema = build_schema("type Query", assume_valid=True) |
| 618 |
|
| 619 |
introspection = introspection_from_schema(schema) |
| 620 |
client_schema = build_client_schema(introspection, assume_valid=True) |
| 621 |
|
| 622 |
assert client_schema.to_kwargs()["assume_valid"] is True |
| 623 |
|
| 624 |
def describe_throws_when_given_invalid_introspection(): |
| 625 |
dummy_schema = build_schema(""" |
| 626 |
type Query { |
| 627 |
foo(bar: String): String |
| 628 |
} |
| 629 |
|
| 630 |
interface SomeInterface { |
| 631 |
foo: String |
| 632 |
} |
| 633 |
|
| 634 |
union SomeUnion = Query |
| 635 |
|
| 636 |
enum SomeEnum { FOO } |
| 637 |
|
| 638 |
input SomeInputObject { |
| 639 |
foo: String |
| 640 |
} |
| 641 |
|
| 642 |
directive @SomeDirective on QUERY |
| 643 |
""") |
| 644 |
|
| 645 |
def throws_when_introspection_is_missing_schema_property(): |
| 646 |
with raises(TypeError) as exc_info: |
| 647 |
# noinspection PyTypeChecker |
| 648 |
build_client_schema(None) # type: ignore |
| 649 |
|
| 650 |
assert str(exc_info.value) == ( |
| 651 |
"Invalid or incomplete introspection result. Ensure that you" |
| 652 |
" are passing the 'data' attribute of an introspection response" |
| 653 |
" and no 'errors' were returned alongside: None." |
| 654 |
) |
| 655 |
|
| 656 |
with raises(TypeError) as exc_info: |
| 657 |
# noinspection PyTypeChecker |
| 658 |
build_client_schema({}) # type: ignore |
| 659 |
|
| 660 |
assert str(exc_info.value) == ( |
| 661 |
"Invalid or incomplete introspection result. Ensure that you" |
| 662 |
" are passing the 'data' attribute of an introspection response" |
| 663 |
" and no 'errors' were returned alongside: {}." |
| 664 |
) |
| 665 |
|
| 666 |
def throws_when_referenced_unknown_type(): |
| 667 |
introspection = introspection_from_schema(dummy_schema) |
| 668 |
|
| 669 |
introspection["__schema"]["types"] = [ |
| 670 |
type_ |
| 671 |
for type_ in introspection["__schema"]["types"] |
| 672 |
if type_["name"] != "Query" |
| 673 |
] |
| 674 |
|
| 675 |
with raises(TypeError) as exc_info: |
| 676 |
build_client_schema(introspection) |
| 677 |
|
| 678 |
assert str(exc_info.value) == ( |
| 679 |
"Invalid or incomplete schema, unknown type: Query." |
| 680 |
" Ensure that a full introspection query is used" |
| 681 |
" in order to build a client schema." |
| 682 |
) |
| 683 |
|
| 684 |
def throws_when_missing_definition_for_one_of_the_standard_scalars(): |
| 685 |
schema = build_schema(""" |
| 686 |
type Query { |
| 687 |
foo: Float |
| 688 |
} |
| 689 |
""") |
| 690 |
introspection = introspection_from_schema(schema) |
| 691 |
introspection["__schema"]["types"] = [ |
| 692 |
type_ |
| 693 |
for type_ in introspection["__schema"]["types"] |
| 694 |
if type_["name"] != "Float" |
| 695 |
] |
| 696 |
|
| 697 |
with raises(TypeError) as exc_info: |
| 698 |
build_client_schema(introspection) |
| 699 |
|
| 700 |
assert str(exc_info.value).endswith( |
| 701 |
"Invalid or incomplete schema, unknown type: Float." |
| 702 |
" Ensure that a full introspection query is used" |
| 703 |
" in order to build a client schema." |
| 704 |
) |
| 705 |
|
| 706 |
def throws_when_type_reference_is_missing_name(): |
| 707 |
introspection = introspection_from_schema(dummy_schema) |
| 708 |
query_type = cast(IntrospectionType, introspection["__schema"]["queryType"]) |
| 709 |
assert query_type["name"] == "Query" |
| 710 |
del query_type["name"] # type: ignore |
| 711 |
|
| 712 |
with raises(TypeError) as exc_info: |
| 713 |
build_client_schema(introspection) |
| 714 |
|
| 715 |
assert str(exc_info.value) == "Unknown type reference: {'kind': 'OBJECT'}." |
| 716 |
|
| 717 |
def throws_when_missing_kind(): |
| 718 |
introspection = introspection_from_schema(dummy_schema) |
| 719 |
query_type_introspection = next( |
| 720 |
type_ |
| 721 |
for type_ in introspection["__schema"]["types"] |
| 722 |
if type_["name"] == "Query" |
| 723 |
) |
| 724 |
assert query_type_introspection["kind"] == "OBJECT" |
| 725 |
del query_type_introspection["kind"] |
| 726 |
|
| 727 |
with raises( |
| 728 |
TypeError, |
| 729 |
match=r"^Invalid or incomplete introspection result\." |
| 730 |
" Ensure that a full introspection query is used" |
| 731 |
r" in order to build a client schema: {'name': 'Query', .*}\.$", |
| 732 |
): |
| 733 |
build_client_schema(introspection) |
| 734 |
|
| 735 |
def throws_when_missing_interfaces(): |
| 736 |
introspection = introspection_from_schema(dummy_schema) |
| 737 |
query_type_introspection = cast( |
| 738 |
IntrospectionObjectType, |
| 739 |
next( |
| 740 |
type_ |
| 741 |
for type_ in introspection["__schema"]["types"] |
| 742 |
if type_["name"] == "Query" |
| 743 |
), |
| 744 |
) |
| 745 |
|
| 746 |
assert query_type_introspection["interfaces"] == [] |
| 747 |
del query_type_introspection["interfaces"] # type: ignore |
| 748 |
|
| 749 |
with raises( |
| 750 |
TypeError, |
| 751 |
match="^Query interfaces cannot be resolved." |
| 752 |
" Introspection result missing interfaces:" |
| 753 |
r" {'kind': 'OBJECT', 'name': 'Query', .*}\.$", |
| 754 |
): |
| 755 |
build_client_schema(introspection) |
| 756 |
|
| 757 |
def legacy_support_for_interfaces_with_null_as_interfaces_field(): |
| 758 |
introspection = introspection_from_schema(dummy_schema) |
| 759 |
some_interface_introspection = cast( |
| 760 |
IntrospectionInterfaceType, |
| 761 |
next( |
| 762 |
type_ |
| 763 |
for type_ in introspection["__schema"]["types"] |
| 764 |
if type_["name"] == "SomeInterface" |
| 765 |
), |
| 766 |
) |
| 767 |
|
| 768 |
assert some_interface_introspection["interfaces"] == [] |
| 769 |
some_interface_introspection["interfaces"] = None # type: ignore |
| 770 |
|
| 771 |
client_schema = build_client_schema(introspection) |
| 772 |
assert print_schema(client_schema) == print_schema(dummy_schema) |
| 773 |
|
| 774 |
def throws_when_missing_fields(): |
| 775 |
introspection = introspection_from_schema(dummy_schema) |
| 776 |
query_type_introspection = cast( |
| 777 |
IntrospectionObjectType, |
| 778 |
next( |
| 779 |
type_ |
| 780 |
for type_ in introspection["__schema"]["types"] |
| 781 |
if type_["name"] == "Query" |
| 782 |
), |
| 783 |
) |
| 784 |
|
| 785 |
assert query_type_introspection["fields"] |
| 786 |
del query_type_introspection["fields"] # type: ignore |
| 787 |
|
| 788 |
with raises( |
| 789 |
TypeError, |
| 790 |
match="^Query fields cannot be resolved." |
| 791 |
" Introspection result missing fields:" |
| 792 |
r" {'kind': 'OBJECT', 'name': 'Query', .*}\.$", |
| 793 |
): |
| 794 |
build_client_schema(introspection) |
| 795 |
|
| 796 |
def throws_when_missing_field_args(): |
| 797 |
introspection = introspection_from_schema(dummy_schema) |
| 798 |
query_type_introspection = cast( |
| 799 |
IntrospectionObjectType, |
| 800 |
next( |
| 801 |
type_ |
| 802 |
for type_ in introspection["__schema"]["types"] |
| 803 |
if type_["name"] == "Query" |
| 804 |
), |
| 805 |
) |
| 806 |
|
| 807 |
field = query_type_introspection["fields"][0] |
| 808 |
assert field["args"] |
| 809 |
del field["args"] # type: ignore |
| 810 |
|
| 811 |
with raises( |
| 812 |
TypeError, |
| 813 |
match="^Query fields cannot be resolved." |
| 814 |
r" Introspection result missing field args: {'name': 'foo', .*}\.$", |
| 815 |
): |
| 816 |
build_client_schema(introspection) |
| 817 |
|
| 818 |
def throws_when_output_type_is_used_as_an_arg_type(): |
| 819 |
introspection = introspection_from_schema(dummy_schema) |
| 820 |
query_type_introspection = cast( |
| 821 |
IntrospectionObjectType, |
| 822 |
next( |
| 823 |
type_ |
| 824 |
for type_ in introspection["__schema"]["types"] |
| 825 |
if type_["name"] == "Query" |
| 826 |
), |
| 827 |
) |
| 828 |
|
| 829 |
arg = query_type_introspection["fields"][0]["args"][0] |
| 830 |
assert arg["type"]["name"] == "String" |
| 831 |
arg["type"]["name"] = "SomeUnion" |
| 832 |
|
| 833 |
with raises(TypeError) as exc_info: |
| 834 |
build_client_schema(introspection) |
| 835 |
|
| 836 |
assert str(exc_info.value).startswith( |
| 837 |
"Query fields cannot be resolved." |
| 838 |
" Introspection must provide input type for arguments," |
| 839 |
" but received: SomeUnion." |
| 840 |
) |
| 841 |
|
| 842 |
def throws_when_output_type_is_used_as_an_input_value_type(): |
| 843 |
introspection = introspection_from_schema(dummy_schema) |
| 844 |
input_object_type_introspection = cast( |
| 845 |
IntrospectionInputObjectType, |
| 846 |
next( |
| 847 |
type_ |
| 848 |
for type_ in introspection["__schema"]["types"] |
| 849 |
if type_["name"] == "SomeInputObject" |
| 850 |
), |
| 851 |
) |
| 852 |
|
| 853 |
input_field = input_object_type_introspection["inputFields"][0] |
| 854 |
assert input_field["type"]["name"] == "String" |
| 855 |
input_field["type"]["name"] = "SomeUnion" |
| 856 |
|
| 857 |
with raises(TypeError) as exc_info: |
| 858 |
build_client_schema(introspection) |
| 859 |
|
| 860 |
assert str(exc_info.value).startswith( |
| 861 |
"SomeInputObject fields cannot be resolved." |
| 862 |
" Introspection must provide input type for input fields," |
| 863 |
" but received: SomeUnion." |
| 864 |
) |
| 865 |
|
| 866 |
def throws_when_input_type_is_used_as_a_field_type(): |
| 867 |
introspection = introspection_from_schema(dummy_schema) |
| 868 |
query_type_introspection = cast( |
| 869 |
IntrospectionObjectType, |
| 870 |
next( |
| 871 |
type_ |
| 872 |
for type_ in introspection["__schema"]["types"] |
| 873 |
if type_["name"] == "Query" |
| 874 |
), |
| 875 |
) |
| 876 |
|
| 877 |
field = query_type_introspection["fields"][0] |
| 878 |
assert field["type"]["name"] == "String" |
| 879 |
field["type"]["name"] = "SomeInputObject" |
| 880 |
|
| 881 |
with raises(TypeError) as exc_info: |
| 882 |
build_client_schema(introspection) |
| 883 |
|
| 884 |
assert str(exc_info.value).startswith( |
| 885 |
"Query fields cannot be resolved." |
| 886 |
" Introspection must provide output type for fields," |
| 887 |
" but received: SomeInputObject." |
| 888 |
) |
| 889 |
|
| 890 |
def throws_when_missing_possible_types(): |
| 891 |
introspection = introspection_from_schema(dummy_schema) |
| 892 |
some_union_introspection = cast( |
| 893 |
IntrospectionUnionType, |
| 894 |
next( |
| 895 |
type_ |
| 896 |
for type_ in introspection["__schema"]["types"] |
| 897 |
if type_["name"] == "SomeUnion" |
| 898 |
), |
| 899 |
) |
| 900 |
|
| 901 |
assert some_union_introspection["possibleTypes"] |
| 902 |
del some_union_introspection["possibleTypes"] # type: ignore |
| 903 |
|
| 904 |
with raises( |
| 905 |
TypeError, |
| 906 |
match="^Introspection result missing possibleTypes:" |
| 907 |
r" {'kind': 'UNION', 'name': 'SomeUnion', .*}\.$", |
| 908 |
): |
| 909 |
build_client_schema(introspection) |
| 910 |
|
| 911 |
def throws_when_missing_enum_values(): |
| 912 |
introspection = introspection_from_schema(dummy_schema) |
| 913 |
some_enum_introspection = cast( |
| 914 |
IntrospectionEnumType, |
| 915 |
next( |
| 916 |
type_ |
| 917 |
for type_ in introspection["__schema"]["types"] |
| 918 |
if type_["name"] == "SomeEnum" |
| 919 |
), |
| 920 |
) |
| 921 |
|
| 922 |
assert some_enum_introspection["enumValues"] |
| 923 |
del some_enum_introspection["enumValues"] # type: ignore |
| 924 |
|
| 925 |
with raises( |
| 926 |
TypeError, |
| 927 |
match="^Introspection result missing enumValues:" |
| 928 |
r" {'kind': 'ENUM', 'name': 'SomeEnum', .*}\.$", |
| 929 |
): |
| 930 |
build_client_schema(introspection) |
| 931 |
|
| 932 |
def throws_when_missing_input_fields(): |
| 933 |
introspection = introspection_from_schema(dummy_schema) |
| 934 |
some_input_object_introspection = cast( |
| 935 |
IntrospectionInputObjectType, |
| 936 |
next( |
| 937 |
type_ |
| 938 |
for type_ in introspection["__schema"]["types"] |
| 939 |
if type_["name"] == "SomeInputObject" |
| 940 |
), |
| 941 |
) |
| 942 |
|
| 943 |
assert some_input_object_introspection["inputFields"] |
| 944 |
del some_input_object_introspection["inputFields"] # type: ignore |
| 945 |
|
| 946 |
with raises( |
| 947 |
TypeError, |
| 948 |
match="^Introspection result missing inputFields:" |
| 949 |
r" {'kind': 'INPUT_OBJECT', 'name': 'SomeInputObject', .*}\.$", |
| 950 |
): |
| 951 |
build_client_schema(introspection) |
| 952 |
|
| 953 |
def throws_when_missing_directive_locations(): |
| 954 |
introspection = introspection_from_schema(dummy_schema) |
| 955 |
some_directive_introspection = introspection["__schema"]["directives"][0] |
| 956 |
|
| 957 |
assert some_directive_introspection["name"] == "SomeDirective" |
| 958 |
assert some_directive_introspection["locations"] == ["QUERY"] |
| 959 |
del some_directive_introspection["locations"] # type: ignore |
| 960 |
|
| 961 |
with raises( |
| 962 |
TypeError, |
| 963 |
match="^Introspection result missing directive locations:" |
| 964 |
r" {'name': 'SomeDirective', .*}\.$", |
| 965 |
): |
| 966 |
build_client_schema(introspection) |
| 967 |
|
| 968 |
def throws_when_missing_directive_args(): |
| 969 |
introspection = introspection_from_schema(dummy_schema) |
| 970 |
some_directive_introspection = introspection["__schema"]["directives"][0] |
| 971 |
|
| 972 |
assert some_directive_introspection["name"] == "SomeDirective" |
| 973 |
assert some_directive_introspection["args"] == [] |
| 974 |
del some_directive_introspection["args"] # type: ignore |
| 975 |
|
| 976 |
with raises( |
| 977 |
TypeError, |
| 978 |
match="^Introspection result missing directive args:" |
| 979 |
r" {'name': 'SomeDirective', .*}\.$", |
| 980 |
): |
| 981 |
build_client_schema(introspection) |
| 982 |
|
| 983 |
def describe_very_deep_decorators_are_not_supported(): |
| 984 |
def fails_on_very_deep_lists_more_than_8_levels(): |
| 985 |
schema = build_schema(""" |
| 986 |
type Query { |
| 987 |
foo: [[[[[[[[[[String]]]]]]]]]] |
| 988 |
} |
| 989 |
""") |
| 990 |
|
| 991 |
introspection = introspection_from_schema(schema) |
| 992 |
|
| 993 |
with raises(TypeError) as exc_info: |
| 994 |
build_client_schema(introspection) |
| 995 |
|
| 996 |
assert str(exc_info.value) == ( |
| 997 |
"Query fields cannot be resolved." |
| 998 |
" Decorated type deeper than introspection query." |
| 999 |
) |
| 1000 |
|
| 1001 |
def fails_on_a_very_deep_non_null_more_than_8_levels(): |
| 1002 |
schema = build_schema(""" |
| 1003 |
type Query { |
| 1004 |
foo: [[[[[String!]!]!]!]!] |
| 1005 |
} |
| 1006 |
""") |
| 1007 |
|
| 1008 |
introspection = introspection_from_schema(schema) |
| 1009 |
|
| 1010 |
with raises(TypeError) as exc_info: |
| 1011 |
build_client_schema(introspection) |
| 1012 |
|
| 1013 |
assert str(exc_info.value) == ( |
| 1014 |
"Query fields cannot be resolved." |
| 1015 |
" Decorated type deeper than introspection query." |
| 1016 |
) |
| 1017 |
|
| 1018 |
def succeeds_on_deep_types_less_or_equal_8_levels(): |
| 1019 |
# e.g., fully non-null 4D matrix |
| 1020 |
sdl = dedent(""" |
| 1021 |
type Query { |
| 1022 |
foo: [[[[String!]!]!]!]! |
| 1023 |
} |
| 1024 |
""") |
| 1025 |
|
| 1026 |
assert cycle_introspection(sdl) == sdl |
| 1027 |
|
| 1028 |
def describe_prevents_infinite_recursion_on_invalid_introspection(): |
| 1029 |
def recursive_interfaces(): |
| 1030 |
sdl = """ |
| 1031 |
type Query { |
| 1032 |
foo: Foo |
| 1033 |
} |
| 1034 |
|
| 1035 |
type Foo { |
| 1036 |
foo: String |
| 1037 |
} |
| 1038 |
""" |
| 1039 |
schema = build_schema(sdl, assume_valid=True) |
| 1040 |
introspection = introspection_from_schema(schema) |
| 1041 |
foo_introspection = cast( |
| 1042 |
IntrospectionObjectType, |
| 1043 |
next( |
| 1044 |
type_ |
| 1045 |
for type_ in introspection["__schema"]["types"] |
| 1046 |
if type_["name"] == "Foo" |
| 1047 |
), |
| 1048 |
) |
| 1049 |
|
| 1050 |
assert foo_introspection["interfaces"] == [] |
| 1051 |
# we need to patch here since invalid interfaces cannot be built with Python |
| 1052 |
foo_introspection["interfaces"] = [ |
| 1053 |
{"kind": "OBJECT", "name": "Foo", "ofType": None} |
| 1054 |
] |
| 1055 |
|
| 1056 |
with raises(TypeError) as exc_info: |
| 1057 |
build_client_schema(introspection) |
| 1058 |
assert str(exc_info.value) == ( |
| 1059 |
"Foo interfaces cannot be resolved." |
| 1060 |
" Expected Foo to be a GraphQL Interface type." |
| 1061 |
) |
| 1062 |
|
| 1063 |
def recursive_union(): |
| 1064 |
sdl = """ |
| 1065 |
type Query { |
| 1066 |
foo: Foo |
| 1067 |
} |
| 1068 |
|
| 1069 |
union Foo |
| 1070 |
""" |
| 1071 |
schema = build_schema(sdl, assume_valid=True) |
| 1072 |
introspection = introspection_from_schema(schema) |
| 1073 |
foo_introspection = next( |
| 1074 |
type_ |
| 1075 |
for type_ in introspection["__schema"]["types"] |
| 1076 |
if type_["name"] == "Foo" |
| 1077 |
) |
| 1078 |
|
| 1079 |
assert foo_introspection["kind"] == "UNION" |
| 1080 |
assert foo_introspection["possibleTypes"] == [] |
| 1081 |
# we need to patch here since invalid unions cannot be built with Python |
| 1082 |
foo_introspection["possibleTypes"] = [ |
| 1083 |
{"kind": "UNION", "name": "Foo", "ofType": None} |
| 1084 |
] |
| 1085 |
|
| 1086 |
with raises(TypeError) as exc_info: |
| 1087 |
build_client_schema(introspection) |
| 1088 |
assert str(exc_info.value) == ( |
| 1089 |
"Foo types cannot be resolved." |
| 1090 |
" Expected Foo to be a GraphQL Object type." |
| 1091 |
) |