def lark_to_document_node(tree: "Tree") -> "DocumentNode": """ Creates and returns a DocumentNode instance extracted from the parsing of the tree instance. :param tree: the Tree to parse in order to extract the proper node :type tree: Tree :return: a DocumentNode instance extracted from the parsing of the tree :rtype: DocumentNode """ return DocumentNode( definitions=[child.value for child in tree.children], location=lark_to_location_node(tree.meta), )
def test_documentnode__init__(): document_node = DocumentNode(definitions="documentDefinitions", location="documentLocation") assert document_node.definitions == "documentDefinitions" assert document_node.location == "documentLocation"
def document_from_ast_json( document_ast: dict, query: Union[str, bytes], schema: "GraphQLSchema" ) -> "DocumentNode": """ Creates and returns a DocumentNode instance from a document's JSON AST libgraphqlparser representation. :param document_ast: document's JSON AST libgraphqlparser representation :param query: query to parse and transform into a DocumentNode :param schema: the GraphQLSchema instance linked to the engine :type document_ast: dict :type query: Union[str, bytes] :type schema: GraphQLSchema :return: a DocumentNode instance equivalent to the JSON AST representation :rtype: DocumentNode :Example: >>> from tartiflette.language.parsers.libgraphqlparser.transformers import ( >>> document_from_ast_json >>> ) >>> >>> >>> document = document_from_ast_json({ >>> "kind": "Document", >>> "loc": { >>> "start": {"line": 2, "column": 13}, >>> "end": {"line": 4, "column": 14}, >>> }, >>> "definitions": [ >>> { >>> "kind": "OperationDefinition", >>> "loc": { >>> "start": {"line": 2, "column": 13}, >>> "end": {"line": 4, "column": 14}, >>> }, >>> "operation": "query", >>> "name": None, >>> "variableDefinitions": None, >>> "directives": None, >>> "selectionSet": { >>> "kind": "SelectionSet", >>> "loc": { >>> "start": {"line": 2, "column": 13}, >>> "end": {"line": 4, "column": 14}, >>> }, >>> "selections": [ >>> { >>> "kind": "Field", >>> "loc": { >>> "start": {"line": 3, "column": 15}, >>> "end": {"line": 3, "column": 20}, >>> }, >>> "alias": None, >>> "name": { >>> "kind": "Name", >>> "loc": { >>> "start": {"line": 3, "column": 15}, >>> "end": {"line": 3, "column": 20}, >>> }, >>> "value": "hello", >>> }, >>> "arguments": None, >>> "directives": None, >>> "selectionSet": None, >>> } >>> ], >>> }, >>> } >>> ], >>> }, >>> ''' >>> { >>> hello >>> } >>> ''') """ validators = Validators(schema, RULE_SET) definitions = _parse_definitions(document_ast["definitions"], validators) validators.validate( rule="executable-definitions", definitions=definitions, path=None ) return DocumentNode( definitions=definitions, validators=validators, hash_id=hash(query), location=_parse_location(document_ast["loc"]), )
from tartiflette.language.ast import DocumentNode def test_documentnode__init__(): document_node = DocumentNode(definitions="documentDefinitions", location="documentLocation") assert document_node.definitions == "documentDefinitions" assert document_node.location == "documentLocation" @pytest.mark.parametrize( "document_node,other,expected", [ ( DocumentNode(definitions="documentDefinitions", location="documentLocation"), Ellipsis, False, ), ( DocumentNode(definitions="documentDefinitions", location="documentLocation"), DocumentNode( definitions="documentDefinitionsBis", location="documentLocation", ), False, ), ( DocumentNode(definitions="documentDefinitions", location="documentLocation"),
def document_from_ast_json(document_ast: dict) -> "DocumentNode": """ Creates and returns a DocumentNode instance from a document's JSON AST libgraphqlparser representation. :param document_ast: document's JSON AST libgraphqlparser representation :type document_ast: dict :return: a DocumentNode instance equivalent to the JSON AST representation :rtype: DocumentNode :Example: >>> from tartiflette.language.parsers.libgraphqlparser.transformers import ( >>> document_from_ast_json >>> ) >>> >>> >>> document = document_from_ast_json({ >>> "kind": "Document", >>> "loc": { >>> "start": {"line": 2, "column": 13}, >>> "end": {"line": 4, "column": 14}, >>> }, >>> "definitions": [ >>> { >>> "kind": "OperationDefinition", >>> "loc": { >>> "start": {"line": 2, "column": 13}, >>> "end": {"line": 4, "column": 14}, >>> }, >>> "operation": "query", >>> "name": None, >>> "variableDefinitions": None, >>> "directives": None, >>> "selectionSet": { >>> "kind": "SelectionSet", >>> "loc": { >>> "start": {"line": 2, "column": 13}, >>> "end": {"line": 4, "column": 14}, >>> }, >>> "selections": [ >>> { >>> "kind": "Field", >>> "loc": { >>> "start": {"line": 3, "column": 15}, >>> "end": {"line": 3, "column": 20}, >>> }, >>> "alias": None, >>> "name": { >>> "kind": "Name", >>> "loc": { >>> "start": {"line": 3, "column": 15}, >>> "end": {"line": 3, "column": 20}, >>> }, >>> "value": "hello", >>> }, >>> "arguments": None, >>> "directives": None, >>> "selectionSet": None, >>> } >>> ], >>> }, >>> } >>> ], >>> }) """ return DocumentNode( definitions=_parse_definitions(document_ast["definitions"]), location=_parse_location(document_ast["loc"]), )