|
| 1 | +from typing import cast |
| 2 | + |
| 3 | +from graphql.type import ( |
| 4 | + GraphQLArgument, |
| 5 | + GraphQLField, |
| 6 | + GraphQLInputField, |
| 7 | + GraphQLInputObjectType, |
| 8 | + GraphQLObjectType, |
| 9 | + GraphQLSchema, |
| 10 | + GraphQLString, |
| 11 | +) |
1 | 12 | from graphql.utilities import build_schema, lexicographic_sort_schema, print_schema |
2 | 13 |
|
3 | 14 | from ..utils import dedent |
@@ -382,3 +393,102 @@ def sort_recursive_types(): |
382 | 393 | } |
383 | 394 | """ |
384 | 395 | ) |
| 396 | + |
| 397 | + def describe_input_field_properties(): |
| 398 | + def preserves_input_field_deprecation_reason(): |
| 399 | + input_type = GraphQLInputObjectType( |
| 400 | + "TestInput", |
| 401 | + { |
| 402 | + "zField": GraphQLInputField(GraphQLString), |
| 403 | + "aField": GraphQLInputField( |
| 404 | + GraphQLString, deprecation_reason="Use bField instead" |
| 405 | + ), |
| 406 | + "mField": GraphQLInputField( |
| 407 | + GraphQLString, deprecation_reason="Deprecated field" |
| 408 | + ), |
| 409 | + }, |
| 410 | + ) |
| 411 | + query_type = GraphQLObjectType( |
| 412 | + "Query", |
| 413 | + { |
| 414 | + "dummy": GraphQLField( |
| 415 | + GraphQLString, args={"input": GraphQLArgument(input_type)} |
| 416 | + ) |
| 417 | + }, |
| 418 | + ) |
| 419 | + schema = GraphQLSchema(query=query_type, types=[input_type]) |
| 420 | + sorted_schema = lexicographic_sort_schema(schema) |
| 421 | + |
| 422 | + sorted_input_type = cast( |
| 423 | + "GraphQLInputObjectType", sorted_schema.type_map["TestInput"] |
| 424 | + ) |
| 425 | + field_names = list(sorted_input_type.fields) |
| 426 | + assert field_names == ["aField", "mField", "zField"] |
| 427 | + |
| 428 | + assert ( |
| 429 | + sorted_input_type.fields["aField"].deprecation_reason |
| 430 | + == "Use bField instead" |
| 431 | + ) |
| 432 | + assert ( |
| 433 | + sorted_input_type.fields["mField"].deprecation_reason |
| 434 | + == "Deprecated field" |
| 435 | + ) |
| 436 | + assert sorted_input_type.fields["zField"].deprecation_reason is None |
| 437 | + |
| 438 | + def preserves_input_field_extensions(): |
| 439 | + input_type = GraphQLInputObjectType( |
| 440 | + "TestInput", |
| 441 | + { |
| 442 | + "zField": GraphQLInputField(GraphQLString, extensions={"x": 1}), |
| 443 | + "aField": GraphQLInputField( |
| 444 | + GraphQLString, extensions={"custom": "value"} |
| 445 | + ), |
| 446 | + }, |
| 447 | + ) |
| 448 | + query_type = GraphQLObjectType( |
| 449 | + "Query", |
| 450 | + { |
| 451 | + "dummy": GraphQLField( |
| 452 | + GraphQLString, args={"input": GraphQLArgument(input_type)} |
| 453 | + ) |
| 454 | + }, |
| 455 | + ) |
| 456 | + schema = GraphQLSchema(query=query_type, types=[input_type]) |
| 457 | + sorted_schema = lexicographic_sort_schema(schema) |
| 458 | + sorted_input_type = cast( |
| 459 | + "GraphQLInputObjectType", sorted_schema.type_map["TestInput"] |
| 460 | + ) |
| 461 | + |
| 462 | + field_names = list(sorted_input_type.fields) |
| 463 | + assert field_names == ["aField", "zField"] |
| 464 | + |
| 465 | + assert sorted_input_type.fields["aField"].extensions == {"custom": "value"} |
| 466 | + assert sorted_input_type.fields["zField"].extensions == {"x": 1} |
| 467 | + |
| 468 | + def preserves_input_field_out_name(): |
| 469 | + input_type = GraphQLInputObjectType( |
| 470 | + "TestInput", |
| 471 | + { |
| 472 | + "zField": GraphQLInputField(GraphQLString, out_name="z_field"), |
| 473 | + "aField": GraphQLInputField(GraphQLString, out_name="a_field"), |
| 474 | + }, |
| 475 | + ) |
| 476 | + query_type = GraphQLObjectType( |
| 477 | + "Query", |
| 478 | + { |
| 479 | + "dummy": GraphQLField( |
| 480 | + GraphQLString, args={"input": GraphQLArgument(input_type)} |
| 481 | + ) |
| 482 | + }, |
| 483 | + ) |
| 484 | + schema = GraphQLSchema(query=query_type, types=[input_type]) |
| 485 | + sorted_schema = lexicographic_sort_schema(schema) |
| 486 | + sorted_input_type = cast( |
| 487 | + "GraphQLInputObjectType", sorted_schema.type_map["TestInput"] |
| 488 | + ) |
| 489 | + |
| 490 | + field_names = list(sorted_input_type.fields) |
| 491 | + assert field_names == ["aField", "zField"] |
| 492 | + |
| 493 | + assert sorted_input_type.fields["aField"].out_name == "a_field" |
| 494 | + assert sorted_input_type.fields["zField"].out_name == "z_field" |
0 commit comments