1from typing import Any, Dict, List, Optional, Union 2 3from tartiflette.language.ast import ( 4 ArgumentNode, 5 BooleanValueNode, 6 DescriptionNode, 7 DirectiveDefinitionNode, 8 DirectiveNode, 9 DocumentNode, 10 EnumTypeDefinitionNode, 11 EnumTypeExtensionNode, 12 EnumValueDefinitionNode, 13 EnumValueNode, 14 FieldDefinitionNode, 15 FloatValueNode, 16 InputObjectTypeDefinitionNode, 17 InputObjectTypeExtensionNode, 18 InputValueDefinitionNode, 19 InterfaceTypeDefinitionNode, 20 InterfaceTypeExtensionNode, 21 IntValueNode, 22 ListTypeNode, 23 ListValueNode, 24 Location, 25 NamedTypeNode, 26 NameNode, 27 NonNullTypeNode, 28 NullValueNode, 29 ObjectFieldNode, 30 ObjectTypeDefinitionNode, 31 ObjectTypeExtensionNode, 32 ObjectValueNode, 33 OperationTypeDefinitionNode, 34 ScalarTypeDefinitionNode, 35 ScalarTypeExtensionNode, 36 SchemaDefinitionNode, 37 SchemaExtensionNode, 38 StringValueNode, 39 UnionTypeDefinitionNode, 40 UnionTypeExtensionNode, 41) 42 43__all__ = ( 44 "lark_to_int_value_node", 45 "lark_to_float_value_node", 46 "lark_to_string_value_node", 47 "lark_to_boolean_value_node", 48 "lark_to_null_value_node", 49 "lark_to_enum_value_node", 50 "lark_to_list_value_node", 51 "lark_to_object_field_node", 52 "lark_to_object_value_node", 53 "lark_to_name_node", 54 "lark_to_description_node", 55 "lark_to_named_type_node", 56 "lark_to_argument_node", 57 "lark_to_directive_node", 58 "lark_to_operation_type_definition_node", 59 "lark_to_schema_definition_node", 60 "lark_to_scalar_type_definition_node", 61 "lark_to_list_type_node", 62 "lark_to_non_null_type_node", 63 "lark_to_input_value_definition_node", 64 "lark_to_directive_definition_node", 65 "lark_to_implements_interfaces_node", 66 "lark_to_field_definition_node", 67 "lark_to_object_type_definition_node", 68 "lark_to_interface_type_definition_node", 69 "lark_to_union_type_definition_node", 70 "lark_to_enum_value_definition_node", 71 "lark_to_enum_type_definition_node", 72 "lark_to_input_object_type_definition_node", 73 "lark_to_schema_extension_node", 74 "lark_to_scalar_type_extension_node", 75 "lark_to_object_type_extension_node", 76 "lark_to_interface_type_extension_node", 77 "lark_to_union_type_extension_node", 78 "lark_to_enum_type_extension_node", 79 "lark_to_input_object_type_extension_node", 80 "lark_to_document_node", 81) 82 83 84class UnexpectedASTNode(Exception): 85 """ 86 Raised when an unexpected node type is encoutered. 87 """ 88 89 90def _extract_node_info( 91 children: List[Union["Token", "SchemaNode"]], 92 types_to_value: Optional[List[str]] = None, 93 types_to_list: Optional[Dict[str, str]] = None, 94 types_to_ignore: Optional[List[str]] = None, 95) -> Dict[str, Any]: 96 """ 97 Extracts information from a node's children depending on the parameters 98 options. 99 :param children: list of child to parse and from which we should extract 100 information 101 :param types_to_value: list of child types for which we should add the 102 value to the returned information 103 :param types_to_list: mapping of child type to returned information key. 104 The key must be the type of children and the value the key where all 105 matched children will be stored in the returned information 106 :param types_to_ignore: list of node type we should ignore 107 :type children: List[Union[Token, SchemaNode]] 108 :type types_to_value: Optional[List[str]] 109 :type types_to_list: Optional[Dict[str, str]] 110 :type types_to_ignore: Optional[List[str]] 111 :return: a dictionary containing data extracted from children 112 :rtype: Dict[str, Any] 113 :raises UnexpectedASTNode: raised when an unexpected type is encountered 114 """ 115 types_to_value = types_to_value or [] 116 types_to_list = types_to_list or {} 117 types_to_ignore = types_to_ignore or [] 118 119 info = {type_to_list: [] for type_to_list in types_to_list.values()} 120 for child in children: 121 if child.type in types_to_value: 122 info[child.type] = child.value 123 elif child.type in types_to_list: 124 info[types_to_list[child.type]].append(child) 125 elif child.type in types_to_ignore: 126 continue 127 else: 128 raise UnexpectedASTNode( 129 "Unexpected AST node `{}`, type `{}`".format( 130 child, child.__class__.__name__ 131 ) 132 ) 133 return info 134 135 136def lark_to_location_node(tree_meta: "Meta") -> "Location": 137 """ 138 Creates and returns a Location instance from a Tree Meta instance. 139 :param tree_meta: Tree Meta Lark instance containing metadata 140 :type tree_meta: Meta 141 :return: a Location instance 142 :rtype: Location 143 """ 144 return Location( 145 line=tree_meta.line, 146 column=tree_meta.column, 147 line_end=tree_meta.end_line, 148 column_end=tree_meta.end_column, 149 ) 150 151 152def lark_to_int_value_node(tree: "Tree") -> "IntValueNode": 153 """ 154 Creates and returns an IntValueNode instance extracted from the parsing of 155 the tree instance. 156 :param tree: the Tree to parse in order to extract the proper node 157 :type tree: Tree 158 :return: an IntValueNode instance extracted from the parsing of the tree 159 :rtype: IntValueNode 160 """ 161 return IntValueNode( 162 value=tree.children[0].value, location=lark_to_location_node(tree.meta) 163 ) 164 165 166def lark_to_float_value_node(tree: "Tree") -> "FloatValueNode": 167 """ 168 Creates and returns a FloatValueNode instance extracted from the parsing of 169 the tree instance. 170 :param tree: the Tree to parse in order to extract the proper node 171 :type tree: Tree 172 :return: a FloatValueNode instance extracted from the parsing of the tree 173 :rtype: FloatValueNode 174 """ 175 return FloatValueNode( 176 value=tree.children[0].value, location=lark_to_location_node(tree.meta) 177 ) 178 179 180def lark_to_string_value_node(tree: "Tree") -> "StringValueNode": 181 """ 182 Creates and returns a StringValueNode instance extracted from the parsing 183 of the tree instance. 184 :param tree: the Tree to parse in order to extract the proper node 185 :type tree: Tree 186 :return: a StringValueNode instance extracted from the parsing of the tree 187 :rtype: StringValueNode 188 """ 189 return StringValueNode( 190 value=tree.children[0].value, location=lark_to_location_node(tree.meta) 191 ) 192 193 194def lark_to_boolean_value_node(tree: "Tree") -> "BooleanValueNode": 195 """ 196 Creates and returns a BooleanValueNode instance extracted from the parsing 197 of the tree instance. 198 :param tree: the Tree to parse in order to extract the proper node 199 :type tree: Tree 200 :return: a BooleanValueNode instance extracted from the parsing of the tree 201 :rtype: BooleanValueNode 202 """ 203 return BooleanValueNode( 204 value=tree.children[0].value, location=lark_to_location_node(tree.meta) 205 ) 206 207 208def lark_to_null_value_node(tree: "Tree") -> "NullValueNode": 209 """ 210 Creates and returns a NullValueNode instance extracted from the parsing of 211 the tree instance. 212 :param tree: the Tree to parse in order to extract the proper node 213 :type tree: Tree 214 :return: a NullValueNode instance extracted from the parsing of the tree 215 :rtype: NullValueNode 216 """ 217 return NullValueNode(location=lark_to_location_node(tree.meta)) 218 219 220def lark_to_enum_value_node(tree: "Tree") -> "EnumValueNode": 221 """ 222 Creates and returns an EnumValueNode instance extracted from the parsing of 223 the tree instance. 224 :param tree: the Tree to parse in order to extract the proper node 225 :type tree: Tree 226 :return: an EnumValueNode instance extracted from the parsing of the tree 227 :rtype: EnumValueNode 228 """ 229 return EnumValueNode( 230 value=tree.children[0].value, location=lark_to_location_node(tree.meta) 231 ) 232 233 234def lark_to_list_value_node(tree: "Tree") -> "ListValueNode": 235 """ 236 Creates and returns a ListValueNode instance extracted from the parsing of 237 the tree instance. 238 :param tree: the Tree to parse in order to extract the proper node 239 :type tree: Tree 240 :return: a ListValueNode instance extracted from the parsing of the tree 241 :rtype: ListValueNode 242 """ 243 return ListValueNode( 244 values=[child.value for child in tree.children], 245 location=lark_to_location_node(tree.meta), 246 ) 247 248 249def lark_to_object_field_node(tree: "Tree") -> "ObjectFieldNode": 250 """ 251 Creates and returns an ObjectFieldNode instance extracted from the parsing 252 of the tree instance. 253 :param tree: the Tree to parse in order to extract the proper node 254 :type tree: Tree 255 :return: an ObjectFieldNode instance extracted from the parsing of the tree 256 :rtype: ObjectFieldNode 257 """ 258 node_info = _extract_node_info( 259 tree.children, types_to_value=["name", "value"] 260 ) 261 262 return ObjectFieldNode( 263 name=node_info["name"], 264 value=node_info["value"], 265 location=lark_to_location_node(tree.meta), 266 ) 267 268 269def lark_to_object_value_node(tree: "Tree") -> "ObjectValueNode": 270 """ 271 Creates and returns an ObjectValueNode instance extracted from the parsing 272 of the tree instance. 273 :param tree: the Tree to parse in order to extract the proper node 274 :type tree: Tree 275 :return: an ObjectValueNode instance extracted from the parsing of the tree 276 :rtype: ObjectValueNode 277 """ 278 return ObjectValueNode( 279 fields=[child.value for child in tree.children], 280 location=lark_to_location_node(tree.meta), 281 ) 282 283 284def lark_to_name_node(tree: "Tree") -> "NameNode": 285 """ 286 Creates and returns a NameNode instance extracted from the parsing of the 287 tree instance. 288 :param tree: the Tree to parse in order to extract the proper node 289 :type tree: Tree 290 :return: a NameNode instance extracted from the parsing of the tree 291 :rtype: NameNode 292 """ 293 return NameNode( 294 value=tree.children[0].value, location=lark_to_location_node(tree.meta) 295 ) 296 297 298def lark_to_description_node(tree: "Tree") -> "DescriptionNode": 299 """ 300 Creates and returns a DescriptionNode instance extracted from the parsing 301 of the tree instance. 302 :param tree: the Tree to parse in order to extract the proper node 303 :type tree: Tree 304 :return: a DescriptionNode instance extracted from the parsing of the tree 305 :rtype: DescriptionNode 306 """ 307 return DescriptionNode( 308 value=tree.children[0].value, location=lark_to_location_node(tree.meta) 309 ) 310 311 312def lark_to_named_type_node(tree: "Tree") -> "NamedTypeNode": 313 """ 314 Creates and returns a NamedTypeNode instance extracted from the parsing of 315 the tree instance. 316 :param tree: the Tree to parse in order to extract the proper node 317 :type tree: Tree 318 :return: a NamedTypeNode instance extracted from the parsing of the tree 319 :rtype: NamedTypeNode 320 """ 321 return NamedTypeNode( 322 name=tree.children[0].value, location=lark_to_location_node(tree.meta) 323 ) 324 325 326def lark_to_argument_node(tree: "Tree") -> "ArgumentNode": 327 """ 328 Creates and returns an ArgumentNode instance extracted from the parsing of 329 the tree instance. 330 :param tree: the Tree to parse in order to extract the proper node 331 :type tree: Tree 332 :return: an ArgumentNode instance extracted from the parsing of the tree 333 :rtype: ArgumentNode 334 """ 335 node_info = _extract_node_info( 336 tree.children, types_to_value=["name", "value"] 337 ) 338 339 return ArgumentNode( 340 name=node_info["name"], 341 value=node_info["value"], 342 location=lark_to_location_node(tree.meta), 343 ) 344 345 346def lark_to_directive_node(tree: "Tree") -> "DirectiveNode": 347 """ 348 Creates and returns a DirectiveNode instance extracted from the parsing of 349 the tree instance. 350 :param tree: the Tree to parse in order to extract the proper node 351 :type tree: Tree 352 :return: a DirectiveNode instance extracted from the parsing of the tree 353 :rtype: DirectiveNode 354 """ 355 node_info = _extract_node_info( 356 tree.children, types_to_value=["name", "arguments"] 357 ) 358 359 return DirectiveNode( 360 name=node_info["name"], 361 arguments=node_info.get("arguments") or [], 362 location=lark_to_location_node(tree.meta), 363 ) 364 365 366def lark_to_operation_type_definition_node( 367 tree: "Tree", 368) -> "OperationTypeDefinitionNode": 369 """ 370 Creates and returns an OperationTypeDefinitionNode instance extracted from 371 the parsing of the tree instance. 372 :param tree: the Tree to parse in order to extract the proper node 373 :type tree: Tree 374 :return: an OperationTypeDefinitionNode instance extracted from the parsing 375 of the tree 376 :rtype: OperationTypeDefinitionNode 377 """ 378 node_info = _extract_node_info( 379 tree.children, types_to_value=["operation_type", "named_type"] 380 ) 381 382 return OperationTypeDefinitionNode( 383 operation_type=node_info["operation_type"], 384 type=node_info["named_type"], 385 location=lark_to_location_node(tree.meta), 386 ) 387 388 389def lark_to_schema_definition_node(tree: "Tree") -> "SchemaDefinitionNode": 390 """ 391 Creates and returns a SchemaDefinitionNode instance extracted from the 392 parsing of the tree instance. 393 :param tree: the Tree to parse in order to extract the proper node 394 :type tree: Tree 395 :return: a SchemaDefinitionNode instance extracted from the parsing of the 396 tree 397 :rtype: SchemaDefinitionNode 398 """ 399 node_info = _extract_node_info( 400 tree.children, 401 types_to_value=["directives"], 402 types_to_list={ 403 "operation_type_definition": "operation_type_definitions" 404 }, 405 types_to_ignore=["SCHEMA"], 406 ) 407 408 return SchemaDefinitionNode( 409 directives=node_info.get("directives") or [], 410 operation_type_definitions=[ 411 operation_type_definition.value 412 for operation_type_definition in node_info.get( 413 "operation_type_definitions" 414 ) 415 or [] 416 ], 417 location=lark_to_location_node(tree.meta), 418 ) 419 420 421def lark_to_scalar_type_definition_node( 422 tree: "Tree", 423) -> "ScalarTypeDefinitionNode": 424 """ 425 Creates and returns a ScalarTypeDefinitionNode instance extracted from the 426 parsing of the tree instance. 427 :param tree: the Tree to parse in order to extract the proper node 428 :type tree: Tree 429 :return: a ScalarTypeDefinitionNode instance extracted from the parsing of 430 the tree 431 :rtype: ScalarTypeDefinitionNode 432 """ 433 node_info = _extract_node_info( 434 tree.children, 435 types_to_value=["description", "name", "directives"], 436 types_to_ignore=["SCALAR"], 437 ) 438 439 return ScalarTypeDefinitionNode( 440 description=node_info.get("description"), 441 name=node_info["name"], 442 directives=node_info.get("directives") or [], 443 location=lark_to_location_node(tree.meta), 444 ) 445 446 447def lark_to_list_type_node(tree: "Tree") -> "ListTypeNode": 448 """ 449 Creates and returns a ListTypeNode instance extracted from the parsing of 450 the tree instance. 451 :param tree: the Tree to parse in order to extract the proper node 452 :type tree: Tree 453 :return: a ListTypeNode instance extracted from the parsing of the tree 454 :rtype: ListTypeNode 455 """ 456 return ListTypeNode( 457 type=tree.children[0].value, location=lark_to_location_node(tree.meta) 458 ) 459 460 461def lark_to_non_null_type_node(tree: "Tree") -> "NonNullTypeNode": 462 """ 463 Creates and returns a NonNullTypeNode instance extracted from the parsing 464 of the tree instance. 465 :param tree: the Tree to parse in order to extract the proper node 466 :type tree: Tree 467 :return: a NonNullTypeNode instance extracted from the parsing of the tree 468 :rtype: NonNullTypeNode 469 """ 470 return NonNullTypeNode( 471 type=tree.children[0].value, location=lark_to_location_node(tree.meta) 472 ) 473 474 475def lark_to_input_value_definition_node( 476 tree: "Tree", 477) -> "InputValueDefinitionNode": 478 """ 479 Creates and returns an InputValueDefinitionNode instance extracted from the 480 parsing of the tree instance. 481 :param tree: the Tree to parse in order to extract the proper node 482 :type tree: Tree 483 :return: an InputValueDefinitionNode instance extracted from the parsing of 484 the tree 485 :rtype: InputValueDefinitionNode 486 """ 487 node_info = _extract_node_info( 488 tree.children, 489 types_to_value=[ 490 "description", 491 "name", 492 "type", 493 "default_value", 494 "directives", 495 ], 496 ) 497 498 return InputValueDefinitionNode( 499 description=node_info.get("description"), 500 name=node_info["name"], 501 type=node_info["type"], 502 default_value=node_info.get("default_value"), 503 directives=node_info.get("directives") or [], 504 location=lark_to_location_node(tree.meta), 505 ) 506 507 508def lark_to_directive_definition_node( 509 tree: "Tree", 510) -> "DirectiveDefinitionNode": 511 """ 512 Creates and returns a DirectiveDefinitionNode instance extracted from the 513 parsing of the tree instance. 514 :param tree: the Tree to parse in order to extract the proper node 515 :type tree: Tree 516 :return: a DirectiveDefinitionNode instance extracted from the parsing of 517 the tree 518 :rtype: DirectiveDefinitionNode 519 """ 520 node_info = _extract_node_info( 521 tree.children, 522 types_to_value=[ 523 "description", 524 "name", 525 "arguments_definition", 526 "directive_locations", 527 ], 528 types_to_ignore=["DIRECTIVE", "ON"], 529 ) 530 531 return DirectiveDefinitionNode( 532 description=node_info.get("description"), 533 name=node_info["name"], 534 arguments=node_info.get("arguments_definition") or [], 535 locations=node_info.get("directive_locations") or [], 536 location=lark_to_location_node(tree.meta), 537 ) 538 539 540def lark_to_implements_interfaces_node(tree: "Tree") -> List["NamedTypeNode"]: 541 """ 542 Creates and returns a list of NamedTypeNode instance extracted from the 543 parsing of the tree instance. 544 :param tree: the Tree to parse in order to extract the proper node 545 :type tree: Tree 546 :return: a list of NamedTypeNode instance extracted from the parsing of the 547 tree 548 :rtype: List[NamedTypeNode] 549 """ 550 node_info = _extract_node_info( 551 tree.children, 552 types_to_list={"named_type": "named_types"}, 553 types_to_ignore=["IMPLEMENTS"], 554 ) 555 556 return [ 557 named_type.value for named_type in node_info.get("named_types") or [] 558 ] 559 560 561def lark_to_field_definition_node(tree: "Tree") -> "FieldDefinitionNode": 562 """ 563 Creates and returns a FieldDefinitionNode instance extracted from the 564 parsing of the tree instance. 565 :param tree: the Tree to parse in order to extract the proper node 566 :type tree: Tree 567 :return: a FieldDefinitionNode instance extracted from the parsing of the 568 tree 569 :rtype: FieldDefinitionNode 570 """ 571 node_info = _extract_node_info( 572 tree.children, 573 types_to_value=[ 574 "description", 575 "name", 576 "arguments_definition", 577 "type", 578 "directives", 579 ], 580 ) 581 582 return FieldDefinitionNode( 583 description=node_info.get("description"), 584 name=node_info["name"], 585 arguments=node_info.get("arguments_definition") or [], 586 type=node_info["type"], 587 directives=node_info.get("directives") or [], 588 location=lark_to_location_node(tree.meta), 589 ) 590 591 592def lark_to_object_type_definition_node( 593 tree: "Tree", 594) -> "ObjectTypeDefinitionNode": 595 """ 596 Creates and returns an ObjectTypeDefinitionNode instance extracted from the 597 parsing of the tree instance. 598 :param tree: the Tree to parse in order to extract the proper node 599 :type tree: Tree 600 :return: an ObjectTypeDefinitionNode instance extracted from the parsing of 601 the tree 602 :rtype: ObjectTypeDefinitionNode 603 """ 604 node_info = _extract_node_info( 605 tree.children, 606 types_to_value=[ 607 "description", 608 "name", 609 "implements_interfaces", 610 "directives", 611 "fields_definition", 612 ], 613 types_to_ignore=["TYPE"], 614 ) 615 616 return ObjectTypeDefinitionNode( 617 description=node_info.get("description"), 618 name=node_info["name"], 619 interfaces=node_info.get("implements_interfaces") or [], 620 directives=node_info.get("directives") or [], 621 fields=node_info.get("fields_definition") or [], 622 location=lark_to_location_node(tree.meta), 623 ) 624 625 626def lark_to_interface_type_definition_node( 627 tree: "Tree", 628) -> "InterfaceTypeDefinitionNode": 629 """ 630 Creates and returns an InterfaceTypeDefinitionNode instance extracted from 631 the parsing of the tree instance. 632 :param tree: the Tree to parse in order to extract the proper node 633 :type tree: Tree 634 :return: an InterfaceTypeDefinitionNode instance extracted from the parsing 635 of the tree 636 :rtype: InterfaceTypeDefinitionNode 637 """ 638 node_info = _extract_node_info( 639 tree.children, 640 types_to_value=[ 641 "description", 642 "name", 643 "directives", 644 "fields_definition", 645 ], 646 types_to_ignore=["INTERFACE"], 647 ) 648 649 return InterfaceTypeDefinitionNode( 650 description=node_info.get("description"), 651 name=node_info["name"], 652 directives=node_info.get("directives") or [], 653 fields=node_info.get("fields_definition") or [], 654 location=lark_to_location_node(tree.meta), 655 ) 656 657 658def lark_to_union_type_definition_node( 659 tree: "Tree", 660) -> "UnionTypeDefinitionNode": 661 """ 662 Creates and returns an UnionTypeDefinitionNode instance extracted from the 663 parsing of the tree instance. 664 :param tree: the Tree to parse in order to extract the proper node 665 :type tree: Tree 666 :return: an UnionTypeDefinitionNode instance extracted from the parsing of 667 the tree 668 :rtype: UnionTypeDefinitionNode 669 """ 670 node_info = _extract_node_info( 671 tree.children, 672 types_to_value=[ 673 "description", 674 "name", 675 "directives", 676 "union_member_types", 677 ], 678 types_to_ignore=["UNION"], 679 ) 680 681 return UnionTypeDefinitionNode( 682 description=node_info.get("description"), 683 name=node_info["name"], 684 directives=node_info.get("directives") or [], 685 types=node_info.get("union_member_types") or [], 686 location=lark_to_location_node(tree.meta), 687 ) 688 689 690def lark_to_enum_value_definition_node( 691 tree: "Tree", 692) -> "EnumValueDefinitionNode": 693 """ 694 Creates and returns an EnumValueDefinitionNode instance extracted from the 695 parsing of the tree instance. 696 :param tree: the Tree to parse in order to extract the proper node 697 :type tree: Tree 698 :return: an EnumValueDefinitionNode instance extracted from the parsing of 699 the tree 700 :rtype: EnumValueDefinitionNode 701 """ 702 node_info = _extract_node_info( 703 tree.children, 704 types_to_value=["description", "enum_value", "directives"], 705 ) 706 707 return EnumValueDefinitionNode( 708 description=node_info.get("description"), 709 name=node_info["enum_value"], 710 directives=node_info.get("directives") or [], 711 location=lark_to_location_node(tree.meta), 712 ) 713 714 715def lark_to_enum_type_definition_node( 716 tree: "Tree", 717) -> "EnumTypeDefinitionNode": 718 """ 719 Creates and returns an EnumTypeDefinitionNode instance extracted from the 720 parsing of the tree instance. 721 :param tree: the Tree to parse in order to extract the proper node 722 :type tree: Tree 723 :return: an EnumTypeDefinitionNode instance extracted from the parsing of 724 the tree 725 :rtype: EnumTypeDefinitionNode 726 """ 727 node_info = _extract_node_info( 728 tree.children, 729 types_to_value=[ 730 "description", 731 "name", 732 "directives", 733 "enum_values_definition", 734 ], 735 types_to_ignore=["ENUM"], 736 ) 737 738 return EnumTypeDefinitionNode( 739 description=node_info.get("description"), 740 name=node_info["name"], 741 directives=node_info.get("directives") or [], 742 values=node_info.get("enum_values_definition") or [], 743 location=lark_to_location_node(tree.meta), 744 ) 745 746 747def lark_to_input_object_type_definition_node( 748 tree: "Tree", 749) -> "InputObjectTypeDefinitionNode": 750 """ 751 Creates and returns an InputObjectTypeDefinitionNode instance extracted 752 from the parsing of the tree instance. 753 :param tree: the Tree to parse in order to extract the proper node 754 :type tree: Tree 755 :return: an InputObjectTypeDefinitionNode instance extracted from the 756 parsing of the tree 757 :rtype: InputObjectTypeDefinitionNode 758 """ 759 node_info = _extract_node_info( 760 tree.children, 761 types_to_value=[ 762 "description", 763 "name", 764 "directives", 765 "input_fields_definition", 766 ], 767 types_to_ignore=["INPUT"], 768 ) 769 770 return InputObjectTypeDefinitionNode( 771 description=node_info.get("description"), 772 name=node_info["name"], 773 directives=node_info.get("directives") or [], 774 fields=node_info.get("input_fields_definition") or [], 775 location=lark_to_location_node(tree.meta), 776 ) 777 778 779def lark_to_schema_extension_node(tree: "Tree") -> "SchemaExtensionNode": 780 """ 781 Creates and returns a SchemaExtensionNode instance extracted from the 782 parsing of the tree instance. 783 :param tree: the Tree to parse in order to extract the proper node 784 :type tree: Tree 785 :return: a SchemaExtensionNode instance extracted from the parsing of the 786 tree 787 :rtype: SchemaExtensionNode 788 """ 789 node_info = _extract_node_info( 790 tree.children, 791 types_to_value=["directives"], 792 types_to_list={ 793 "operation_type_definition": "operation_type_definitions" 794 }, 795 types_to_ignore=["EXTEND", "SCHEMA"], 796 ) 797 798 return SchemaExtensionNode( 799 directives=node_info.get("directives") or [], 800 operation_type_definitions=[ 801 operation_type_definition.value 802 for operation_type_definition in node_info.get( 803 "operation_type_definitions" 804 ) 805 or [] 806 ], 807 location=lark_to_location_node(tree.meta), 808 ) 809 810 811def lark_to_scalar_type_extension_node( 812 tree: "Tree", 813) -> "ScalarTypeExtensionNode": 814 """ 815 Creates and returns a ScalarTypeExtensionNode instance extracted from the 816 parsing of the tree instance. 817 :param tree: the Tree to parse in order to extract the proper node 818 :type tree: Tree 819 :return: a ScalarTypeExtensionNode instance extracted from the parsing of 820 the tree 821 :rtype: ScalarTypeExtensionNode 822 """ 823 node_info = _extract_node_info( 824 tree.children, 825 types_to_value=["name", "directives"], 826 types_to_ignore=["EXTEND", "SCALAR"], 827 ) 828 829 return ScalarTypeExtensionNode( 830 name=node_info["name"], 831 directives=node_info.get("directives") or [], 832 location=lark_to_location_node(tree.meta), 833 ) 834 835 836def lark_to_object_type_extension_node( 837 tree: "Tree", 838) -> "ObjectTypeExtensionNode": 839 """ 840 Creates and returns an ObjectTypeExtensionNode instance extracted from the 841 parsing of the tree instance. 842 :param tree: the Tree to parse in order to extract the proper node 843 :type tree: Tree 844 :return: an ObjectTypeExtensionNode instance extracted from the parsing of 845 the tree 846 :rtype: ObjectTypeExtensionNode 847 """ 848 node_info = _extract_node_info( 849 tree.children, 850 types_to_value=[ 851 "name", 852 "implements_interfaces", 853 "directives", 854 "fields_definition", 855 ], 856 types_to_ignore=["EXTEND", "TYPE"], 857 ) 858 859 return ObjectTypeExtensionNode( 860 name=node_info["name"], 861 interfaces=node_info.get("implements_interfaces") or [], 862 directives=node_info.get("directives") or [], 863 fields=node_info.get("fields_definition") or [], 864 location=lark_to_location_node(tree.meta), 865 ) 866 867 868def lark_to_interface_type_extension_node( 869 tree: "Tree", 870) -> "InterfaceTypeExtensionNode": 871 """ 872 Creates and returns an InterfaceTypeExtensionNode instance extracted from 873 the parsing of the tree instance. 874 :param tree: the Tree to parse in order to extract the proper node 875 :type tree: Tree 876 :return: an InterfaceTypeExtensionNode instance extracted from the parsing 877 of the tree 878 :rtype: InterfaceTypeExtensionNode 879 """ 880 node_info = _extract_node_info( 881 tree.children, 882 types_to_value=["name", "directives", "fields_definition"], 883 types_to_ignore=["EXTEND", "INTERFACE"], 884 ) 885 886 return InterfaceTypeExtensionNode( 887 name=node_info["name"], 888 directives=node_info.get("directives") or [], 889 fields=node_info.get("fields_definition") or [], 890 location=lark_to_location_node(tree.meta), 891 ) 892 893 894def lark_to_union_type_extension_node( 895 tree: "Tree", 896) -> "UnionTypeExtensionNode": 897 """ 898 Creates and returns an UnionTypeExtensionNode instance extracted from the 899 parsing of the tree instance. 900 :param tree: the Tree to parse in order to extract the proper node 901 :type tree: Tree 902 :return: an UnionTypeExtensionNode instance extracted from the parsing of 903 the tree 904 :rtype: UnionTypeExtensionNode 905 """ 906 node_info = _extract_node_info( 907 tree.children, 908 types_to_value=["name", "directives", "union_member_types"], 909 types_to_ignore=["EXTEND", "UNION"], 910 ) 911 912 return UnionTypeExtensionNode( 913 name=node_info["name"], 914 directives=node_info.get("directives") or [], 915 types=node_info.get("union_member_types") or [], 916 location=lark_to_location_node(tree.meta), 917 ) 918 919 920def lark_to_enum_type_extension_node(tree: "Tree") -> "EnumTypeExtensionNode": 921 """ 922 Creates and returns an EnumTypeExtensionNode instance extracted from the 923 parsing of the tree instance. 924 :param tree: the Tree to parse in order to extract the proper node 925 :type tree: Tree 926 :return: an EnumTypeExtensionNode instance extracted from the parsing of 927 the tree 928 :rtype: EnumTypeExtensionNode 929 """ 930 node_info = _extract_node_info( 931 tree.children, 932 types_to_value=["name", "directives", "enum_values_definition"], 933 types_to_ignore=["EXTEND", "ENUM"], 934 ) 935 936 return EnumTypeExtensionNode( 937 name=node_info["name"], 938 directives=node_info.get("directives") or [], 939 values=node_info.get("enum_values_definition") or [], 940 location=lark_to_location_node(tree.meta), 941 ) 942 943 944def lark_to_input_object_type_extension_node( 945 tree: "Tree", 946) -> "InputObjectTypeExtensionNode": 947 """ 948 Creates and returns an InputObjectTypeExtensionNode instance extracted from 949 the parsing of the tree instance. 950 :param tree: the Tree to parse in order to extract the proper node 951 :type tree: Tree 952 :return: an InputObjectTypeExtensionNode instance extracted from the 953 parsing of the tree 954 :rtype: InputObjectTypeExtensionNode 955 """ 956 node_info = _extract_node_info( 957 tree.children, 958 types_to_value=["name", "directives", "input_fields_definition"], 959 types_to_ignore=["EXTEND", "INPUT"], 960 ) 961 962 return InputObjectTypeExtensionNode( 963 name=node_info["name"], 964 directives=node_info.get("directives") or [], 965 fields=node_info.get("input_fields_definition") or [], 966 location=lark_to_location_node(tree.meta), 967 ) 968 969 970def lark_to_document_node(tree: "Tree") -> "DocumentNode": 971 """ 972 Creates and returns a DocumentNode instance extracted from the parsing of 973 the tree instance. 974 :param tree: the Tree to parse in order to extract the proper node 975 :type tree: Tree 976 :return: a DocumentNode instance extracted from the parsing of the tree 977 :rtype: DocumentNode 978 """ 979 return DocumentNode( 980 definitions=[child.value for child in tree.children], 981 location=lark_to_location_node(tree.meta), 982 ) 983